Battery Alerts

Battery Alerts with Dunst

July 16 2014

So I always struggle with what I want, need, think I need, and so on. Don't want a status bar, so put by battery info in my prompt. Now I don't want anything in my prompt, so I need something to tell me the current battery level. I have aliases so I can type batt, but I still always forget, then my battery dies. That happened again this morning. Was on IRC with alad and wux. I said there had to be a way to check current battery level, and if it was at [x] level, display through dunst. I remember something I read in z3bra's blog, about a script set to run at intervals and piping through bar. So I used that for inspiration, and took it a few steps further (or back, depending on your point of view)

~/bin/battery6.html
#!/bin/sh
#
## dkeg 2014
## Notifications using dunst for battery alerts

## You're battery config may be different
BATT=/sys/class/power_supply/BAT0
CAPACITY=$(cat $BATT/capacity)
STATE=$(cat $BATT/status)

NOTIF='notify-send'
MSG='Battery level at'
URGENT='Yo Fucker! Plug in or Die!'

## Capture states
STATUSF='Full'
STATUSC='Charging'
STATUSD='Discharging'

case ${STATE} in
    'Full')
        STATE=$STATUSF
        ;;
    'Charging')
        STATE=$STATUSC
        ;;
    'Discharging')
        STATE=$STATUSD
        ;;
esac

## Only show notifications if Discharging
## Only show once battery level below 70
if [ ${STATE} = ${STATUSD} ] ; then
case ${CAPACITY} in
    [0-9]
        ${NOTIF} -u critical "${URGENT}" "${CAPACITY}"%
        ;;
    [1-3]*)
        ${NOTIF} -u critical "${MSG}" "${CAPACITY}"%
        ;;
    [4-6]*)
        ${NOTIF} -u normal "${MSG}" "${CAPACITY}"%
        ;;
esac
fi

Then place the following in .xinitrc

while :; do /path/to/script ; sleep 300; done &

As a caveat, this script was created for personal use. If it doesn't work for you, may just need to put some effort into it so it does.


LINUX

LINKS