Python script to query Shelly Pro1PM current/voltage/power/consumption

The following script queries a Shelly Pro1PM device for its current, voltage, power, and energy consumption statistics. It uses the httpx library to make HTTP GET requests to the Shelly device’s API.

import httpx

# Shelly device IP address
device_ip = "192.168.33.1"
base_url = f"http://{device_ip}"

# Endpoint for power parameters (assumes Gen2 device with RPC API)
power_url = f"{base_url}/rpc/Switch.GetStatus?id=0"

try:
    # Send GET request to the power endpoint
    response = httpx.get(power_url, timeout=5.0)
    response.raise_for_status()

    data = response.json()

    print("Shelly Pro 1PM Power Parameters:")
    print(f"  Output On: {data.get('output')}")
    print(f"  Power: {data.get('apower')} W")
    print(f"  Voltage: {data.get('voltage')} V")
    print(f"  Current: {data.get('current')} A")
    print(f"  Power Factor: {data.get('pf')}")
    print(f"  Frequency: {data.get('freq')} Hz")
    print(f"  Energy: {data.get('aenergy', {}).get('total', 0)} Wh")

except httpx.RequestError as e:
    print(f"Request error: {e}")
except httpx.HTTPStatusError as e:
    print(f"HTTP error: {e.response.status_code} - {e.response.text}")
except Exception as e:
    print(f"Unexpected error: {e}")

Example output

Shelly Pro 1PM Power Parameters:
  Output On: True
  Power: -36.8 W
  Voltage: 234.3 V
  Current: 0.927 A
  Power Factor: 0.7
  Frequency: 50.0 Hz
  Energy: 14169.316 Wh