Creating Battery Low and Hight Notifier with Python - Aman Aadi

Creating Battery Low and Hight Notifier with Python

Introduction:

Have you ever found yourself caught off guard by your device’s battery draining unexpectedly or missing the chance to plug it in before it reaches critical levels? Keeping track of our device’s battery levels is crucial to ensure uninterrupted productivity and prevent sudden shutdowns. In this blog post, we will embark on a Python project that allows you to create a Battery Low and High Notifier. With this handy script, you can receive timely notifications when your device’s battery level is low or fully charged, helping you manage your power efficiently and avoid disruptions caused by sudden battery depletion.

Understanding the Python Battery Low and High Notifier:

The Battery Notifier is a Python script that monitor your device’s battery level continuously. When the battery reaches user-defined low or high thresholds, the script sends a notification to alert you to take appropriate action.

How it works? It will run a command “WMIC PATH Win32_Battery Get EstimatedChargeRemaining” to catch the current battery percentage. On the delay of 20 seconds it will check whether the battery is less than 30 or more than 80. It shows a warning if the battery reaches that point.

Requirements:

To get started, ensure you have Python installed on your computer. You can download the latest version from the official Python website (https://www.python.org/downloads/) and follow the installation instructions. Additionally, a code editor such as Visual Studio Code or PyCharm, will make coding and testing more convenient.

In Python, we will use the subprocess module to run the cmd command, tkinter module to show warning messages and time module for delay. You can install a python module using “pip install <module_name>” command.

Python Code:

# Importing modules
import subprocess
from tkinter import messagebox  # install this module using "pip install tk-tools"
import time


# Creating a function to get the current battery lavel by catching the result of a command "WMIC PATH Win32_Battery Get EstimatedChargeRemaining".
def getBattery():
    battery_result = subprocess.check_output(["WMIC", "PATH", "Win32_Battery", "Get", "EstimatedChargeRemaining"])
    battery_percentage = battery_result.decode("utf-8").split("\n")
    return int(battery_percentage[1].strip())


# On delay of 20 seconds we will run getBattery() function and also check whether battery is less than 30 or more than 80. It shows a warning if the battery reaches that point.
is_runnning = True
while is_runnning:
    time.sleep(20)
    battery = getBattery()

    if battery < 30:
        messagebox.showwarning("Battery Low &#x203c;", "Plugin charger &#x203c;")
        is_runnning = False
    elif battery > 80:
        messagebox.showwarning("Battery Charged &#x203c;", "Unplug your charger &#x203c;")
        is_runnning = False

Output:

Creating Battery Low and Hight Notifier with Python - Aman Aadi
Creating Battery Low and Hight Notifier with Python - Aman Aadi

Conclusion:

Congratulations! You’ve successfully built a Battery Low and High Notifier with Python. With this simple yet powerful tool, you can now stay informed about your device’s battery levels and take timely actions to ensure uninterrupted productivity and optimize battery health. Whether you’re working on your laptop or using a desktop device, this Battery Notifier will keep you in charge and ready for any task at hand. Happy coding, and stay powered up!

Discover more from Aman Aadi

Subscribe now to keep reading and get access to the full archive.

Continue reading

Scroll to Top