Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Laptop Battery Percentage

by naildownx (Beadle)
on Aug 01, 2011 at 02:16 UTC ( #917741=CUFP: print w/replies, xml ) Need Help??

I needed a simple script to parse out the Battery Percentage from system call acpi for my conky display on my desktop. Keep it simple. :)
MODIFIED CODE:
*I left out the word 'Battery' intentionally because it is displayed in my conky by default and with another color. The script does what is supposed to do only, get the Percentage and return that only.
#!/usr/bin/perl print '',( `acpi` =~ /(\d+)%/ )[ 0 ] // '??', "%";
ORIGINAL CODE - Doesn't include last 0 for 100%. Thanks to jwkrahn for the simpler and more precise code ^^ above
#!/usr/bin/perl #battery.pl use strict; use warnings; use 5.012; my $battery_charge; $battery_charge = `acpi`; if($battery_charge =~ /\d{2}%/ ) { print "Battery: $&\n"; }
Silence is golden, but Duct Tape is silver x)

Replies are listed 'Best First'.
Re: Laptop Battery Percentage
by jwkrahn (Monsignor) on Aug 01, 2011 at 04:55 UTC

    If the charge is at 100% you are only picking up the last two zeros.

    Simple would be:

    #!/usr/bin/perl print 'Battery: ', ( `acpi` =~ /(\d+)%/ )[ 0 ] // '??';
      I will have to try that out when I get to my laptop tonight :) Thanks for the reply.
      Silence is golden, but Duct Tape is silver x)
      I actually liked this reply the best, and it works very good...going to EDIT it now. Thanks for the help jwkrahn
      U-U-D-D-L-R-L-R-A-B->START
Re: Laptop Battery Percentage
by zentara (Archbishop) on Aug 01, 2011 at 13:18 UTC
    I don't have the acpi program on my computer, but on linux, with the latest kernel, you can directly read the battery state from

    /proc/acpi/battery/BAT1/state

    which yields when reading

    present: yes capacity state: ok charging state: charged present rate: 65535 mA remaining capacity: 4261 mAh present voltage: 12209 mV

    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku ................... flash japh

      the state file is kinda static, as in it shows the current state, from which one cannot deduce the percentage.

      The info file is much more informative:

      $ cat /proc/acpi/battery/BAT0/info present: yes design capacity: 5144 mAh last full capacity: 5144 mAh battery technology: rechargeable design voltage: 10800 mV design capacity warning: 200 mAh design capacity low: 100 mAh cycle count: 0 capacity granularity 1: 100 mAh capacity granularity 2: 100 mAh model number: Primary serial number: 49233 2011/02/25 battery type: LIon OEM info: Hewlett-Packard

      Combining the two gets you to:

      $ cat /proc/acpi/battery/*/* | grep capacity: design capacity: 5144 mAh last full capacity: 5144 mAh remaining capacity: 5144 mAh $

      Or in perl:

      $ perl -MDP -e'while(<>){/^(.*)\s+capacity:\s*((\d+).*)/ and$i{$1}=[$2 +,$3]}print"$_: $i{$_}[0], "for keys%i;printf"%5.1f%%\n",100*$i{remain +ing}[1]/$i{design}[1]' /proc/acpi/battery/*/* remaining: 5144 mAh, design: 5144 mAh, last full: 5144 mAh, 100.0%

      Enjoy, Have FUN! H.Merijn

        I use this code in gnu screen's statusbar:

        backtick 1 1 1 sh -c 'perl -ne "/^(la|re|st)[^:]+:\s+(\S+)\s/and \$h{\ +$1}=\$2}{\$h{st}=~s/of.*/-/;\$h{st}=~s/on.*/+/;printfq(%.0f%%%s),\$h{ +re}*100/\$h{la},\$h{st}" /proc/acpi/battery/BAT0/* /proc/acpi/ac_adap +ter/ACAD/state'
        But what if you have two batteries? Some laptops allow that. You will have multiple lines.
        Information doesn't want to be free. It wants to be feline.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: CUFP [id://917741]
Front-paged by Arunbear
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (9)
As of 2018-06-25 14:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?



    Results (126 votes). Check out past polls.

    Notices?