#!/usr/bin/env python3 # -*- coding: utf-8 -*- # Powerwerte Chalet aus Tasmota ESP-WROOM im Uponor-Schrank abrufen # ask_Chalet_S0_Uponor.py, jede Minute im Cronjob import requests from influxdb import InfluxDBClient # ============================================================ # Konfiguration Tasmota S0-Zaehler TASMOTA_URL = "http://172.16.0.198/cm?cmnd=Status%2010" # TASMOTA_URL = "http://energie-airbnb.local/cm?cmnd=Status%2010" HTTP_TIMEOUT = 5 INFLUX_HOST = "127.0.0.1" INFLUX_PORT = 8086 INFLUX_USER = "vz" INFLUX_PASSWORD = "naja" INFLUX_DATABASE = "strom" INFLUX_MEASUREMENT = "chaletmessung" # das json vom esp32 holen def lese_tasmota_s0(): response = requests.get( TASMOTA_URL, timeout=HTTP_TIMEOUT ) response.raise_for_status() data = response.json() try: sensor = data["StatusSNS"] garten = sensor["Gartenstudio"] heizung = sensor["Pelletheizung"] werte = { # Leistung bereits in Watt "gartenstudio_power": float(garten["Power"]), # Tasmota liefert Wh -> fuer Influx wie bisher in kWh umrechnen "gartenstudio_total": float(garten["Total_Wh"]) / 1000.0, "gartenstudio_today": float(garten["Today_Wh"]) / 1000.0, "gartenstudio_yesterday": float(garten["Yesterday_Wh"]) / 1000.0, "chalet_hzg_power": float(heizung["Power"]), "chalet_hzg_total": float(heizung["Total_Wh"]) / 1000.0, "chalet_hzg_today": float(heizung["Today_Wh"]) / 1000.0, "chalet_hzg_yesterday": float(heizung["Yesterday_Wh"]) / 1000.0 } return werte except KeyError as exc: raise RuntimeError( f"Erwarteter Wert fehlt in Tasmota-Antwort: {exc}" ) from exc # alle 8 werte in strom Datenbank schreiben def schreibe_influx(client, werte): punkt = [ { "measurement": INFLUX_MEASUREMENT, "fields": { "gartenstudio_power": werte["gartenstudio_power"], "gartenstudio_total": werte["gartenstudio_total"], "gartenstudio_today": werte["gartenstudio_today"], "gartenstudio_yesterday": werte["gartenstudio_yesterday"], "chalet_hzg_power": werte["chalet_hzg_power"], "chalet_hzg_total": werte["chalet_hzg_total"], "chalet_hzg_today": werte["chalet_hzg_today"], "chalet_hzg_yesterday": werte["chalet_hzg_yesterday"] } } ] erfolg = client.write_points(punkt) if not erfolg: raise RuntimeError( "InfluxDB write_points() war nicht erfolgreich" ) def main(): try: werte = lese_tasmota_s0() print("Verbindung zu Tasmota OK") print( "Gartenstudio: " f"{werte['gartenstudio_power']:.0f} W, " f"Heute {werte['gartenstudio_today']:.3f} kWh, " f"Gestern {werte['gartenstudio_yesterday']:.3f} kWh, " f"Gesamt {werte['gartenstudio_total']:.3f} kWh" ) print( "Pelletheizung: " f"{werte['chalet_hzg_power']:.0f} W, " f"Heute {werte['chalet_hzg_today']:.3f} kWh, " f"Gestern {werte['chalet_hzg_yesterday']:.3f} kWh, " f"Gesamt {werte['chalet_hzg_total']:.3f} kWh" ) # Verbindung zur InfluxDB V1.6 client = InfluxDBClient( INFLUX_HOST, INFLUX_PORT, INFLUX_USER, INFLUX_PASSWORD, INFLUX_DATABASE ) schreibe_influx(client, werte) client.close() print("InfluxDB: Werte geschrieben") except requests.exceptions.Timeout: print("FEHLER: Timeout beim Tasmota") except requests.exceptions.ConnectionError: print("FEHLER: Tasmota nicht erreichbar") except requests.exceptions.HTTPError as exc: print(f"FEHLER: Tasmota HTTP-Fehler: {exc}") except requests.exceptions.RequestException as exc: print(f"FEHLER bei Tasmota-Abfrage: {exc}") except Exception as exc: print(f"FEHLER: {exc}") if __name__ == "__main__": main()