Finding Live Battery Usage Using Python and Displaying Results in JSON

Ravi Tiwari
2 min readJul 2, 2023

--

Python’s versatility and wide range of libraries allow us to accomplish a variety of tasks, from simple calculations to system-level operations. One of these tasks is monitoring live battery usage. In this article, we’ll use the psutil library to accomplish this and we will display our results in JSON format.

Before we get started, make sure Python is installed on your system. If it’s not, visit the Python official website to download and install the latest version.

Once Python is installed, you can install the psutil library. This package will provide the functions needed to access system information, including battery usage data. You can install psutil using pip, which is Python's package installer. Run the following command in your terminal:

pip install psutil

With psutil installed, you can now write a script to monitor battery usage. We'll use the psutil and time libraries, and also the json library to format our output.

import psutil
import time
import json

The psutil library provides a method named sensors_battery(), which returns system battery information such as the percentage of battery remaining, the time left for the battery to run out, and whether the battery is currently being charged.

We can fetch and display this information in a loop, to continually monitor the battery status. We’ll add the information to a dictionary, and then convert that dictionary to a JSON string for output.

In this script, we fetch the battery status every minute, store the information in a dictionary, convert the dictionary to a JSON string using json.dumps(), and print it out.


while True:
battery = psutil.sensors_battery()
battery_data = {}

battery_data["battery_percentage"] = battery.percent
battery_data["power_plugged_in"] = battery.power_plugged

# battery.secsleft provides the approximate number of seconds left before the battery runs out
if battery.secsleft == psutil.POWER_TIME_UNKNOWN:
battery_data["battery_time_left"] = None
elif battery.secsleft == psutil.POWER_TIME_UNLIMITED:
battery_data["battery_time_left"] = "Battery is currently being charged."
else:
# converting seconds to hours, minutes, and seconds
hours_left = battery.secsleft // 3600
minutes_left = (battery.secsleft % 3600) // 60
seconds_left = (battery.secsleft % 60)
battery_data["battery_time_left"] = {"hours": hours_left, "minutes": minutes_left, "seconds": seconds_left}

# Convert dictionary to JSON and print
print(json.dumps(battery_data, indent=4))

# Sleep for a minute before fetching the information again
time.sleep(60)

This is a simple yet powerful application of Python’s ability to interact with system-level information and represents a good starting point for developing more advanced system-monitoring tools.

Source Code — https://github.com/rtiwariops/CodeHub/tree/main/live-battery-usage-python

--

--

Ravi Tiwari
Ravi Tiwari

Written by Ravi Tiwari

Experienced hands-on CTO with 20+ years of cloud native microservices expertise, driving solutions for large-scale enterprises.

No responses yet