5

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

5 Answers 5

6

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.

7
  • +1 It's clean and simple which I like!
    – phagento
    Commented Sep 20, 2021 at 8:16
  • 1
    @saravana linuxquestions
    – Reganto
    Commented Dec 14, 2021 at 16:15
  • 1
    It doesn't detect if the battery is already full charged. In my case, I can handle it by checking if battery_status is "Unknown". Commented Feb 5, 2022 at 15:31
  • 1
    @Reganto This seems to work on my system.
    – MDeBusk
    Commented Jun 30, 2022 at 16:32
  • 1
    Thanks @MDeBusk, It works flawlessly
    – Reganto
    Commented Jul 1, 2022 at 13:00
4

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.

4
  • can I get an alert tone using this? Commented Jul 11, 2019 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
    Commented Jul 11, 2019 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 :/ Commented Jul 11, 2019 at 23:48
  • @pomsky thank you so much, worked perfectly Commented Jul 12, 2019 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.

0

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

1
  • Not a good answer when you are needing to reference other information that may not be there later. If you need info from that answer to make your answer work you need to include it in your answer not just a reference to it.
    – David DE
    Commented Apr 11, 2022 at 8:57
0

Building on @Regantos answer for KDE:

  • Fixing small syntax errors like missing space in line 9 and removing redundant nested if statements.
  • Now make script executable using sudo chmod +x /path-to-script/
  • Add script as login script in background services in kubuntu which makes it run automatically after you login

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
1

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .