PDA

View Full Version : [KDE] battery-status monitor (of a sort)


albright
2008-01-21, 21:48
I believe that the following script will run
perpetually, checking the battery status
every five minutes. It will pop up a message
when the battery level falls below 10%.


#!/bin/sh
while true
do
sleep 300
X=`battery-status | awk '{print $4}'`
if [ "$X" \< "90" ]
then
kdialog --title "Battery Status" --msgbox ". . . . . . . . . $X . . . . . . . . ."
fi
done


Some options in kdialog don't seem to work quite right but
the above works for me.

Of course, without the while loop, sleep etc the X=
and kdialog lines can form a script to get a popup
of the battery level whenever you want. But in that
case, this is better:


#!/bin/sh
X=`battery-status | awk '{print $4}'`
kdialog --geometry +15+450 --title "Battery Status" --passivepopup $X 4


geometry option does not work with msgbox - don't know why (but
that explains all the "dots").

You could put the first script in ~/.kde/Autostart

It does not seem to use any cpu that I can notice. One problem is
that the "busy cursor" bounces or blinks for the full timeout. I
wonder if there is a way to disable launch notification for particular
apps ...

Oh, and by the way, if you want to get rid of the script you'll
have to kill it from command line.

albright
2008-01-21, 21:50
oops, can't edit these posts but of course the this line

[CODE][if [ "$X" \< "90" ]/CODE]

should be

[CODE][if [ "$X" \< "10" ]/CODE]

PinCushionQueen
2008-01-22, 14:32
Thanks a bunch albright!! I've tried out your "on demand" battery % indicator and it works great! I've switched to using it instead of battery-status mainly because I really like the unobtrusive little bubble. I've even set up a hotkey shortcut (Ctrl+B) to enable me to be able to check battery % with a couple of button presses while using any other app.

I haven't had a chance to test the <10% battery warning script yet - but should be able to get that low today. I think I might like it to warn me at 15 or 20% though - no rational reason really, I just prefer more warning. Can't I just replace the <"10" with <"15" and have it work?

Thanks again! :D

penguinbait
2008-01-22, 16:10
A script is provided in new KDE version to do this, it has a shortcut you can run at will on taskbar, and a background script that runs evry 10 minutes and notifys you when you are at 20%

NOTE you must have battery-status and python installed..

albright
2008-01-22, 16:17
Can't I just replace the <"10" with <"15" and have it work?

Yes, certainly any value you want.

I don't know how PB has
implemented the battery monitor in the new kde package
but hopefully it is user configurable. Well, he just has
to tell us where the script is I guess :)

Plus you can add other commands to the script. For example,
this line (inserted just before the kdialog command):

play-sound /usr/local/kde/share/sounds/KDE_Notify.wav


causes a little beep to go with the popup ...

It seems that perpetual scripts like these can stand in for
the missing cron demon (pending more knowledge about
how many resources they use up - it does not seem to
be noticeable but ...)

albright
2008-01-22, 18:24
There's a problem with my script when the battery
gets below 10%. The string value e.g. "9.9%" is regarded
as *greater than* "10" (cause of the first character).

So, I suggest using the length of the battery-status
output. If it is less than 5 characters long then that
is the range less than 10% (since battery-status always
adds one decimal place).

Here's the new script


#!/bin/sh
while true
do
sleep 600
X=`battery-status | awk '{print $4}'`
if [ ${#X} -lt 5 ] && [ "$X" != "Charging" ]
then
play-sound /usr/local/kde/share/sounds/KDE_Notify.wav
kdialog --title "Battery Status" --msgbox ". . . . . . . . . $X . . . . . . . . ."
fi
done


I wonder what bugs are in this version :)

albright
2008-01-23, 12:00
Here's another problem (not with the script though).

If the battery is fully charged then the battery-status
app hangs when run and returns no value and does not
exit.

A workaround that would avoid having instances of
battery-status accumulate might be to add something
like


killall battery-status


after the sleep 600 line ...

albright
2008-01-27, 18:23
looks like the latest battery-status fixes the hang if
fully charged problem (though it now always reports
"charging" even when the battery is full). Thanks to
the author.

If you have flite installed, the tablet can tell you the
battery level, add lines like these in the appopriate
place:

X=`battery-status | awk '{print $4}'`
flite -t "battery status $X" &

P.S. it's tricky to do this in the tablet since the keyboard
lacks the " ` " symbol and the " ' " symbol (soft keyboard
has the latter)

albright
2008-01-28, 16:33
I was wrong - the apostrophe is on the n810 hard
keyboard (on the key with question mark; looked to
me like a comma :) )

I respectfully changed PenguinBait's .xmap file to
include the left quote thus:

keycode 48 = apostrophe quoteleft question question question question

helps with scripting ...