diff options
| author | srdusr <trevorgray@srdusr.com> | 2025-09-24 05:01:20 +0200 |
|---|---|---|
| committer | srdusr <trevorgray@srdusr.com> | 2025-09-24 05:01:20 +0200 |
| commit | 553cb2204b0bf27afe13c6332f5679bbd47172a0 (patch) | |
| tree | 75c86ff018122a682e0afd7a0e2a0228a63e44bd /unix/sys/battery_alert.sh | |
| parent | b20e4e004be74884cc72c57a3128e36fd5177d7a (diff) | |
| download | dotfiles-553cb2204b0bf27afe13c6332f5679bbd47172a0.tar.gz dotfiles-553cb2204b0bf27afe13c6332f5679bbd47172a0.zip | |
Update/Overhaul
Diffstat (limited to 'unix/sys/battery_alert.sh')
| -rwxr-xr-x | unix/sys/battery_alert.sh | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/unix/sys/battery_alert.sh b/unix/sys/battery_alert.sh new file mode 100755 index 0000000..47fa556 --- /dev/null +++ b/unix/sys/battery_alert.sh @@ -0,0 +1,43 @@ +#!/bin/sh + +# Send a notification if the laptop battery is either low or is fully charged. + +# Battery percentage at which to notify +WARNING_LEVEL=78 +CRITICAL_LEVEL=5 +BATTERY_DISCHARGING=$(acpi -b | grep "Battery 0" | grep -c "Discharging") +BATTERY_LEVEL=$(acpi -b | grep "Battery 0" | grep -P -o '[0-9]+(?=%)') + +# Use files to store whether we've shown a notification or not (to prevent multiple notifications) +FULL_FILE=/tmp/batteryfull +EMPTY_FILE=/tmp/batteryempty +CRITICAL_FILE=/tmp/batterycritical + +# Reset notifications if the computer is charging/discharging +if [ "$BATTERY_DISCHARGING" -eq 1 ]; then + # Battery is discharging + if [ -f "$FULL_FILE" ]; then + command rm "$FULL_FILE" + fi + if [ "$BATTERY_LEVEL" -le "$WARNING_LEVEL" ] && [ ! -f "$EMPTY_FILE" ]; then + notify-send "Low Battery" "${BATTERY_LEVEL}% of battery remaining." -u critical -i "battery-low-symbolic" -r 9991 + touch "$EMPTY_FILE" + fi + if [ "$BATTERY_LEVEL" -le "$CRITICAL_LEVEL" ] && [ ! -f "$CRITICAL_FILE" ]; then + notify-send "Battery Critical" "The computer will shut down soon." -u critical -i "battery-caution-symbolic" -r 9991 + touch "$CRITICAL_FILE" + fi +elif [ "$BATTERY_DISCHARGING" -eq 0 ]; then + # Battery is charging + if [ "$BATTERY_LEVEL" -gt 99 ] && [ ! -f "$FULL_FILE" ]; then + notify-send "Battery Charged" "Battery is fully charged." -i "battery-full-symbolic" -r 9991 + touch "$FULL_FILE" + fi + # Reset empty/critical notifications when charging begins + if [ -f "$EMPTY_FILE" ]; then + command rm "$EMPTY_FILE" + fi + if [ -f "$CRITICAL_FILE" ]; then + command rm "$CRITICAL_FILE" + fi +fi |
