Skip to content

Instantly share code, notes, and snippets.

@hahanein
Last active July 21, 2024 11:11
Show Gist options
  • Save hahanein/a5daeb22a4e5e74da55c3b4a1aa0d85d to your computer and use it in GitHub Desktop.
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
[Unit]
Description=Sway battery alarm
[Service]
ExecStart="/usr/bin/dash" "/usr/bin/swaybat"
#!/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
[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