Get a notification when laptop battery is fully charged

I am surprised of the lack of option in Ubuntu to displayย a notification when laptop battery is fully charged

Therefore I have written a small script to do it :

V1mCN

Script:

 

Installation:

Run:

Copy the script to ~/bin folder (why the ~/bin folder?)

Or copy it to /usr/local/bin if you want it to be available for all users on your computer:

Then add batteryfull.sh script as a startup application by:

  • Open Dash
  • Search for Startup Applications
  • Double-click the icon
  • Click Add and fill in as follows:

rwMJc

 

Done! Do not hesitate to leave a feedback or to send pull request on the Github repositoryย ๐Ÿ™‚

  • Franck Matteoli

    Thanks for this script!!
    I modified it a little because I wanted to have a notification when I plug / unplug the AC cable and wanted other icons (see code and screenshot below). My first script in bash using notifications, i m sure it is not really “clean code” but it works ๐Ÿ˜‰

    
    #!/usr/bin/env bash
    #Declare Variables
    declare -i battery_percent
    declare -i init_time
    declare -i interval
    #Declare Icons Paths
    b_full=$PWD/Documents/battery_icon/battery-full-icon.png
    b_char=$PWD/Documents/battery_icon/battery-loading-icon.png
    b_empt=$PWD/Documents/battery_icon/battery-empty-icon.png
    b_disc=$PWD/Documents/battery_icon/battery-discharging-icon.png
    #Declare Functions
    function display_notification () {
        #check status of ac_power and status of battery
        if on_ac_power; then
            if [ "$battery_percent" -gt 98 ]; then
                 notify-send -i $b_full "Battery fully charged." "AC Power plugged and Battery Level: ${battery_percent}% "
            fi
            if [ "$battery_percent" -lt 80 ]; then
                 notify-send -i $bchar "Battery currently charging." "AC Power plugged and Battery Level: ${battery_percent}% "
            fi
        fi	
        if ! on_ac_power; then
            if [ "$battery_percent" -gt 80 ]; then
                 notify-send -i $b_full "On battery." "Battery Level: ${battery_percent}% "
            fi
            if [[ "$battery_percent" -lt 80 && "$battery_percent" -gt 20 ]]; then
                 notify-send -i $b_disc "On battery." "Battery Level: ${battery_percent}% "
            fi 
            if [ "$battery_percent" -lt 20 ]; then
                 notify-send -i $b_empt "Battery low." "Please plug AC Power and Battery Level: ${battery_percent}% "
            fi 
        fi;
    }
    
    #Main script
    #Initialize
    if on_ac_power; then powerstat=true; else powerstat=false; fi
    interval=6000
    export DISPLAY=:0.0
    #get battery pourcentage from acpi
    battery_percent=$(acpi -b | grep -P -o '[0-9]+(?=%)')
    
    #Main Loop
    while true
        do	
    	init_time=0
    	init_stat=$powerstat
            # Loop while time since last notification less than interval and power status did not change	
    	while [[ "$init_time" -lt $interval ]] && [[ $init_stat = $powerstat ]]
    		do
    		if on_ac_power; then powerstat=true; else powerstat=false; fi	
    		init_time=$init_time+1
    		sleep 0.1
    	done
            # Out of the loop, so we send notification
    	display_notification
    	sleep 3
    	killall notify-osd
    done
    
    
    

    • hg8

      Hello Frank! That is a great script! Thanks very much for sharing it ๐Ÿ™‚ Those icons looks really nice, where did you got them ?

    • Hari Kiran Vusirikala

      were should i create “Documents/battery_icon/”, in “/bin” or in “/home/myname/”, plz help me, thanks for don’t such a great job.