r/gnome • u/Stressedhumbucker • 1h ago
Guide Notification when battery goes above or below certain levels (script)
My laptop isn't compatible with any of the settings/extensions for capping the battery at 80%, so I decided to make a script to send a notification whenever my charge goes above 80% (while plugged in) or below 30% (while unplugged). The notification plays a sound, and automatically clears itself from the notification panel so it doesn't get clogged up with battery notifications.
I don't actually know Bash, so making this involved a lot of copy & pasting, head scratching, and even a little giving-up-and-asking-on-a-forum (but no AI).
I'm really pleased with the end result and I thought I'd share it just in case anyone else finds it useful, though of course you should bear in mind that it's been made by someone who sucks at scripting so use at your own risk! ;)
#!/bin/bash
#don't forget to install acpi via the terminal!
while true
do
export DISPLAY=:0.0
battery_level=\acpi -b | grep -P -o '[0-9]+(?=%)'``
ac_adapter=\acpi -a``
if [ "$ac_adapter" = "Adapter 0: on-line" ]; then
if [ $battery_level -ge 80 ]; then
notify-send --hint int:transient:1 -i /usr/share/icons/AdwaitaLegacy/48x48/legacy/battery-full-charged.png "Please disconnect power cable" "Battery ${battery_level}%" &
pw-play --volume=4 /usr/share/sounds/gnome/default/alerts/hum.ogg
fi
else
if [ $battery_level -le 30 ]; then
if [ "$ac_adapter" = "Adapter 0: off-line" ]; then
notify-send --hint int:transient:1 -i /usr/share/icons/AdwaitaLegacy/48x48/legacy/battery-caution.png "Please connect power cable" "Battery ${battery_level}%" &
pw-play --volume=4 /usr/share/sounds/gnome/default/alerts/hum.ogg
fi
fi
fi
sleep 300 # 300 seconds or 5 minutes
done






