Last active
July 21, 2024 11:11
-
-
Save hahanein/a5daeb22a4e5e74da55c3b4a1aa0d85d to your computer and use it in GitHub Desktop.
Present a battery alarm banner in sway and shutdown computer if battery is lower than 2 percent
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[Unit] | |
Description=Sway battery alarm | |
[Service] | |
ExecStart="/usr/bin/dash" "/usr/bin/swaybat" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/dash | |
read -r status < /sys/class/power_supply/BAT0/status | |
if [ "$status" = "Charging" ] | |
then | |
exit 0 | |
fi | |
read -r energy_full < /sys/class/power_supply/BAT0/energy_full | |
read -r energy_now < /sys/class/power_supply/BAT0/energy_now | |
charge=$(/usr/bin/dc -e "10 k 100 $energy_full / $energy_now * 0 k 1 / p") | |
if [ "$charge" -lt 2 ] | |
then | |
/usr/bin/systemctl poweroff | |
elif [ "$charge" -lt 11 ] | |
then | |
/usr/bin/swaynag -m "Battery low ($charge). Please charge now." | |
fi |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[Unit] | |
Description=Sway battery alarm | |
[Timer] | |
OnBootSec=1min | |
OnUnitActiveSec=1min | |
[Install] | |
WantedBy=timers.target |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment