5

It is really helpful that there is a low battery warning, which can be customised. But there is no warning when the battery is full. If left unattended, it will cause battery wear. Is there any method to get a warning when the battery charge has reached 95% or 100%?

I am using 22.04.

user227495
  • 4,089
  • 17
  • 56
  • 101
  • Actually AFAIK battery wear/life is affected by how many full charges and full discharges it performs (full power cycles) and keeping your battery fully charged all the time thus preventing/limiting fully cycling it, should extend its life. – Raffa Jun 04 '22 at 04:28
  • A notification is, however, possible ... please see: https://askubuntu.com/q/1157608 – Raffa Jun 04 '22 at 04:56

1 Answers1

5

There are many ways of doing that ... here is one that is tested on Ubuntu 22.04 using Upower:

  • Identify your battery's device reference(name) with upower -e like so:

    upower -e | grep -i "batt"
    
  • Get battery information by using upower -i with the reference(name) of your battery like so:

    upower -i /org/freedesktop/UPower/devices/battery_BAT0
    
  • Refine the upower -i output to get only state like so:

    awk '/state:/ {print $2}' <(upower -i /org/freedesktop/UPower/devices/battery_BAT0)
    
  • Refine the upower -i output to get only percentage without "%" like so:

    awk '/percentage:/ {print 0+$2}' <(upower -i /org/freedesktop/UPower/devices/battery_BAT0)
    
  • Use the above knowledge in a script that checks every five minutes to notify you when your battery charge reaches or exceeds 95% while it is in the charging state like so:

#!/bin/bash

while true; do state="$(awk '/state:/ {print $2}' <(upower -i /org/freedesktop/UPower/devices/battery_BAT0))" percentage="$(awk '/percentage:/ {print 0+$2}' <(upower -i /org/freedesktop/UPower/devices/battery_BAT0))" [ "$state" == "charging" ] && [ "$percentage" -ge "95" ] && notify-send -u critical "Battery $percentage% full. Please unplug your AC adapter" sleep 300 done

Raffa
  • 32,237