1

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?

1

Probably not the best way, but you can do something like the following.

  1. First you'll need acpi. Install it by running

    sudo apt install acpi
    
  2. 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
    
  3. Make the script executable and run it. You'll get a persistent notification if the battery is charging and the level is over 90%.

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.

| improve this answer | |
  • can I get an alert tone using this? – Kethan Chauhan Jul 11 '19 at 23:14
  • @Kethan Yes, you can append a proper audio-playing command after the notify-send command in the script. Please see the update to the answer (after point 3). – pomsky Jul 11 '19 at 23:38
  • Cool script I read similar ones just a few minutes ago. The neatest one though is where the charger is plugged into a WiFi controlled wall outlet pass through switch that disconnected/reconnected power to the charger power brick. Only problem is I never use my laptop on battery and I want to buy a new laptop every 2 years on average. So for me it's a mute point if the battery lasts 5 years or 10 years :/ – WinEunuuchs2Unix Jul 11 '19 at 23:48
  • @pomsky thank you so much, worked perfectly – Kethan Chauhan Jul 12 '19 at 0:25
0

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.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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