Script de Python para consultar corriente/voltaje/potencia/consumo de Shelly Pro1PM

El siguiente script consulta un dispositivo Shelly Pro1PM para obtener sus estadísticas de corriente, voltaje, potencia y consumo de energía. Usa la biblioteca httpx para hacer peticiones HTTP GET a la API del dispositivo Shelly.

shelly_query.py
import httpx

# Dirección IP del dispositivo Shelly
device_ip = "192.168.33.1"
base_url = f"http://{device_ip}"

# Endpoint para parámetros de potencia (asume dispositivo Gen2 con API RPC)
power_url = f"{base_url}/rpc/Switch.GetStatus?id=0"

try:
    # Enviar petición GET al endpoint de potencia
    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}")

Ejemplo de salida

shelly_output.txt
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

Echa un vistazo a artículos similares por categoría: Python, Embedded