Reading12 min
Building a Battery Monitor in 20 Lines of Python
🐍 Module 6: The Python SDK
Building a Battery Monitor in 20 Lines of Python
Here is a complete battery monitoring script. 20 lines. Production-ready pattern.
The Script
Create battery_monitor.py:
import os, time
from energyatit import EnergyAtItClient
client = EnergyAtItClient(api_key=os.environ["EAT_KEY"])
print("Battery Monitor — Ctrl+C to stop")
print("-" * 50)
while True:
try:
readings = client.grid.get_meter_readings(grid_connection_id=1)
data = readings.get("readings", readings.get("data", {}))
soc = data.get("soc_pct", "N/A")
power = data.get("activePower_kW", "N/A")
voltage = data.get("voltage_V", "N/A")
freq = data.get("frequency_Hz", "N/A")
ts = data.get("timestamp", "unknown")
print(f"[{ts}] SOC: {soc}% | Power: {power} kW | Voltage: {voltage} V | Freq: {freq} Hz")
if isinstance(soc, (int, float)) and soc < 20:
print(" WARNING: Battery SOC below 20%!")
time.sleep(30)
except KeyboardInterrupt:
print("\nMonitor stopped.")
break
except Exception as e:
print(f" Error: {e}")
time.sleep(10)Run It
export EAT_KEY="eat_test_YOUR_KEY_HERE"
python3 battery_monitor.pyWhat This Script Does
- Connects to the API (once)
- Reads battery data every 30 seconds
- Prints SOC, power, voltage, frequency
- Warns if SOC drops below 20%
- Handles errors gracefully (retries after 10s)
- Stops cleanly on Ctrl+C
This is the pattern for every monitoring app: connect, read, display, alert, repeat.
Log in to track your progress and earn certifications.