Battery Indicator Python Script
Tags: python
Published: 2020-02-29 14:37:08

A laptop user you must take caution about your battery because the battery is also the most important component
If you are using an old laptop then you might be a wonder to protect your battery from getting overcharged as well wanted to protect from getting drain at 0%
Even if you have a new and very high spec laptop still you need to take caution or protect your battery to get overcharged.
So, in this case, you might use any battery percentage indicator application on our laptop to save your battery life.
and what if I tell you!! You can do the same things with the help of a simple python script.
as it is a simple python script so it won't use your laptop many resources it just uses a little bit of resource of your laptop
so even you have a very low spec laptop with you then you can simply use this script to give battery percentage alert.
as this script is written in python so that you can run this on any OS as it is platform-independent language. so you no need to worry about compatibility.

Script Introduction
so basically in this python script, I simply monitor the battery percentage and also look for sockets is get a plug or not based on that you will get notified and notified in the sense of you will hear the voice which will warn you or about your battery percentage as well as on your screen notification will be appear in right side corner so that it will draw your attention. 
In order to use this script you simply need to copy below code and save it as whatever name you want but make it extension as .pyw
by making extension .pyw so it can run in the background and also run this script continuously in the background simply create it a shortcut and paste it into the startup folder so whenever your computer is restarted this program will run in background and base on your battery status it will warn you.

```
import psutil
import time
import pyttsx3
from win10toast import ToastNotifier # also need to install win32api
import threading

toaster = ToastNotifier()
x=pyttsx3.init()
x.setProperty('rate',130)
x.setProperty('volume',8)
count = 0

def show_notification(show_text):
   toaster.show_toast(show_text,
                       icon_path='battery_indicator.ico',
                       duration=10)
   # loop the toaster over some period of time
   while toaster.notification_active():
      time.sleep(0.1)

def monitor():
   while (True):
      time.sleep(10)
      battery = psutil.sensors_battery()
      plugged = battery.power_plugged
      percent = int(battery.percent)
      
      if percent < 40:
         if plugged == False:
            processThread = threading.Thread(target=show_notification, args=("Your Battery at "+str(percent)+"% Please plug the cable",))  # <- note extra ','
            processThread.start()
            x.say("Your battery is getting low so charge it right now")
            x.runAndWait()
            count = 0
      elif percent == 100:
         if plugged == True:
            processThread = threading.Thread(target=show_notification, args=("Charging is getting complete",))  # <- note extra ','
            processThread.start()
            x.say("Charging is getting complete")
            x.runAndWait()
      elif percent == 90:
         if plugged == True:
            if count == 0:
               processThread = threading.Thread(target=show_notification, args=("Your Battery at 90% Please plug out the cable",))  # <- note extra ','
               processThread.start()
               x.say("Your battery at 90% ")
               x.runAndWait()
               count = count + 1

if __name__ == "__main__":
   monitor()

   
   

```

script link:- https://gist.github.com/ddc6faf2ece824930fda1ed74b78fea4.git

so far now you very well know how we are going to use this script. and if you still wanted to know how each line of python code works. then you can watch the below video for more details.

{% youtube oRzGTuXKINQ %}