[
   {
       "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": "",
       "libs": [],
       "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": "",
       "libs": [],
       "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"
           ]
       ]
   }
]