I got my laptop two days ago. It’s a Compaq Presario CQ42-220TU. Battery backup is around 3 hours. I am using Linux Mint which is a variation of Ubuntu. Just a few minutes ago I wrote a Python script that monitors the battery charge and if it’s discharging, it gives a notification with remaining percentage of charge every 5 minutes.
Here’s the source codes for the curious minds π
Download:http://code.google.com/p/masnun/downloads/detail?name=battery.py
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
#!/usr/bin/env python import commands import pynotify from threading import Timer def battery_check(): rem = float(commands.getoutput("grep \"^remaining capacity\" /proc/acpi/battery/BAT0/state | awk '{ print $3 }'")) full = float(commands.getoutput("grep \"^last full capacity\" /proc/acpi/battery/BAT0/info | awk '{ print $4 }'")) state = commands.getoutput("grep \"^charging state\" /proc/acpi/battery/BAT0/state | awk '{ print $3 }'") percentage = int((rem/full) * 100) if state == "discharging": pynotify.init("Battery Alert!") notification = pynotify.Notification("Battery "+state,str(percentage)+"%","/usr/share/icons/gnome/32x32/status/battery-low.png") notification.show() timer = Timer(300.0,battery_check) timer.start() if __name__ == "__main__": battery_check() |
For convenience, I added the script to my start up applications. So whenever I log in to my lappy, it gets auto started. If you want to use this script as well, save the above source code in a file. Let’s assume you saved it in a file named “battery.py” in your home folder. Now give it execution permission. Right click on the file. Go to properties. In the “Permissions” tab, tick the option saying “Allow executing file as program”. Close the dialog box.
Now go to “Preferences” menu from Sytem menu (Ubuntu) or Mint Menu (Linux Mint). Go to Startup Applications. Click Add. Type in a name and comment. In the command field, type the full path of the file. Eg. /home/<_username_>/battery.py.
Restart your PC. π
If you can’t get it working, please provide feedback here or catch me on IM. My contact details are available on https://masnun.com.
13 replies on “Python Script to Monitor Laptop Battery Charge (Ubuntu/Linux Mint)”
Wow..Script is cool. Just uses system acpi to get values.. Liked it.. π Simple easy but useful. Can be integrated into other app. Keep up masnun.
Clean & efficient.Good job man:)
For the help please use http://www.google.com
hellow bro, i m looking forward to buy the same laptop u have. can you please tell me whether buying it would be a good deal or not. or if you are facing any problem with this laptop, please let me know. thank you.
I am having no issues. It’s cool π
Man you save my day!!
I need to check low battery state because I’m using XMonad since a few weeks and XMobar shows battery status but doesn’t notify low battery level, so I made a few changes to your original script. I’ll send it to you, feel free to change it, use it or to distributed it.
Yes, I recieved it. Thanks π
Thank you for the script! I modified it to show me alert to unplug my laptop from the AC adaptor when the battery is full.
Great! π
Hey there,
Thanks a lot man.. Your script is very useful indeed. Similar to what Eliezer Faizal did, i too modified it to alert me whenever charge is full and unplug my cable π But my laptop is not ACPI compatible and it does not contains battery state in /proc system instead i used /sys system and get it done. If there are anyone similar who cannot find battery info from /proc, tel me i could send you the code which uses /sys
hello,
um using the same notebook, and facing the same issue with battery backup time. Since um nt as tech savvy as u ppl, can any1 suggest me any software for my windows pc which will help me to solve this problem.
Hi
i was really in search of such a code, i just have a small question .. i have a seprate python script running , and i want to add a flag value in that script which will take value=1 when the laptop battery is less then 10% Β can you give me some hints how can i do that while utilizing this script.. im using ubuntu aswell
Can you tell me how can I use this script in windows?