Custom laptop battery notifications

Since i bought my laptop, i was concernead of the long-term effects of keeping it plugged in constantly. A lot of people do this and apparently, it shortens the battery life. I wanted an app or a script that could show me the battery notifications that i wanted – notify me when the battery is charged or discharged at a custom level. I think it’s optimal to keep it charged until 90% and not let it discharge below 20%. Since i didn’t find anything that fitted my needs i decided to make my own script.

Seeing battery information from the terminal

There is a nice linux app called acpi that can tell you about your battery from the terminal. It’s called acpi. It wasn’t installed by default in my Debian so i’ve installed quickly:

apt-get install acpi

Now run it, to see the output.

acpi -V

acpi-output

Now let’s get only the charge value from the acpi output, with some bash-foo:

acpi -V | head -n 1 | awk ‘{print ($4)}’ | sed ‘s/..$//’

Sending notifications

There is a small linux app called notify-send, which can generate small notifications on your desktop. For Ubuntu users, it’s that notification in the upper-right corner of the screen. I didn’t had it installed on Debian so i had to install the libnotify-bin package

apt-get install libnotify-bin

Scripting stuff up

I made this little script that checks if the battery status is under 21% or over 90%.

#!/bin/bash

BAT=$(acpi -V | head -n 1 | awk ‘{print ($4)}’ | sed ‘s/..$//’)

if [ $BAT -lt ’21’  ]
then
notify-send -u “critical” -t 89000 “Battery running low, plug in AC adapter”
fi

if [ $BAT -gt ’90’  ]
then
notify-send -u “critical” -t 89000 “Battery is charged, unplug AC adapter”
fi

And i’ve made a cronjob that runs every 10 minutes. I ran

crontab -e

And added the path to the script that will run every 10 minutes.

10 * * * * /path/to/script

Other ways of doing it

It may look a bit overkill, as there are other ways of doing it. I’m pretty sure there isn’t a “max-charge” value in any power-management utilities. But there is always a low and a critical value. To edit these values in Debian, you can do it via the terminal or using dconf-editor. It should be installed by default but if not you can install the dconf-tools package

apt-get install dconf-tools

Open dconf-editor and go to org->gnome->settings-daemon->plugins->power.

dconf-power-settings

Resources

  • karim

    cool stuff. I like it

  • Zhong

    The string in the dconf-tool editor doesn’t exist in the current Gnome build, do you have an alternative?

    • Not quite, as I haven’t used it in a while. Maybe it’s possible to access the key-values on the cli using gsettings or dconf.