A lightweight Python script to monitor laptop battery levels and send desktop notifications to maintain battery health by keeping the charge between 40% and 80%. Designed for Windows 11, tested on an MSI Modern 15 B7M.
- Monitors battery level every minute using the
psutillibrary. - Sends notifications:
- Every 5 minutes when the battery is ≤40% and unplugged, prompting to plug in the charger.
- Once when the battery is ≥80% and plugged in, prompting to unplug the charger.
- Stops gracefully with the
Esckey, using thekeyboardlibrary. - Runs in the background with minimal CPU/memory usage.
- Displays startup and shutdown notifications for user feedback.
- Handles errors (e.g., inaccessible battery data) with informative notifications.
- Operating System: Windows 11 (other Windows versions may work but are untested).
- Python: Version 3.8 or higher.
- Python Libraries:
psutil: For battery monitoring.plyer: For cross-platform desktop notifications.keyboard: For detecting theEsckey to stop the script.
-
Install Python:
- Download and install Python 3.8+ from python.org.
- Ensure Python is added to your PATH during installation.
-
Install Required Libraries: Open a Command Prompt and run:
pip install psutil plyer keyboard
-
Download the Script:
- Clone or download this repository to your local machine.
- Save
BatteryReminder.pyto a desired location (e.g.,<path_to_script>/BatteryReminder.py).
-
Run the Script:
- Open a Command Prompt and navigate to the script’s directory:
cd <path_to_script>
- Run the script:
Replace
<path_to_python>/python.exe BatteryReminder.py
<path_to_python>with the path to your Python executable (e.g.,C:\Python39). - Alternatively, create a shortcut (see below) for easier access.
- Open a Command Prompt and navigate to the script’s directory:
-
Stop the Script:
- Press the
Esckey to stop the script. A “Battery Reminder Stopped” notification will appear. - If running in a terminal, you can also press
Ctrl + Cor close the terminal window.
- Press the
-
Silent Execution (Optional):
- Rename
BatteryReminder.pytoBatteryReminder.pyw. - Run with
pythonw.exeto avoid a terminal window:<path_to_python>/pythonw.exe <path_to_script>/BatteryReminder.pyw
- Stop with the
Esckey or end thepythonw.exeprocess in Task Manager.
- Rename
-
Create a Shortcut (Optional):
- Right-click
BatteryReminder.py, select Create shortcut. - Edit the shortcut’s Properties:
- Target:
<path_to_python>/python.exe "<path_to_script>/BatteryReminder.py" - Start in:
<path_to_script> - Set Run to Minimized (optional).
- Target:
- For silent execution, use
BatteryReminder.pywandpythonw.exein the Target. - Double-click the shortcut to run.
- Right-click
-
Run on Startup (Optional):
- Copy the shortcut to the Startup folder:
- Press
Win + R, typeshell:startup, and press Enter. - Move the shortcut to this folder (e.g.,
C:\Users\<YourUsername>\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup).
- Press
- The script will start automatically on login.
- Copy the shortcut to the Startup folder:
- Battery Monitoring: Uses
psutil.sensors_battery()to check battery percentage and charging status every 60 seconds. - Notifications: Uses
plyer.notificationto display Windows desktop notifications:- Low Battery: When ≤40% and unplugged, notifies every 5 minutes.
- High Battery: When ≥80% and plugged in, notifies once.
- Errors: Notifies if battery data is unavailable or errors occur.
- Termination: Listens for the
Esckey usingkeyboard.wait('esc')to stop the script cleanly via athreading.Event. - Threading: Runs battery monitoring in a separate thread to allow simultaneous key listening.
- No Notifications:
- Ensure notifications are enabled: Windows Settings > System > Notifications > Python.
- Verify libraries:
pip show psutil plyer keyboard.
- Esc Key Doesn’t Work:
- Run the shortcut as administrator: Properties > Compatibility > Run as administrator.
- Ensure
keyboardis installed.
- Script Errors:
- Run in a terminal to see errors:
<path_to_python>/python.exe <path_to_script>/BatteryReminder.py
- Check for battery driver issues if “Battery Reminder Error” notifications appear.
- Run in a terminal to see errors:
- Multiple Instances:
- End
python.exeorpythonw.exeprocesses in Task Manager if duplicate notifications occur.
- End
This project is licensed under the MIT License. See the LICENSE file for details.
Contributions are welcome! Please submit a pull request or open an issue on GitHub.
- Jasmin Sultana Shimu (GitHub: [Shimu-I])