Simple Python Script for Ubuntu/Mint to Notify You When to Plug-In and Plug-Out the Laptop Charger

Linux Mint 17.x series has a problem. Unlike MATE desktop, current Cinnamon doesn’t have an option to let you know that your laptop battery level is critically low. You can change the gsettings to modify the notifications, but that didn’t work for me above 15% power level. So I went with this simple script. I want to be notified when the laptop power comes down to 40% or less so that I can plug-in the power adaptor. I also want to be notified when the laptop charges up to 80% or more. Then I will unplug the charger. I heard this improves the battery life to keep the battery charge level between 40-80%. Here’s the simple python script made by Jacob Vlijm

#!/usr/bin/env python3

import subprocess
import time

def read_status():
    This function reads the output of your command, finds the line with
    'percentage' (line 17, where first line = 0) and reads the figure
    command = "upower -i $(upower -e | grep BAT) | grep --color=never -E percentage|xargs|cut -d' ' -f2|sed s/%//"
    get_batterydata = subprocess.Popen(["/bin/bash", "-c", command], stdout=subprocess.PIPE)
    return get_batterydata.communicate()[0].decode("utf-8").replace("\n", "")

def take_action():
    When the charge is over 80% or below 40%, I assume the action does
    not have to be repeated every 10 seconds. As it is, it only runs
    1 time if charge exceeds the values. Then only if it exceeds the
    limit again.
    # the two commands to run if charged over 80% or below 60%
    command_above = "notify-send 'Battery is charging above 80%. Please unplug the AC adapter'"
    command_below = "notify-send 'Battery is lower than 40%. Please plug your AC adapter'"
    times = 0
    while True:
        charge = int(read_status())
        if charge > 80:
            if times == 0:
                subprocess.Popen(["/bin/bash", "-c", command_above])
                times = 1
        elif charge < 40:
            if times == 0:
                subprocess.Popen(["/bin/bash", "-c", command_below])
                times = 1
            times = 0

  • Copy this script into an empty file and save it as .py file e.g.
  • Make it executable. You can do that easily by going to the file Properties > Permissions > Allow executing file as program.
  • Make it run on login. You can do that by opening the Startup Applications. Now click on Add, give it any name and add the path of the file in the Command field. For example, I’ve saved the file in my Home folder. So the path becomes, /home/username/

That’s it. Now you may need to log out and log in back to make this script work. Even better, reboot your system. After that, you’ll be notified every time your laptop’s battery level comes down to 40% or, goes up to 80%. You can change or modify the script to suit your taste.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s