Skip to content

Instantly share code, notes, and snippets.

@lmaertin
Created February 15, 2018 16:10
Show Gist options
  • Save lmaertin/e17e5b437d2caafca217608fecb0b10d to your computer and use it in GitHub Desktop.
Save lmaertin/e17e5b437d2caafca217608fecb0b10d to your computer and use it in GitHub Desktop.
Homematic Sonoff Status Parser
!Sonoff Pow Parser (Entwurf)
!Schreibt Momentanleistung und Energiewerte in Systemvariablen
!Zeitstempel des letzten Durchgangs wird zusaetzlich geschrieben wenn das Script erfolgreich durchlaeuft.
!
!Das Script funktioniert nur wenn die Tasmota-Firmware auf dem SonOff Installiert ist. Steuern des SonOff
!als CuxD-Device, vgl. https://github.com/jp112sdl/SonoffHMLOX/wiki/Homematic (Danke an Jerome!)
!Anpassung an andere SonOff Geraete moeglich - entsprechend der Status-Nachrichten.
!
!Entwickelt auf Basis von http://homematic-forum.de/forum/viewtopic.php?f=31&t=17209
!Dank an Eugen (funkleuchtturm) fuer das JSON-Parserscript.
!Autor: Lukas Maertin
! Parameter im JSON-String:
! Beispiel: STATUS8 = {"StatusPWR":{"Total":0.520, "Yesterday":0.100, "Today":0.320, "Power":27, "Factor":0.94, "Voltage":234, "Current":0.122}}
! Message | Unit | Description
! ----------|------|-----------------------------------------------------
! Total | kWh | Total Energy usage including Today
! Yesterday | kWh | Total Energy usage between 00:00 and 24:00 yesterday
! Today | kWh | Total Energy usage today from 00:00 until now
! Period | Wh | Energy usage between previous message and now
! Power | W | Current power load
! Factor | | The ratio of the real power flowing to the load to the apparent power in the circuit
! Voltage | V | Current line voltage
! Current | A | Current line current
!Vorbereitung: Systemvariablen anlegen (Schreibweise muss identisch sein!)
!Pow_Aktualisierung Zeichenkette
!Pow_Status Wahrheitswert
!Pow_Momentanleistung Zahl W (Power)
!Pow_Energie_total Zahl kWh (Total)
!Pow_Energie_gestern Zahl kWh (Yesterday)
!Pow_Energie_heute Zahl kWh (Today)
!Variable setzen:
var hostname = "IP-Adresse"; !hostname oder IP des Sonoff Pow
!interne Variablen:
var url = "http://" + hostname + "/cm?cmnd=Status%208";
!hier ist die Abfrage mit CUxD
dom.GetObject("CUxD.CUX2801001:1.CMD_SETS").State("wget -q -O - '"#url#"'");
dom.GetObject("CUxD.CUX2801001:1.CMD_QUERY_RET").State(1);
string json = dom.GetObject("CUxD.CUX2801001:1.CMD_RETS").State();
!hier ist die Abfrage mit system.Exec
!string stdout;
!string stderr;
!system.Exec("wget -q -O - '"#url#"'", &stdout, &stderr);
!string json = stdout;
!WriteLine(json);
integer jsonLen = json.Length();
!Beim XML-File den ueberfluessigen Header entfernen
string wordStart = "Status\":";
integer wortPos = json.Find(wordStart) + wordStart.Length();
json = json.Substr(wortPos, jsonLen-wortPos);
!WriteLine(json);
!Daten mit Suchworten aus JSON-File ausfiltern:
!Total -> Pow_Energie_total
string wordStart = "Power\":";
string wordEnd = ",";
integer wordStartPos = json.Find(wordStart) + wordStart.Length();
string jsonRest = json.Substr(wordStartPos,jsonLen-wordStartPos);
integer wordEndPos = jsonRest.Find(wordEnd);
string data = json.Substr(wordStartPos,wordEndPos);
if(data.Length() < 1) {
quit; !Auslesen fehlgeschlagen
} else {
!real dataFloat = data.ToFloat(); !nur ggf. noetig, testen
dom.GetObject("Pow_Momentanleistung").State(data);
if(data >= 1){
dom.GetObject("Pow_Status").State(true); !Setze Status auf eingeschaltet
}
else{
dom.GetObject("Pow_Status").State(false); !Setze Status auf ausgeschaltet
}
}
!WriteLine(data);
!Yesterday -> Pow_Energie_gestern
!TODO
!Today -> Pow_Energie_heute
!TODO
!Systemzeit -> Pow_Aktualisierung
dom.GetObject('Pow_Aktualisierung').State(system.Date("%d.%m.%Y %H:%M:%S"));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment