- Home
- Deutsch
- Skripten / Logik
- Node-Red
- Energieverbrauch in Node Red mit Shelly Plug S erfassen
Energieverbrauch in Node Red mit Shelly Plug S erfassen
-
Hallo zusammen,
ich habe mir mit einem Shelly Plug S und Node Red eine Messung gebaut mit der ich den Energievebrauch meines 3D Druckers in Euros umrechnen kann.
Der Shelly Plug S liefert den Verbrauch in Watt Minuten per MQTT an Node Red.
Mein Problem ist allerdings, dass wenn der Shelly Plug S selbst keinen Strom mehr hat wieder bei 0 anfängt zu zählen. Ich hätte aber gerne, dass in Node Red bei dem letzten Wert weitergezählt wird.
Ich habe mich jetzt schon mal in das Thema etwas eingelesen und habe herausgefunden, dass ich dafür mit Kontextdaten in Node Red arbeiten muss.
In der "settings.js" Datei habe ich die Funktion auch bereits aktiviert.contextStorage: { storeInFile: { module: "localfilesystem"}, default : { module: "memory" } },
Kann mir ab hier bitte jemand ein paar Tipps oder nützliche Infos liefern wie ich das am besten realisiert bekomme?
Vielleicht hat hier auch schon jemand das gleiche Problem gelöst und kann mir Codebeispiele zur Verfügung stellen.Vielen Dank schon mal im Voraus
-
@retzi91 Also das ist noch nicht durchgetestet - aber ich habe mir einen NodeRed Flow gebastelt, der mir in Userdata folgende Datenstuktur bereitstellt.
Ich bin aber kein FAN von Function Node.
Wenn Du daran Interesse hast, dann kann ich Dir den Flow schon zur Verfügung stellen. Allerdings läuft der noch nicht über 2 Monate - so dass ich noch nicht sagen kann ob Vormonat oder längere Zeiträume funktionieren.
Im Moment arbeitet der Flow auch nach Zeiträumen und nicht nach kalendarischem Datum - das wollte ich implementieren, habe das aber noch nicht gemacht. Zuviele Baustellen. Aber Du siehst ich habe zumindest schon mal einen Datenpunkt vorgesehen, um den Flow zu erweitern.
Ich wollte auch noch ein paar Nodes - für laufende Zeiträume machen - also die fließende Zeitperioden ausgeben - aber das ist auch noch etwas Aufwand. Momentan ist es halt ZigZack. Eine Stunde wir aufsummiert und dann als komplette Periode in der Vorstunde ausgegeben usw
Sieht man momentan and diesen Chart.
Meine Ausgaben passen auch noch nicht - die stelle ich Dir ggf. deshalb nicht zur Verfügung - aber die Berechnungen - die Du machst mit den Kosten - das geht schon:
EDIT:
Noch ein Zusatz - mit Kontextdaten zu arbeiten - hilft Dir zwar die Werte zu erhalten, wenn der Shelly mal stromlos ist - doch sobald Du NodeRed neustartest, sind auch die Kontextdaten weg. Deswegen speichere ich alles in Datenpunkten, damit die auch einen Neustart des Adapters oder der ganzen Maschine überstehen.
-
@mickym vielen Dank schon mal für die ausführliche Antwort
Dein Flow würde mir schon ein ganzen Stück weiter bringen, da hätte ich auf jeden Fall Interesse dran. -
Also dann mal eine kleine Anleitung. Damit der Flow arbeitet - darfst Du keine Stringkonvertierung machen und musst erlauben, dass Node Red Datenpunkte anlegen darf:
Wenn Du die Struktur einigermassen übernimmst, brauchst Du natürlich weniger anpassen:
Du musst nur einen Datenpunkt erstellen, den Du mit reset bezeichnet hast. Die anderen Datenpunkte werden selbst erstellt, sobald der Wert des reset Datenpunktes = true ist.
Nach Du auch über mqtt arbeitest, wird der power Punkt dann in die Struktur mit übernommen und dann sollte der untere Flow passen:Hier zum Import:
[ { "id": "85b15a4e.9faa68", "type": "mqtt in", "z": "2c7d577a.9b4378", "name": "", "topic": "shellies/+/+/+/power", "qos": "2", "datatype": "auto", "broker": "2c892b43.13e934", "x": 150, "y": 1180, "wires": [ [ "29d04232.c3ca2e" ] ] }, { "id": "29d04232.c3ca2e", "type": "change", "z": "2c7d577a.9b4378", "name": "msg.topic und number(payload)", "rules": [ { "t": "change", "p": "topic", "pt": "msg", "from": "shellies\\/(.*)\\/.*\\/0\\/power", "fromt": "re", "to": "$1_0", "tot": "str" }, { "t": "change", "p": "topic", "pt": "msg", "from": "shellies\\/(.*)\\/.*\\/1\\/power", "fromt": "re", "to": "$1_1", "tot": "str" }, { "t": "set", "p": "payload", "pt": "msg", "to": "$number(payload)\t", "tot": "jsonata" } ], "action": "", "property": "", "from": "", "to": "", "reg": false, "x": 430, "y": 1180, "wires": [ [ "9e6fe19e.99952" ] ] }, { "id": "9e6fe19e.99952", "type": "change", "z": "2c7d577a.9b4378", "name": "", "rules": [ { "t": "set", "p": "topic", "pt": "msg", "to": "\"0_userdata.0.stromverbrauch.shellies.\" & topic & \".power\"", "tot": "jsonata" } ], "action": "", "property": "", "from": "", "to": "", "reg": false, "x": 700, "y": 1180, "wires": [ [ "18752e47.ecc2e2" ] ] }, { "id": "18752e47.ecc2e2", "type": "ioBroker out", "z": "2c7d577a.9b4378", "name": "", "topic": "", "ack": "true", "autoCreate": "false", "stateName": "", "role": "", "payloadType": "", "readonly": "", "stateUnit": "", "stateMin": "", "stateMax": "", "x": 900, "y": 1180, "wires": [] }, { "id": "2c892b43.13e934", "type": "mqtt-broker", "name": "ioBrokerMQTT", "broker": "192.168.178.28", "port": "1883", "clientid": "mqttClientNodeRed", "usetls": false, "compatmode": false, "keepalive": "60", "cleansession": false, "birthTopic": "node-red/connections/mqttClientNodeRed/state", "birthQos": "0", "birthPayload": "connected", "closeTopic": "node-red/connections/mqttClientNodeRed/state", "closeQos": "0", "closePayload": "disconnected", "willTopic": "", "willQos": "0", "willPayload": "" } ]
Der Input erfolgt dann automatisch über Wildcards, wenn Du mehrere Geräte überwachen willst:
Der Flow ist relativ komplex:
Hier zum Import:
[ { "id": "5f860e94.598d3", "type": "comment", "z": "2c7d577a.9b4378", "name": "Reset Stromverbrauch mit Neustart des Shellies", "info": "", "x": 380, "y": 60, "wires": [] }, { "id": "deddb841.5d18f8", "type": "comment", "z": "2c7d577a.9b4378", "name": " Shellies", "info": "", "x": 90, "y": 60, "wires": [] }, { "id": "35850f3.22dd0f", "type": "inject", "z": "2c7d577a.9b4378", "name": "", "props": [ { "p": "payload" }, { "p": "topic", "vt": "str" } ], "repeat": "", "crontab": "", "once": false, "onceDelay": 0.1, "topic": "0_userdata/0/stromverbrauch/shellies/shelly-dummy/reset", "payload": "true", "payloadType": "bool", "x": 310, "y": 180, "wires": [ [ "80f66656.38dec8" ] ] }, { "id": "80f66656.38dec8", "type": "switch", "z": "2c7d577a.9b4378", "name": "reset = true?", "property": "payload", "propertyType": "msg", "rules": [ { "t": "true" } ], "checkall": "true", "repair": false, "outputs": 1, "x": 510, "y": 220, "wires": [ [ "98ce503d.77b81" ] ] }, { "id": "5a848615.797c98", "type": "change", "z": "2c7d577a.9b4378", "name": "define and reset topics", "rules": [ { "t": "set", "p": "payload", "pt": "msg", "to": "[{\"common\":{\"name\":\"total\",\"desc\":\"Created by Node-Red\",\"role\":\"state\",\"type\":\"number\",\"unit\":\"kWh\",\"read\":true,\"write\":false,\"def\":0},\"type\":\"state\"},{\"common\":{\"name\":\"yesterday\",\"desc\":\"Created by Node-Red\",\"role\":\"state\",\"type\":\"number\",\"unit\":\"Wh\",\"read\":true,\"write\":false,\"def\":0},\"type\":\"state\"},{\"common\":{\"name\":\"today\",\"desc\":\"Created by Node-Red\",\"role\":\"state\",\"type\":\"number\",\"unit\":\"Wh\",\"read\":true,\"write\":false,\"def\":0},\"type\":\"state\"},{\"common\":{\"name\":\"year\",\"desc\":\"Created by Node-Red\",\"role\":\"state\",\"type\":\"number\",\"unit\":\"kWh\",\"read\":true,\"write\":false,\"def\":0},\"type\":\"state\"},{\"common\":{\"name\":\"month\",\"desc\":\"Created by Node-Red\",\"role\":\"state\",\"type\":\"number\",\"unit\":\"kWh\",\"read\":true,\"write\":false,\"def\":0},\"type\":\"state\"},{\"common\":{\"name\":\"power\",\"desc\":\"Created by Node-Red\",\"role\":\"state\",\"type\":\"number\",\"unit\":\"Wh\",\"read\":true,\"write\":false,\"def\":0},\"type\":\"state\"},{\"common\":{\"name\":\"hour\",\"desc\":\"Created by Node-Red\",\"role\":\"state\",\"type\":\"number\",\"unit\":\"Wh\",\"read\":true,\"write\":false,\"def\":0},\"type\":\"state\"},{\"common\":{\"name\":\"hour_before\",\"desc\":\"Created by Node-Red\",\"role\":\"state\",\"type\":\"number\",\"unit\":\"Wh\",\"read\":true,\"write\":false,\"def\":0},\"type\":\"state\"},{\"common\":{\"name\":\"year_before\",\"desc\":\"Created by Node-Red\",\"role\":\"state\",\"type\":\"number\",\"unit\":\"kWh\",\"read\":true,\"write\":false,\"def\":0},\"type\":\"state\"},{\"common\":{\"name\":\"month_before\",\"desc\":\"Created by Node-Red\",\"role\":\"state\",\"type\":\"number\",\"unit\":\"kWh\",\"read\":true,\"write\":false,\"def\":0},\"type\":\"state\"},{\"common\":{\"name\":\"week_before\",\"desc\":\"Created by Node-Red\",\"role\":\"state\",\"type\":\"number\",\"unit\":\"Wh\",\"read\":true,\"write\":false,\"def\":0},\"type\":\"state\"},{\"common\":{\"name\":\"week\",\"desc\":\"Created by Node-Red\",\"role\":\"state\",\"type\":\"number\",\"unit\":\"Wh\",\"read\":true,\"write\":false,\"def\":0},\"type\":\"state\"},{\"common\":{\"name\":\"start\",\"desc\":\"Created by Node-Red\",\"role\":\"state\",\"type\":\"string\",\"read\":true,\"write\":false,\"def\":\"\"},\"type\":\"state\"},{\"common\":{\"name\":\"reset\",\"desc\":\"Created by Node-Red\",\"role\":\"state\",\"type\":\"boolean\",\"read\":true,\"write\":true,\"def\":false},\"type\":\"state\"},{\"common\":{\"name\":\"use_time_date\",\"desc\":\"Created by Node-Red\",\"role\":\"state\",\"type\":\"boolean\",\"read\":true,\"write\":true,\"def\":false},\"type\":\"state\"},{\"common\":{\"name\":\"flot\",\"role\":\"\",\"type\":\"string\",\"desc\":\"Created by Node-Red\",\"def\":\"\",\"read\":true,\"write\":true},\"type\":\"state\"}]", "tot": "json" } ], "action": "", "property": "", "from": "", "to": "", "reg": false, "x": 1100, "y": 220, "wires": [ [ "85d30229.134d7" ] ] }, { "id": "85d30229.134d7", "type": "split", "z": "2c7d577a.9b4378", "name": "", "splt": "\\n", "spltType": "str", "arraySplt": 1, "arraySpltType": "len", "stream": false, "addname": "", "x": 1280, "y": 220, "wires": [ [ "14273608.84409a" ] ] }, { "id": "98ce503d.77b81", "type": "change", "z": "2c7d577a.9b4378", "name": "get parent topic", "rules": [ { "t": "change", "p": "topic", "pt": "msg", "from": "(.*)[\\.,\\/]reset$", "fromt": "re", "to": "$1", "tot": "str" } ], "action": "", "property": "", "from": "", "to": "", "reg": false, "x": 800, "y": 220, "wires": [ [ "5a848615.797c98" ] ] }, { "id": "14273608.84409a", "type": "change", "z": "2c7d577a.9b4378", "name": "set new topic", "rules": [ { "t": "change", "p": "topic", "pt": "msg", "from": "/", "fromt": "str", "to": ".", "tot": "str" }, { "t": "set", "p": "topic", "pt": "msg", "to": "topic & '.' & payload.common.name", "tot": "jsonata" } ], "action": "", "property": "", "from": "", "to": "", "reg": false, "x": 1430, "y": 220, "wires": [ [ "c26782ba.6d0cb" ] ] }, { "id": "a066a4b2.bffac8", "type": "switch", "z": "2c7d577a.9b4378", "name": "reset to default?", "property": "payload.common.def", "propertyType": "msg", "rules": [ { "t": "null" }, { "t": "nnull" } ], "checkall": "true", "repair": false, "outputs": 2, "x": 2280, "y": 220, "wires": [ [ "aaaf4f8e.09af2" ], [ "8c7b3b16.005078" ] ] }, { "id": "c26782ba.6d0cb", "type": "change", "z": "2c7d577a.9b4378", "name": "translate definition", "rules": [ { "t": "set", "p": "stateName", "pt": "msg", "to": "payload.common.name", "tot": "msg" }, { "t": "set", "p": "stateType", "pt": "msg", "to": "payload.common.type", "tot": "msg" }, { "t": "set", "p": "stateReadonly", "pt": "msg", "to": "payload.common.write", "tot": "msg" } ], "action": "", "property": "", "from": "", "to": "", "reg": false, "x": 1610, "y": 220, "wires": [ [ "edec84c8.29ee98" ] ] }, { "id": "edec84c8.29ee98", "type": "switch", "z": "2c7d577a.9b4378", "name": "unit available?", "property": "payload.common.unit", "propertyType": "msg", "rules": [ { "t": "nnull" }, { "t": "null" } ], "checkall": "true", "repair": false, "outputs": 2, "x": 1820, "y": 220, "wires": [ [ "7b7016cb.ac10c8" ], [ "1f3da0b6.9e766f" ] ] }, { "id": "1f3da0b6.9e766f", "type": "function", "z": "2c7d577a.9b4378", "name": "do nothing", "func": "\nreturn msg;", "outputs": 1, "noerr": 0, "initialize": "", "finalize": "", "x": 2050, "y": 240, "wires": [ [ "a066a4b2.bffac8" ] ] }, { "id": "7b7016cb.ac10c8", "type": "change", "z": "2c7d577a.9b4378", "name": "", "rules": [ { "t": "set", "p": "stateUnit", "pt": "msg", "to": "payload.common.unit", "tot": "msg" } ], "action": "", "property": "", "from": "", "to": "", "reg": false, "x": 2050, "y": 200, "wires": [ [ "a066a4b2.bffac8" ] ] }, { "id": "cc84166f.06b778", "type": "ioBroker out", "z": "2c7d577a.9b4378", "name": "", "topic": "", "ack": "false", "autoCreate": "true", "stateName": "", "role": "", "payloadType": "", "readonly": "", "stateUnit": "", "stateMin": "", "stateMax": "", "x": 2720, "y": 220, "wires": [] }, { "id": "aaaf4f8e.09af2", "type": "change", "z": "2c7d577a.9b4378", "name": "", "rules": [ { "t": "delete", "p": "payload", "pt": "msg" } ], "action": "", "property": "", "from": "", "to": "", "reg": false, "x": 2500, "y": 180, "wires": [ [ "cc84166f.06b778" ] ] }, { "id": "8c7b3b16.005078", "type": "change", "z": "2c7d577a.9b4378", "name": "set defaults", "rules": [ { "t": "set", "p": "default", "pt": "msg", "to": "payload.common.def", "tot": "msg" }, { "t": "delete", "p": "payload", "pt": "msg" }, { "t": "move", "p": "default", "pt": "msg", "to": "payload", "tot": "msg" } ], "action": "", "property": "", "from": "", "to": "", "reg": false, "x": 2470, "y": 260, "wires": [ [ "cc84166f.06b778" ] ] }, { "id": "cc6771bb.13f14", "type": "switch", "z": "2c7d577a.9b4378", "name": "start without object", "property": "payload", "propertyType": "msg", "rules": [ { "t": "eq", "v": "", "vt": "str" } ], "checkall": "true", "repair": false, "outputs": 1, "x": 790, "y": 280, "wires": [ [ "ce0be96b.5c5fa8" ] ] }, { "id": "ce0be96b.5c5fa8", "type": "change", "z": "2c7d577a.9b4378", "name": "Initialisiere start", "rules": [ { "t": "set", "p": "payload", "pt": "msg", "to": "{}", "tot": "json" }, { "t": "set", "p": "payload.ts", "pt": "msg", "to": "", "tot": "date" }, { "t": "set", "p": "payload.date", "pt": "msg", "to": " $moment(payload.ts).locale(\"de\").tz('Europe/Berlin').format('YYYY-MM-DD HH:mm:ss')", "tot": "jsonata" }, { "t": "set", "p": "payload.timespan", "pt": "msg", "to": "{\"hour\":0,\"day\":0,\"week\":0,\"month\":0,\"year\":0}", "tot": "json" }, { "t": "change", "p": "topic", "pt": "msg", "from": "/", "fromt": "str", "to": ".", "tot": "str" } ], "action": "", "property": "", "from": "", "to": "", "reg": false, "x": 1060, "y": 280, "wires": [ [ "450761a2.575e", "d9135ecb.c5bef" ] ] }, { "id": "729a31f7.1f0d", "type": "change", "z": "2c7d577a.9b4378", "name": "set msg.topic & msg.power", "rules": [ { "t": "change", "p": "topic", "pt": "msg", "from": "(.*)\\/power", "fromt": "re", "to": "$1", "tot": "str" }, { "t": "move", "p": "payload", "pt": "msg", "to": "power", "tot": "msg" } ], "action": "", "property": "", "from": "", "to": "", "reg": false, "x": 560, "y": 560, "wires": [ [ "257ceae5.2dbc16" ] ] }, { "id": "257ceae5.2dbc16", "type": "function", "z": "2c7d577a.9b4378", "name": "Aktuelle Wh", "func": "var lasttime = context.get(msg.topic);\nvar timespan;\n\nif (lasttime === undefined) {\n lasttime = msg.timestamp;\n context.set(msg.topic,lasttime);\n return null;\n}\n\ntimespan = msg.timestamp - lasttime;\nlasttime = msg.timestamp;\ncontext.set(msg.topic,lasttime);\nmsg.Wh=msg.power*(timespan/1000/60/60);\n\nreturn msg;", "outputs": 1, "noerr": 0, "initialize": "", "finalize": "", "x": 910, "y": 560, "wires": [ [ "7b7a6cd4.f05704" ] ] }, { "id": "d9135ecb.c5bef", "type": "json", "z": "2c7d577a.9b4378", "name": "", "property": "payload", "action": "", "pretty": false, "x": 1230, "y": 280, "wires": [ [ "ffaaaca9.31ecf" ] ] }, { "id": "ffaaaca9.31ecf", "type": "ioBroker out", "z": "2c7d577a.9b4378", "name": "", "topic": "", "ack": "true", "autoCreate": "false", "stateName": "", "role": "", "payloadType": "", "readonly": "", "stateUnit": "", "stateMin": "", "stateMax": "", "x": 1410, "y": 280, "wires": [] }, { "id": "7b7a6cd4.f05704", "type": "function", "z": "2c7d577a.9b4378", "name": "add topics", "func": "var outputMsgs = [];\nvar topiclist=\"hour,month,today,total,week,year,start\";\n\nvar topics = topiclist.split(\",\");\n\nfor (var w in topics) {\n outputMsgs.push({topic:msg.topic + '/' + topics[w], Wh: msg.Wh});\n}\nreturn [ outputMsgs ];\n", "outputs": 1, "noerr": 0, "initialize": "", "finalize": "", "x": 1090, "y": 560, "wires": [ [ "7e64ff11.4278" ] ] }, { "id": "1de3c97e.1a0bf7", "type": "ioBroker get", "z": "2c7d577a.9b4378", "name": "hour", "topic": "", "attrname": "payload", "payloadType": "value", "x": 1410, "y": 380, "wires": [ [ "9d1bc133.e80b7" ] ] }, { "id": "21dc8345.7618bc", "type": "ioBroker get", "z": "2c7d577a.9b4378", "name": "today", "topic": "", "attrname": "payload", "payloadType": "value", "x": 1410, "y": 440, "wires": [ [ "9d1bc133.e80b7" ] ] }, { "id": "70288298.bf84dc", "type": "ioBroker get", "z": "2c7d577a.9b4378", "name": "month", "topic": "", "attrname": "payload", "payloadType": "value", "x": 1410, "y": 560, "wires": [ [ "dea3c66b.907df8" ] ] }, { "id": "b9db5bd0.a50a78", "type": "ioBroker get", "z": "2c7d577a.9b4378", "name": "total", "topic": "", "attrname": "payload", "payloadType": "value", "x": 1410, "y": 680, "wires": [ [ "dea3c66b.907df8" ] ] }, { "id": "6e65954f.bdec0c", "type": "ioBroker get", "z": "2c7d577a.9b4378", "name": "week", "topic": "", "attrname": "payload", "payloadType": "value", "x": 1410, "y": 500, "wires": [ [ "9d1bc133.e80b7" ] ] }, { "id": "44b9db26.ca4314", "type": "ioBroker get", "z": "2c7d577a.9b4378", "name": "year", "topic": "", "attrname": "payload", "payloadType": "value", "x": 1410, "y": 620, "wires": [ [ "dea3c66b.907df8" ] ] }, { "id": "9d1bc133.e80b7", "type": "change", "z": "2c7d577a.9b4378", "name": "Addiere Wh", "rules": [ { "t": "set", "p": "payload", "pt": "msg", "to": "payload + Wh", "tot": "jsonata" }, { "t": "change", "p": "topic", "pt": "msg", "from": "/", "fromt": "str", "to": ".", "tot": "str" } ], "action": "", "property": "", "from": "", "to": "", "reg": false, "x": 1600, "y": 440, "wires": [ [ "4bca099e.ca1e88" ] ] }, { "id": "dea3c66b.907df8", "type": "change", "z": "2c7d577a.9b4378", "name": "Addiere kWh", "rules": [ { "t": "set", "p": "payload", "pt": "msg", "to": "payload + (Wh/1000)", "tot": "jsonata" }, { "t": "change", "p": "topic", "pt": "msg", "from": "/", "fromt": "str", "to": ".", "tot": "str" } ], "action": "", "property": "", "from": "", "to": "", "reg": false, "x": 1590, "y": 620, "wires": [ [ "4bca099e.ca1e88" ] ] }, { "id": "4bca099e.ca1e88", "type": "ioBroker out", "z": "2c7d577a.9b4378", "name": "", "topic": "", "ack": "true", "autoCreate": "false", "stateName": "", "role": "", "payloadType": "", "readonly": "", "stateUnit": "", "stateMin": "", "stateMax": "", "x": 1820, "y": 520, "wires": [] }, { "id": "7e64ff11.4278", "type": "switch", "z": "2c7d577a.9b4378", "name": "", "property": "topic", "propertyType": "msg", "rules": [ { "t": "regex", "v": ".*[\\/,\\.]hour$", "vt": "str", "case": false }, { "t": "regex", "v": ".*[\\/,\\.]today$", "vt": "str", "case": false }, { "t": "regex", "v": ".*[\\/,\\.]week$", "vt": "str", "case": false }, { "t": "regex", "v": ".*[\\/,\\.]month$", "vt": "str", "case": false }, { "t": "regex", "v": ".*[\\/,\\.]year$", "vt": "str", "case": false }, { "t": "regex", "v": ".*[\\/,\\.]total$", "vt": "str", "case": false }, { "t": "regex", "v": ".*[\\/,\\.]start$", "vt": "str", "case": false } ], "checkall": "true", "repair": false, "outputs": 7, "x": 1250, "y": 560, "wires": [ [ "1de3c97e.1a0bf7" ], [ "21dc8345.7618bc" ], [ "6e65954f.bdec0c" ], [ "70288298.bf84dc" ], [ "44b9db26.ca4314" ], [ "b9db5bd0.a50a78" ], [ "93fdc929.570df8" ] ] }, { "id": "93fdc929.570df8", "type": "ioBroker get", "z": "2c7d577a.9b4378", "name": "start", "topic": "", "attrname": "payload", "payloadType": "value", "x": 1410, "y": 780, "wires": [ [ "107973e8.2fdc3c" ] ] }, { "id": "97f2d9d1.1420f8", "type": "change", "z": "2c7d577a.9b4378", "name": "", "rules": [ { "t": "change", "p": "topic", "pt": "msg", "from": "(.*)\\/start", "fromt": "re", "to": "$1", "tot": "str" }, { "t": "move", "p": "payload", "pt": "msg", "to": "start", "tot": "msg" }, { "t": "change", "p": "topic", "pt": "msg", "from": "/", "fromt": "str", "to": ".", "tot": "str" }, { "t": "set", "p": "topic", "pt": "msg", "to": "topic & '.use_time_date'", "tot": "jsonata" } ], "action": "", "property": "", "from": "", "to": "", "reg": false, "x": 1900, "y": 780, "wires": [ [ "18019d9a.2b8552" ] ] }, { "id": "18019d9a.2b8552", "type": "ioBroker get", "z": "2c7d577a.9b4378", "name": "use_time_date", "topic": "", "attrname": "payload", "payloadType": "value", "x": 2100, "y": 780, "wires": [ [ "7f7e5f9f.41f46" ] ] }, { "id": "453a7a8d.9c72f4", "type": "switch", "z": "2c7d577a.9b4378", "name": "", "property": "payload", "propertyType": "msg", "rules": [ { "t": "true" }, { "t": "false" } ], "checkall": "true", "repair": false, "outputs": 2, "x": 2470, "y": 780, "wires": [ [], [ "fc7f508d.c986d", "fc2c5159.6e28" ] ] }, { "id": "fc2c5159.6e28", "type": "function", "z": "2c7d577a.9b4378", "name": "trigger nach Dauer", "func": "const sec = 1000;\nconst min = sec * 60;\nconst hour = min * 60;\nconst day = hour * 24;\nconst week = day * 7;\nconst month = day * 30.4375;\nconst year = month * 12;\nvar ts = new Date().getTime();\nvar bUpdate = false;\n// msg.topic = msg.topic.replace(/(.*)\\.use_time_date/,\"$1\");\n// var device = msg.topic.replace(/.*shellies\\.(.*)/,\"$1\");\nvar device = msg.device;\n\nvar timespan = context.get(device)\nif (timespan === undefined) {\n \n // node.warn ('device: ' + device);\n // node.warn ('msg.start.timespan: ' + JSON.stringify(msg.start.timespan));\n timespan = msg.start.timespan;\n context.set(device,timespan); \n // node.warn ('timespan = defined');\n return [null,null]\n \n}\n\nvar addValues = [];\n// var resetValues = [];\nvar msgStart = {topic: msg.topic + '.start', payload: msg.start}\n\nvar topiclist=\"hour,month,today,total,week,year\";\nvar topics = topiclist.split(\",\");\n\n// node.warn ('device: ' + device);\nfor (var w in topics) {\n var diff = 0;\n switch(topics[w]) {\n case 'hour':\n // code block\n diff= Math.trunc((ts - msg.start.ts) / hour);\n // node.warn ('const hour: ' + hour);\n // hours: timespan.hour\n if (diff > timespan.hour){\n bUpdate = true;\n timespan.hour=diff;\n\n addValues.push({topic: msg.topic + '.hour', payload: 0 , newTopic: msg.topic + '.hour_before' , start: msg.start.ts , ts: ts , hours: diff, comment: diff + ' hours since start' });\n }\n break;\n case 'month':\n // code block\n diff= Math.trunc((ts - msg.start.ts) / month);\n // node.warn ('const month: ' + month);\n // months: timespan.month\n if (diff > timespan.month){\n bUpdate = true;\n timespan.month=diff;\n\n addValues.push({topic: msg.topic + '.month', payload: 0 , newTopic: msg.topic + '.month_before', start: msg.start.ts , ts: ts , months: diff, comment: diff + ' months since start' });\n }\n break;\n case 'today':\n // code block\n diff= Math.trunc((ts - msg.start.ts) / day);\n // node.warn ('const day: ' + day);\n // days: timespan.day\n if (diff > timespan.day){\n bUpdate = true;\n timespan.day=diff;\n\n addValues.push({topic: msg.topic + '.today', payload: 0 , newTopic: msg.topic + '.yesterday' , start: msg.start.ts , ts: ts , days: diff, comment: diff + ' days since start'});\n }\n break;\n case 'total':\n // code block - nichts machen - gibt nichts zum Zurücksetzen\n break;\n case 'week':\n // code block\n diff= Math.trunc((ts - msg.start.ts) / week);\n // node.warn ('const week: ' + week);\n // weeks: timespan.week\n if (diff > timespan.week){\n bUpdate = true;\n timespan.week=diff;\n\n addValues.push({topic: msg.topic + '.week', payload: 0 , newTopic: msg.topic + '.week_before' , start: msg.start.ts , ts: ts , weeks: diff, comment: diff + ' weeks since start' });\n }\n break;\n case 'year':\n // code block\n diff= Math.trunc((ts - msg.start.ts) / year);\n // if (diff > 0 ) { node.warn(' -> ts: ' + ts + ' -> msg.start.ts: ' + msg.start.ts + ' -> (ts - msg.start.ts) / year: ' + ((ts - msg.start.ts) / year))}\n // node.warn ('const year: ' + year);\n // years: timespan.hour\n if (diff > timespan.year){\n bUpdate = true;\n timespan.year=diff;\n\n addValues.push({topic: msg.topic + '.year', payload: 0 , newTopic: msg.topic + '.year_before', start: msg.start.ts , ts: ts , years: diff, comment: diff + ' years since start'});\n }\n break;\n \n default:\n // code block\n } \n}\n\n// bUpdate = true;\n\nif (bUpdate){\n context.set(device,timespan);\n msgStart.payload.timespan = timespan;\n return [ addValues, msgStart ];\n}\n\n\n// outputMsgs.push({topic:msg.topic + '/' + topics[w], Wh: msg.Wh});", "outputs": 2, "noerr": 0, "initialize": "", "finalize": "", "x": 2690, "y": 860, "wires": [ [ "4b61812.853d08", "990a6d1c.e33ae" ], [ "4cdfe1c1.22b5e", "fc7e7f82.b40bc" ] ], "outputLabels": [ "add", "start" ] }, { "id": "4cdfe1c1.22b5e", "type": "debug", "z": "2c7d577a.9b4378", "name": "start", "active": false, "tosidebar": true, "console": false, "tostatus": true, "complete": "true", "targetType": "full", "statusVal": "payload.timespan", "statusType": "msg", "x": 2890, "y": 980, "wires": [] }, { "id": "4b61812.853d08", "type": "ioBroker get", "z": "2c7d577a.9b4378", "name": "save value", "topic": "", "attrname": "set", "payloadType": "value", "x": 2890, "y": 820, "wires": [ [ "48de049c.55a6cc", "2f65897a.e15036" ] ] }, { "id": "2f65897a.e15036", "type": "change", "z": "2c7d577a.9b4378", "name": "set previous", "rules": [ { "t": "set", "p": "topic", "pt": "msg", "to": "newTopic", "tot": "msg" }, { "t": "set", "p": "payload", "pt": "msg", "to": "set", "tot": "msg" } ], "action": "", "property": "", "from": "", "to": "", "reg": false, "x": 3070, "y": 860, "wires": [ [ "d5922114.e3edb" ] ] }, { "id": "d5922114.e3edb", "type": "ioBroker out", "z": "2c7d577a.9b4378", "name": "set previous period", "topic": "", "ack": "true", "autoCreate": "false", "stateName": "", "role": "", "payloadType": "", "readonly": "", "stateUnit": "", "stateMin": "", "stateMax": "", "x": 3280, "y": 860, "wires": [] }, { "id": "48de049c.55a6cc", "type": "ioBroker out", "z": "2c7d577a.9b4378", "name": "reset current period", "topic": "", "ack": "true", "autoCreate": "false", "stateName": "", "role": "", "payloadType": "", "readonly": "", "stateUnit": "", "stateMin": "", "stateMax": "", "x": 3090, "y": 800, "wires": [] }, { "id": "afcbed60.a3b52", "type": "ioBroker out", "z": "2c7d577a.9b4378", "name": "update start.timespan", "topic": "", "ack": "true", "autoCreate": "false", "stateName": "", "role": "", "payloadType": "", "readonly": "", "stateUnit": "", "stateMin": "", "stateMax": "", "x": 3080, "y": 920, "wires": [] }, { "id": "fc7e7f82.b40bc", "type": "json", "z": "2c7d577a.9b4378", "name": "", "property": "payload", "action": "", "pretty": false, "x": 2880, "y": 920, "wires": [ [ "afcbed60.a3b52" ] ] }, { "id": "990a6d1c.e33ae", "type": "debug", "z": "2c7d577a.9b4378", "name": "add", "active": false, "tosidebar": true, "console": false, "tostatus": false, "complete": "payload", "targetType": "msg", "statusVal": "", "statusType": "auto", "x": 2870, "y": 760, "wires": [] }, { "id": "fc7f508d.c986d", "type": "debug", "z": "2c7d577a.9b4378", "name": "", "active": false, "tosidebar": true, "console": false, "tostatus": false, "complete": "true", "targetType": "full", "statusVal": "", "statusType": "auto", "x": 2670, "y": 800, "wires": [] }, { "id": "8e9e06fd.870a08", "type": "json", "z": "2c7d577a.9b4378", "name": "", "property": "payload", "action": "", "pretty": false, "x": 1730, "y": 780, "wires": [ [ "97f2d9d1.1420f8" ] ] }, { "id": "107973e8.2fdc3c", "type": "switch", "z": "2c7d577a.9b4378", "name": "JSON string?", "property": "payload", "propertyType": "msg", "rules": [ { "t": "istype", "v": "json", "vt": "json" } ], "checkall": "true", "repair": false, "outputs": 1, "x": 1570, "y": 780, "wires": [ [ "8e9e06fd.870a08" ] ] }, { "id": "450761a2.575e", "type": "debug", "z": "2c7d577a.9b4378", "name": "", "active": false, "tosidebar": true, "console": false, "tostatus": false, "complete": "true", "targetType": "full", "statusVal": "", "statusType": "auto", "x": 1240, "y": 340, "wires": [] }, { "id": "13719db3.07a702", "type": "ioBroker in", "z": "2c7d577a.9b4378", "name": "", "topic": "0_userdata.0.stromverbrauch.*.*.reset", "payloadType": "value", "onlyack": "", "func": "all", "gap": "", "fireOnStart": "false", "x": 210, "y": 220, "wires": [ [ "80f66656.38dec8" ] ] }, { "id": "7f7e5f9f.41f46", "type": "change", "z": "2c7d577a.9b4378", "name": "topic & device", "rules": [ { "t": "change", "p": "topic", "pt": "msg", "from": "(.*)\\.use_time_date", "fromt": "re", "to": "$1", "tot": "str" }, { "t": "set", "p": "device", "pt": "msg", "to": "topic", "tot": "msg" }, { "t": "change", "p": "device", "pt": "msg", "from": ".*[\\.,\\/](.*)", "fromt": "re", "to": "$1", "tot": "str" } ], "action": "", "property": "", "from": "", "to": "", "reg": false, "x": 2300, "y": 780, "wires": [ [ "453a7a8d.9c72f4" ] ] }, { "id": "c208dc2e.5880a", "type": "ioBroker in", "z": "2c7d577a.9b4378", "name": "", "topic": "0_userdata.0.stromverbrauch.*.*.power", "payloadType": "value", "onlyack": "", "func": "all", "gap": "", "fireOnStart": "false", "x": 210, "y": 560, "wires": [ [ "729a31f7.1f0d" ] ] }, { "id": "ec03eb7f.ecf038", "type": "ioBroker in", "z": "2c7d577a.9b4378", "name": "", "topic": "0_userdata.0.stromverbrauch.*.*.start", "payloadType": "value", "onlyack": "", "func": "all", "gap": "", "fireOnStart": "false", "x": 210, "y": 280, "wires": [ [ "cc6771bb.13f14" ] ] }, { "id": "c37842ae.5829b", "type": "inject", "z": "2c7d577a.9b4378", "name": "", "props": [ { "p": "topic", "vt": "str" } ], "repeat": "", "crontab": "", "once": false, "onceDelay": 0.1, "topic": "", "x": 810, "y": 160, "wires": [ [] ] } ]
Bei Fragen melden. Aber der Flow ist nicht ganz banal.
Noch eine Ergänzung: Als Input nutze ich nur den Power-Punkt des Momentanverbrauchs und nicht den Energy Datenpunkt mit den Wattminuten.
-
@mickym Vielen Dank schon mal für deine Arbeit
Habe den Flow gerade schon mal bei mir eingefügt und werde mich diese Woche mal intensiv damit beschäftigen. -
@retzi91 Ach und anstelle in der settings.js die Kontextdaten nun im Filesystem zu verankern - sollte man gerade mit iobroker die Daten lieber dort speichern.
-
Auch an der Stelle nochmal vielen Dank für die Hilfe
Dein Code hat mich um einiges weitergebracht.