summaryrefslogtreecommitdiff
path: root/scripts/battery-monitor
blob: 32a18dd6baf4c1d605fe488a46bedef8c5f0f3d7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#!/usr/bin/env bash

# Device path from 'upower -e'
BATTERY_PATH="/org/freedesktop/UPower/devices/DisplayDevice"

# Get initial battery percentage
get_battery_percentage() {
    busctl get-property org.freedesktop.UPower "$BATTERY_PATH" org.freedesktop.UPower.Device Percentage |
        awk '{print int($2)}'
    }

while true; do
    PERCENT=$(get_battery_percentage)

    if [[ $PERCENT -le 10 ]]; then
        notify-send -u critical "Battery Low" "Battery at ${PERCENT}%" -i battery-caution -t 30000
        # Wait 5 minutes before rechecking to avoid spam
        sleep 300
    else
        # Check every minute otherwise
        sleep 60
    fi
done