You are not logged in.

#1 2008-07-07 23:54:42

fwojciec
Member
Registered: 2007-05-20
Posts: 1,411

simple critical battery level warning script

This is a little script I wrote for use on my laptop.  I wanted to have some kind of visual cue to let me know when the battery charge gets below a certain level and this is what I came up with:

#!/bin/bash

# Configuration
interval=120    #in seconds
critical_level=10    #percent 
icon="/usr/share/icons/Tango/48x48/devices/battery.png" #notification icon
battery_id="BAT1"   #ACPI battery identifier
ac_adapter_id="ADP1"    #ACPI power adapter identifier

while true
do
    if [ "$(cat /proc/acpi/ac_adapter/$ac_adapter_id/state | grep -o off)" == "off" ]; then
        battery_max=`cat /proc/acpi/battery/$battery_id/info | head -3 | tail -1 | awk '{print $4}'`
        battery_current=`cat /proc/acpi/battery/$battery_id/state | head -5 | tail -1 | awk '{print $3}'`
        battery_level=$((100*$battery_current/$battery_max))
        [ $battery_level -le $critical_level ] && \
        notify-send -u critical -i "$icon" -t 15000 \
        "Battery level is low!" "Only $battery_level% of the charge remains."
    fi
    sleep $interval
done

This script uses notification-daemon (or notification-daemon-xfce) package.  It can be configured by editing values in the "Configuration" section.  I just autostart it with my openbox session.  It all should be pretty self-explanatory otherwise. 

I hope someone finds it useful.

EDIT: Some improvements to the script.

Last edited by fwojciec (2008-07-09 13:11:14)

Offline

#2 2008-07-09 07:34:57

robmaloy
Member
From: Germany
Registered: 2008-05-14
Posts: 263

Re: simple critical battery level warning script

thx, really cool i always forget to look after my battery percentage big_smile


maybe you can replace "BAT1" and "ADP1" with variables, so configuration would be easier


☃ Snowman ☃

Offline

#3 2008-07-09 10:59:52

moljac024
Member
From: Serbia
Registered: 2008-01-29
Posts: 2,676

Re: simple critical battery level warning script

laptop-mode tools has an option of running commands when battery charge reaches a certain level. You could just run notify-send from there.


The day Microsoft makes a product that doesn't suck, is the day they make a vacuum cleaner.
--------------------------------------------------------------------------------------------------------------
But if they tell you that I've lost my mind, maybe it's not gone just a little hard to find...

Offline

#4 2008-07-09 12:24:27

fwojciec
Member
Registered: 2007-05-20
Posts: 1,411

Re: simple critical battery level warning script

robmaloy wrote:

thx, really cool i always forget to look after my battery percentage big_smile


maybe you can replace "BAT1" and "ADP1" with variables, so configuration would be easier

Thanks smile  I made changes according to your suggestion.

moljac024 wrote:

laptop-mode tools has an option of running commands when battery charge reaches a certain level. You could just run notify-send from there.

You mean in the auto-hibernate section?  I use it as it was supposed to be used -- for hibernating -- and I think you can only set one command that's executed according to a specified battery level in laptop-mode-tools.  Plus it would be sort of complicated, since displaying the notification depends on two conditions in this script -- specified battery level and power cord being disconnected.

Offline

#5 2008-07-09 12:53:54

fwojciec
Member
Registered: 2007-05-20
Posts: 1,411

Re: simple critical battery level warning script

Here's an alternative version of the script -- if anyone prefers it.  It is slightly more minimalistic, but also depends on acpi package.

#!/bin/bash

# Configuration
interval=120    #in seconds
critical_level=10    #percent
icon="/usr/share/icons/Tango/48x48/devices/battery.png"     #notification icon

while true
do
    if [ "$(acpi -a | grep -o off)" == "off" ]; then
        battery_level=`acpi -b | sed 's/.*[dg], //g;s/\%,.*//g'`
        [ $battery_level -le $critical_level ] && \
        notify-send -u critical -i "$icon" -t 15000 \
        "Battery level is low!" "Only $battery_level% of the charge remains."
    fi
    sleep $interval
done

Last edited by fwojciec (2008-07-09 13:11:38)

Offline

#6 2009-04-10 07:09:37

fwojciec
Member
Registered: 2007-05-20
Posts: 1,411

Re: simple critical battery level warning script

Thanks for the correction smile  It was one of the very first scripts I wrote so it's badly written, I admit.  Your version is much better -- thanks for sharing.

Offline

Board footer

Powered by FluxBB