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