How do I set battery full alert tone in ubuntu 18.04 to save battery wear out?

My battery has gone faulty for the third time now. Now I want to set an alarm when my battery charge reaches 90%, so that I can disconnect it.

Can someone help me in setting that up?


Probably not the best way, but you can do something like the following.

  1. First you'll need acpi. Install it by running

    sudo apt install acpi
    
  2. Next you need to create a bash script. Create an empty text file, say battery-full.sh and add the following lines

    #!/bin/bash
    while true
        do
            export DISPLAY=:0.0
            battery_level=`acpi -b | grep -P -o '[0-9]+(?=%)'`
            if on_ac_power; then                                #check if AC is plugged in
                if [ $battery_level -ge 90 ]; then              #check if the battery level is over 90%
                    notify-send -u critical "Please unplug your AC adapter" "Battery level: ${battery_level}% (charged above 90%)" -i battery-full-charged
                 fi
            fi
          sleep 300                                             #wait for 300 seconds before checking again
    
        done
    
  3. Make the script executable and run it. You'll get a persistent notification if the battery is charging and the level is over 90%.

You can also get a sound alert by adding a suitable a audio-playing command after the notify-send command in the script above. For example you can you the play command from the sox package (for other options, see this). First install it by running

sudo apt install sox

Then modify the notify-send line in the script as follows

notify-send -u critical "Please unplug your AC adapter" "Battery level: ${battery_level}% (charged above 90%)" -i battery-full-charged; play /path/to/audio-file

(Replace /path/to/audio-file by a valid path to an actual audio file present in your system).

You may also consider adding the script to your startup applications so that it starts automatically every time you boot your laptop.


You know, I don't like to install extra programs and utilities on my laptop. so I think about a solution without acpi. inspired of @pomsky response bellow scripts maybe a better solution.

For Gnome:

#!/bin/bash
while true
    do 
        export DISPLAY=:0.0
        battery_level=`cat /sys/class/power_supply/BAT0/capacity`
        battery_status=`cat /sys/class/power_supply/BAT0/status`
        if [ $battery_status="Charging" ]; then
            if [ $battery_level -ge 85 ]; then
                notify-send -u critical "Battery fully charged"
            fi
        fi
        sleep 300
    done

For KDE:

#!/bin/bash
while true
    do 
        export DISPLAY=:0.0
        battery_level=`cat /sys/class/power_supply/BAT0/capacity`
        battery_status=`cat /sys/class/power_supply/BAT0/status`
        if [ $battery_status="Charging" ]; then
            if [ $battery_level -ge 85 ]; then
                kdialog --msgbox "Battery fully charged" 5
            fi
        fi
        sleep 300
    done

as you can see only difference is in notification systems of KDE and Gnome . we use /sys/class/power_supply/BAT0/ facilities instead of acpi. make script executable: chmod +x SCRIPTNAME.sh then add it to startup.

hope this help.