battery-monitor 932 B

123456789101112131415161718192021222324252627282930313233343536
  1. #!/usr/bin/env bash
  2. # Designed to be run by systemd timer every 30 seconds and alerts if battery is low
  3. BATTERY_THRESHOLD=10
  4. NOTIFICATION_FLAG="/run/user/$UID/omarchy_battery_notified"
  5. get_battery_percentage() {
  6. upower -i "$(upower -e | grep 'BAT')" |
  7. awk -F: '/percentage/ {
  8. gsub(/[%[:space:]]/, "", $2);
  9. val=$2;
  10. printf("%d\n", (val+0.5))
  11. exit
  12. }'
  13. }
  14. get_battery_state() {
  15. upower -i $(upower -e | grep 'BAT') | grep -E "state" | awk '{print $2}'
  16. }
  17. send_notification() {
  18. notify-send -u critical "󱐋 Time to recharge!" "Battery is down to ${1}%" -i battery-caution -t 30000
  19. }
  20. BATTERY_LEVEL=$(get_battery_percentage)
  21. BATTERY_STATE=$(get_battery_state)
  22. if [[ "$BATTERY_STATE" == "discharging" && "$BATTERY_LEVEL" -le "$BATTERY_THRESHOLD" ]]; then
  23. if [[ ! -f "$NOTIFICATION_FLAG" ]]; then
  24. send_notification "$BATTERY_LEVEL"
  25. touch "$NOTIFICATION_FLAG"
  26. fi
  27. else
  28. rm -f "$NOTIFICATION_FLAG"
  29. fi