Save this file by named battery_status.sh in your favorite location (mine is Home directory) and add this line in /etc/rc.local file (in my example, just replace your battery_status.sh location by /home/kasiya/):

sh /home/kasiya/battery_status.sh &

That's all. Reboot and see the magic.

If you don't have instell acpi, just install that using sudo apt-get install acpi

One BONUS

If you want to run make this script responsible with AC adapter,you don't need an extra variable to check that run for one time. If your AC adapter is plugged and battery charged above 60% it makes a alert to "Unplug your adapter!" until you don't unplug that. If alert tell you

just unplug AC adapter then you will see the message alert don't appear again until your battery charge down to 40%. Then another message alert and tell you

if you don't unplug AC adapter on above 60% or don't plug AC adapter below 40%, the alert message shown every 5 minutes (you can adjust that for yourself in code, see sleep [seconds]) show and Will remind you.

You cannot write a tutorial to create one script. That would be similar to a general python tutorial, which you can find on many places on the web.

What I did is write a small script, with comments to explain what I did, to check battery charge every 10 seconds. That might be overdone. Change the line time.sleep(10) to set the interval (in seconds). The commands can be changed of course, I made it send a notification as it is.

I made it run the message, or whatever command you give it, only run once per incident, so if charge exceeds 80% for example, the command only runs once, until the next time it exceeds defined values.

How to use

The script is meant to run in the background; copy it into an empty file, save it as check_battery.py make it executable and make it run on login: Dash > Startup Applications > Add, add the command:

/path/to/check_battery.py

The script

#!/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 60% 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 'charged over 80'%"
command_below = "notify-send 'charged below 80%'"
times = 0
while True:
charge = int(read_status())
if charge > 60:
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
else:
times = 0
time.sleep(10)
take_action()