My laptop battery didn't last half as long as I had hoped and it is an internal one. That is, it is very inconvenient to replace. I would like my replacement battery to last as long as possible, and after reading up on it, it looks like the best way to do that is to allow it to charge and discharge on a regular basis. So I looked for a program or app in the Ubuntu software center that could remind me to unplug or plug in and found nothing, and the default battery indicator is insufficient because it doesn't care about battery health.

So, is there any app that does this? If not, is there an API I can take advantage of so I can write my own?

Edit: A simple google search revealed I could look up battery information using the command upower -i /org/freedesktop/UPower/devices/battery_BAT0. It would be nice to have a status indicator do this for me (and possibly keep a tally of charge/discharge cycles).

share|improve this question
    
There is a battery status indicator in the top right of your screen. – psusi Apr 7 '16 at 2:06
    
I'm well aware of the battery status indicator. The problem is that that indicator fails to inform me of when, for example, the battery hits 80% and I should start discharging it, and when it hits about %40 and I should start charging it. It displays the percentage, but doesn't alert me in any way that would grab my attention. – Adam Apr 7 '16 at 15:48

You can try to use this script.

It raise a notification and play sound (with pulseaudio) when battery level reach selected value. It doesn't keep track of charge/discharge cycles, but with some edit you can add this feature.

#! /bin/bash

# read battery percentage value
OUT=`upower -i /org/freedesktop/UPower/devices/battery_BAT0 | grep percentage`

# select only the int value
IFS=':' read -ra P <<< "$OUT"
PERCENTAGE="%"
BATTERY_VALUE=${P[1]%$PERCENTAGE}

# send a notification and play sound if battery level is under 10%
if (( $BATTERY_VALUE  < "10")); then
  notify-send "Battery Low level! You need to plug your PC!"

# command to play sound - you can select your preferred sound if this doesn't work
  paplay /usr/share/sounds/freedesktop/stereo/complete.oga
fi

# send a notification and play sound if battery level is equal to 100%
if (( $BATTERY_VALUE  >= "100")); then
  notify-send "Battery charged! You can now unplug your PC!"
  paplay /usr/share/sounds/freedesktop/stereo/complete.oga
fi

Copy this script into a bash file and move it into local bin folder:

sudo mv <script_file> /usr/local/bin/

Where script_file is the name (or path) of your script. Then you can use cron daemon to run it every 5 minutes to check battery level. So edit cron:

crontab -e

Select preferred editor and add at the end of file this line:

*/5 * * * * /usr/local/bin/<script_file>

Change script_file with your script name.

Now it should work. Check it after a system reboot.

I hope to be useful.

share|improve this answer
    
Looks great, thanks ) – Slava Fomin II Apr 20 '17 at 1:34

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.