My battery has gone faulty for the third time now. Now I want to set an alarm when my battery charge reaches 90%, so that I can disconnect it.
Can someone help me in setting that up?
I don't like to install extra programs and utilities on my laptop, so I am thinking about a solution without acpi
. Inspired by @pomsky's response, the below scripts may be a better solution.
Update: It does detect if the battery is already fully charged thanks to @mdebusk and @blaze-fire(krishan singh)
For Gnome:
#!/bin/bash
while true
do
export DISPLAY=:0.0
battery_level=`cat /sys/class/power_supply/BAT0/capacity`
battery_status=`cat /sys/class/power_supply/BAT0/status`
if [ $battery_status = "Charging" ] && [ $battery_level -ge 85 ];
then
notify-send -u critical "Battery fully charged"
fi
sleep 120
done
For KDE:
#!/bin/bash
while true
do
export DISPLAY=:0.0
battery_level=`cat /sys/class/power_supply/BAT0/capacity`
battery_status=`cat /sys/class/power_supply/BAT0/status`
if [ $battery_status = "Charging" ] && [ $battery_level -ge 85 ];
then
kdialog --msgbox "Battery fully charged" 5
fi
sleep 120
done
as you can see, the only difference is in the notification systems of KDE and Gnome . We use /sys/class/power_supply/BAT0/
facilities instead of acpi
. make the script executable: chmod +x SCRIPTNAME.sh
, then add it to startup.
hope this help.
Probably not the best way, but you can do something like the following.
First you'll need acpi
. Install it by running
sudo apt install acpi
Next you need to create a bash script. Create an empty text file, say battery-full.sh
and add the following lines
#!/bin/bash
while true
do
export DISPLAY=:0.0
battery_level=`acpi -b | grep -P -o '[0-9]+(?=%)'`
if on_ac_power; then #check if AC is plugged in
if [ $battery_level -ge 90 ]; then #check if the battery level is over 90%
notify-send -u critical "Please unplug your AC adapter" "Battery level: ${battery_level}% (charged above 90%)" -i battery-full-charged
fi
fi
sleep 300 #wait for 300 seconds before checking again
done
You can also get a sound alert by adding a suitable a audio-playing command after the notify-send
command in the script above. For example you can you the play
command from the sox
package (for other options, see this). First install it by running
sudo apt install sox
Then modify the notify-send
line in the script as follows
notify-send -u critical "Please unplug your AC adapter" "Battery level: ${battery_level}% (charged above 90%)" -i battery-full-charged; play /path/to/audio-file
(Replace /path/to/audio-file
by a valid path to an actual audio file present in your system).
You may also consider adding the script to your startup applications so that it starts automatically every time you boot your laptop.
notify-send
command in the script. Please see the update to the answer (after point 3).
Another option is to set the battery charge limiter to say 60% to 80% if you aren't planning on taking it on the road. However for my laptop at least I can only change it in Windows as this answer details:
The reason why your battery would stay upto 70% and not charge further could be due to an option called "Desktop Mode" which might be enabled. Desktop mode helps in disabling the battery to charge upto 100% to maximize the life of the battery. This option can also be disabled. To disable this option:
- Right-click the battery icon on the Windows notification area, and then click Dell Extended Battery life Options
- The Battery Meter dialog box is displayed, Click the Desktop Mode tab
- Select the option to disable this feature
- Then, click OK
I dual boot Ubuntu and Windows 10 so it's not a big deal to change the battery charge limiter. I don't know of any apps to do this in Linux. It would be a nifty little C program to write for my inaugural journey into the Linux Kernel coding though.
Building off of @pornsky's answer, the format of the acpi output wasn't working with @pornsky's code, so I modified it a little bit. The following script got the job done for me.
#!/bin/bash
while true
do
on_ac=`acpi | grep -o 'Charging'`
battery_level=`acpi -b| grep -P -o '\d+(?=%)'| sed -n 2p`
if [[ "$on_ac" == "Charging" ]]; then #check if AC is plugged in
if [ $battery_level -ge 90 ]; then #check if the battery level is over 90%
notify-send -u critical "Please unplug your AC adapter" "Battery level: ${battery_level}% (charged above 90%)" -i battery-full-charged
fi
fi
sleep 300 #wait for 300 seconds before checking again
done
Check out his answer for more depth
Building on @Regantos answer for KDE:
sudo chmod +x /path-to-script/
While copying script beware of the intended spaces as they will affect the output.
Note: In order to play audio you will have to install Sox
#!/bin/bash
while true
do
export DISPLAY=:0.0
battery_level=`cat /sys/class/power_supply/BAT1/capacity`
battery_status=`cat /sys/class/power_supply/BAT1/status`
if [ $battery_status = "Charging" ] && [ $battery_level -ge 85 ];
then
play /path-to-mp3-file; // to play alert sound
kdialog --msgbox "Battery fully charged";
fi
sleep 120 // cooldown time
done