automatic notifications through dunst

Submitted scripts and programs
Forum rules
Your own work only.
User avatar
dkeg
Configurator
Posts: 3782
Joined: Sun Nov 18, 2012 9:23 pm
Location: Mid-Atlantic Grill

automatic notifications through dunst

Unread post by dkeg » Tue Jul 15, 2014 10:59 pm

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])
Last edited by dkeg on Sun Jul 27, 2014 8:23 pm, edited 7 times in total.
Reason: fixed a typo in the title ;)

Work hard; Complain less

User avatar
dkeg
Configurator
Posts: 3782
Joined: Sun Nov 18, 2012 9:23 pm
Location: Mid-Atlantic Grill

Re: automatic notifications thought dunst

Unread post by dkeg » Tue Jul 15, 2014 11:53 pm

hmm, how can I make sure I only get notifications if its discharging. Let me check.

Work hard; Complain less

machinebacon
Baconator
Posts: 10253
Joined: Thu Sep 16, 2010 11:03 am
Location: Pfälzerwald
Contact:

Re: automatic notifications thought dunst

Unread post by machinebacon » Wed Jul 16, 2014 3:21 am

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.
..gnutella..

User avatar
dkeg
Configurator
Posts: 3782
Joined: Sun Nov 18, 2012 9:23 pm
Location: Mid-Atlantic Grill

Re: automatic notifications thought dunst

Unread post by dkeg » Wed Jul 16, 2014 3:52 am

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!

Work hard; Complain less

machinebacon
Baconator
Posts: 10253
Joined: Thu Sep 16, 2010 11:03 am
Location: Pfälzerwald
Contact:

Re: automatic notifications thought dunst

Unread post by machinebacon » Wed Jul 16, 2014 4:09 am

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.
..gnutella..

User avatar
dkeg
Configurator
Posts: 3782
Joined: Sun Nov 18, 2012 9:23 pm
Location: Mid-Atlantic Grill

Re: automatic notifications through dunst

Unread post by dkeg » Wed Jul 16, 2014 5:16 pm

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

Work hard; Complain less

User avatar
dkeg
Configurator
Posts: 3782
Joined: Sun Nov 18, 2012 9:23 pm
Location: Mid-Atlantic Grill

Re: automatic notifications through dunst

Unread post by dkeg » Sun Aug 03, 2014 3:05 am

mimiced the script to use bar instead of dunst to cut away dbus and dunst, you know, all about options and a cleaner pstree

Work hard; Complain less

User avatar
wuxmedia
Grasshopper
Posts: 6445
Joined: Wed Oct 17, 2012 11:32 am
Location: Back in Blighty
Contact:

Re: automatic notifications through dunst

Unread post by wuxmedia » Tue Mar 03, 2015 7:28 pm

Bit late to this party :)
Just implemented this, works a treat.
"Seek, and Ye shall find"
"Github | Chooons | Site"

User avatar
wuxmedia
Grasshopper
Posts: 6445
Joined: Wed Oct 17, 2012 11:32 am
Location: Back in Blighty
Contact:

Re: automatic notifications through dunst

Unread post by wuxmedia » Fri Apr 28, 2017 9:41 am

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
"Seek, and Ye shall find"
"Github | Chooons | Site"

User avatar
stark
MILF
Posts: 521
Joined: Sat Sep 27, 2014 6:38 pm
Location: Arpanet
Contact:

Re: automatic notifications through dunst

Unread post by stark » Tue May 09, 2017 8:07 am

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]
If you can do it go ahead and do it, if you can't do it then don't even criticize it. - gingerdesu

User avatar
wuxmedia
Grasshopper
Posts: 6445
Joined: Wed Oct 17, 2012 11:32 am
Location: Back in Blighty
Contact:

Re: automatic notifications through dunst

Unread post by wuxmedia » Tue May 09, 2017 10:08 am

^ 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.
"Seek, and Ye shall find"
"Github | Chooons | Site"

User avatar
stark
MILF
Posts: 521
Joined: Sat Sep 27, 2014 6:38 pm
Location: Arpanet
Contact:

Re: automatic notifications through dunst

Unread post by stark » Tue May 09, 2017 1:54 pm

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
If you can do it go ahead and do it, if you can't do it then don't even criticize it. - gingerdesu

User avatar
z3bra
Window Manager
Posts: 53
Joined: Fri Jun 27, 2014 8:34 am
Location: France
Contact:

Re: automatic notifications through dunst

Unread post by z3bra » Wed Jun 14, 2017 10:53 pm

It's been a while since I last played with it, but I remember that the best trick to auto choose the battery was:

Code: Select all

BAT=/sys/class/power_supply/BAT*
Shell expension has never failed me here, even with dash as /bin/sh. And most people only have a single battery anyway ;)
BANGARANG, MOTHERFUCKER.

User avatar
wuxmedia
Grasshopper
Posts: 6445
Joined: Wed Oct 17, 2012 11:32 am
Location: Back in Blighty
Contact:

Re: automatic notifications through dunst

Unread post by wuxmedia » Thu Jun 15, 2017 12:00 pm

nice a few lines less. slightly obfuscated, only slightly
"Seek, and Ye shall find"
"Github | Chooons | Site"

Post Reply