🐍 The Python SDK — app6.3
Developer CourseModule 6: The Python SDKapp6.3
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.py

What This Script Does

  1. Connects to the API (once)
  2. Reads battery data every 30 seconds
  3. Prints SOC, power, voltage, frequency
  4. Warns if SOC drops below 20%
  5. Handles errors gracefully (retries after 10s)
  6. 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.