Battery-alert-inProgrammer

Battery alert! | Python

In this article, we will discuss how to create a Battery Alert in python.

Introduction:

As 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 drained 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 your 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.

Outline:

In this project, we won’t take any input. We just write our python script in any text editor or IDE and run it. We can define our own battery alert notification at our desired battery percentage level.

Project Prerequisites:

You will have to install some packages in your system to run this script and work successfully.

psutil: psutil (process and system utilities) is a cross-platform library for retrieving information on running processes and system utilization (CPU, memory, disks, network, sensors) in Python. It is useful mainly for system monitoringprofiling, limiting process resources, and management of running processes. psutil currently supports the following platforms:

  • Linux
  • Windows
  • macOS
  • FreeBSD, OpenBSDNetBSD
  • Sun Solaris
  • AIX
See also  Face Detector Using Python

Supported Python versions are 2.73.4+

for more information about this library refer to this – psutil · PyPI

py-notifier: Simple Python3 module for displaying desktop notifications on Windows, Linux, and macOS.

for more information about this library refer to this – py-notifier · PyPI

win10toast: An easy-to-use Python library for displaying Windows 10 Toast Notifications which is useful for Windows GUI development.

for more information about this library refer to this – win10toast · PyPI

That’s it…! We just don’t need to install any other package for this project.

Algorithm :

The first thing we need to do is to import the required packages and modules. In this project, we import psutil, pynotifier packages to make use of their functions.

import psutil
from pynotifier import Notification

After the import section, we need to define our variables like :
battery = psutil.sensors_battery()
which creates an object for the battery

plugged = battery.power_plugged
It gives us information on whether the charger is plugged or not.

percent = battery.percent
It provides us the battery percentage of our system.

battery = psutil.sensors_battery()
plugged = battery.power_plugged
percent = battery.percent

In the next section, we start implementing the main part of our project.
In this section, we check whether the percent (Percentage level of our battery) is below the minimum rate which we fix and if our system is plugged in with a charger or not.

if percent <= 30 and plugged!=True:
    Notification(
        title="Battery Low",
        description=str(percent) + "% Battery remain!!",
        duration=5,  # Duration in seconds
        
    ).send()

If the above condition is satisfied, we will send a notification to the OS that our battery percentage is low. Otherwise, we don’t do anything.

See also  Get Wifi Password Using Python

Source Code :

Here is the complete source code for the project.

import psutil
from pynotifier import Notification

battery = psutil.sensors_battery()
plugged = battery.power_plugged
percent = battery.percent

if percent <= 30 and plugged!=True:
    Notification(
        title="Battery Low",
        description=str(percent) + "% Battery remain!!",
        duration=5,  # Duration in seconds
        
    ).send()

As we have completed the project let’s check the output.

Output :

Here is the output of the project :

You can run the program using the sample command provided in the above example image.

Tada! Check the output now.

Congratulations! You have successfully created your own battery alert notification system using python.

Important Note :

Note that the above-given example output notification is generated at a condition of battery percentage level below 80% for the sample purpose. But you can play around with the script and set your own percentage level.

Thank You !

By UdaySK

Leave a Comment

Your email address will not be published.

Ads Blocker Image Powered by Code Help Pro
Ads Blocker Detected!!!

We have detected that you are using extensions to block ads. Please support us by disabling these ads blocker.

Refresh