Page 1 of 1
automatic notifications through dunst
Posted: Tue Jul 15, 2014 10:59 pm
by dkeg
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)
Code: Select all
#!/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 in .xinitrc
Code: Select all
while :; do /path/to/script; sleep 300; done &
Now, obviously there are much better ways to do this. But so far it works for me and I'm happy. Hopefully, I can remember to plug in my laptop now before it dies.
May be useful for other things too.
Edit: repost of fixed version.
Still working on only displaying only if discharging -> Done
Edit July 27 - syntax error fixed from
0 [1-9]*) to [0-9])
Re: automatic notifications thought dunst
Posted: Tue Jul 15, 2014 11:53 pm
by dkeg
hmm, how can I make sure I only get notifications if its discharging. Let me check.
Re: automatic notifications thought dunst
Posted: Wed Jul 16, 2014 3:21 am
by machinebacon
Something like:
Code: Select all
if [ $(cat /sys/class/power_supply/BAT0/status) = "Discharging" ] ; then your_script ; fi
of course check the path to BAT, maybe it is BAT1 for you, or BATA
Edit:
here in full:
Code: Select all
#!/bin/sh
if [ $(cat /sys/class/power_supply/BAT0/status) = "Discharging" ]
then
BATC=$(cat /sys/class/power_supply/BAT0/capacity)
case ${BATC} in
0[1-9]*)
notify-send -u critical 'Yo Fucker! Plug in or Die!' ${BATC}
;;
[1-3]*)
notify-send -u critical 'Plug that shit in; level at' ${BATC}
;;
[4-7]*)
notify-send -u normal 'Find you charger; level is' ${BATC}
;;
[8-9]*)
notify-send -u low 'Battery is at' ${BATC}
;;
100)
notify-send -u low 'Full'
;;
esac
fi
One could make it more beautiful by using
BATTERY=/sys/class/power_supply/BAT0
STAT=$BATT/status
CAP=$BATT/capacity
so the user only needs to change BAT/BAT0/BAT1, but that's cosmetics.
Re: automatic notifications thought dunst
Posted: Wed Jul 16, 2014 3:52 am
by dkeg
Thank you! I was on that same path! Initially had some syntax errors, but got them sorted.
Edited first post to reflect newest version.
Edit: That's funny! I did exactly that. Guess we're on the same wave length!
Re: automatic notifications thought dunst
Posted: Wed Jul 16, 2014 4:09 am
by machinebacon
Yeah, there are at least two ways of doing it: check for "Discharging" or check for NOT "Discharging", of course the double loop is a bit rude, especially because it is run inside of a third loop, but who cares :) It does what it says on the tin.
Re: automatic notifications through dunst
Posted: Wed Jul 16, 2014 5:16 pm
by dkeg
updated first post with new version. Mainly just added comments. Also add a blog post for it, but its pretty much the same as whats here
Re: automatic notifications through dunst
Posted: Sun Aug 03, 2014 3:05 am
by dkeg
mimiced the script to use bar instead of dunst to cut away dbus and dunst, you know, all about options and a cleaner pstree
Re: automatic notifications through dunst
Posted: Tue Mar 03, 2015 7:28 pm
by wuxmedia
Bit late to this party :)
Just implemented this, works a treat.
Re: automatic notifications through dunst
Posted: Fri Apr 28, 2017 9:41 am
by wuxmedia
put this on again, still nice, added a loop, so it starts nice in i3, for my own ref really:
Code: Select all
#!/bin/sh
#
## dkeg 2014
## Notifications using dunst for battery alerts
while :
do
## auto select battery
if [ -d /sys/class/power_supply/BAT0/ ]
then
DIR=/sys/class/power_supply/BAT0/
else
DIR=/sys/class/power_supply/BAT1
fi
CAPACITY=$(cat $DIR/capacity)
STATE=$(cat $DIR/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}"%
;;
esac
fi
sleep 20s
done
Re: automatic notifications through dunst
Posted: Tue May 09, 2017 8:07 am
by stark
here's an excerpt from my script : )
Code: Select all
#!/bin/sh
DEV="$(find /sys/class/power_supply -iname 'bat*')"
case "$1" in
s | stat*)
printf 'Status: %s\n' "$(cat $DEV/status)"
;;
c | cap*)
printf 'Capacity: %s\n' "$(cat $DEV/capacity)"
;;
esac
my phone uses the directory 'battery' as opposed to the usual BAT[0-9]
Re: automatic notifications through dunst
Posted: Tue May 09, 2017 10:08 am
by wuxmedia
^ nice one.
was wondering what you put your alerts on at.
I got annoyed at every 5 mins after 70% so it's just on 10/% or lower now, I think it is anyway.
Re: automatic notifications through dunst
Posted: Tue May 09, 2017 1:54 pm
by stark
i run it every 25 mins but it only notifies or initiates something when charge is around 0-19%
Example:
Code: Select all
case "$CAP" in
[0-9] | 1[0-9])
_notify
;;
*)
date +"%H:%M Charge: $CAP"
;;
esac
Re: automatic notifications through dunst
Posted: Wed Jun 14, 2017 10:53 pm
by z3bra
It's been a while since I last played with it, but I remember that the best trick to auto choose the battery was:
Shell expension has never failed me here, even with dash as /bin/sh. And most people only have a single battery anyway ;)
Re: automatic notifications through dunst
Posted: Thu Jun 15, 2017 12:00 pm
by wuxmedia
nice a few lines less. slightly obfuscated, only slightly