diff options
| author | srdusr <trevorgray@srdusr.com> | 2025-09-24 05:45:26 +0200 |
|---|---|---|
| committer | srdusr <trevorgray@srdusr.com> | 2025-09-24 05:45:26 +0200 |
| commit | d96a422496fe3a6b80659a720e63c6abc41b7de9 (patch) | |
| tree | 20f90abb5ce91875a40027333efbb52877902132 /common/scripts/sys/battery_alert.sh | |
| parent | 521215add7da7fef6722c7b5adec839b84d72db0 (diff) | |
| parent | a1627ac743289e768b138f1a60753a62e0869cc4 (diff) | |
| download | dotfiles-d96a422496fe3a6b80659a720e63c6abc41b7de9.tar.gz dotfiles-d96a422496fe3a6b80659a720e63c6abc41b7de9.zip | |
Add 'common/scripts/' from commit 'a1627ac743289e768b138f1a60753a62e0869cc4'
git-subtree-dir: common/scripts
git-subtree-mainline: 521215add7da7fef6722c7b5adec839b84d72db0
git-subtree-split: a1627ac743289e768b138f1a60753a62e0869cc4
Diffstat (limited to 'common/scripts/sys/battery_alert.sh')
| -rwxr-xr-x | common/scripts/sys/battery_alert.sh | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/common/scripts/sys/battery_alert.sh b/common/scripts/sys/battery_alert.sh new file mode 100755 index 0000000..47fa556 --- /dev/null +++ b/common/scripts/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 |
