Hi.
When i am trying to get a global variable with function
TotkWh = global.get("power.VK.gang.kWh.Total")||0;
i get this error:
function : (error) "TypeError: Cannot read property 'VK' of undefined"
If i write it without .
like this it works:
TotkWh = global.get("powerVKgangkWhTotal")||0;
i am using ||0
to get a 0 for the first read when it is undefined.
Is this a bug or am i using the wrong function?
Yes i know i can write it without .
but i want it with it.
Here is the flow i am trying to write.
Flow
[{"id":"d0bcb4c3.860368","type":"inject","z":"f841627a.c1e92","name":"","topic":"","payload":"ON","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":150,"y":100,"wires":[["6d88067f.f655e8"]]},{"id":"6a2a8908.b6a448","type":"inject","z":"f841627a.c1e92","name":"","topic":"","payload":"OFF","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":150,"y":140,"wires":[["6d88067f.f655e8"]]},{"id":"a7fa44ca.2f3708","type":"comment","z":"f841627a.c1e92","name":"Floorheat","info":"","x":100,"y":60,"wires":[]},{"id":"5ca1fa09.178854","type":"power-monitor","z":"f841627a.c1e92","name":"VKgang","threshold":0,"startafter":1,"stopafter":1,"x":400,"y":120,"wires":[[],["5041b7d7.f4d068","7a26ef14.03737"]]},{"id":"6d88067f.f655e8","type":"function","z":"f841627a.c1e92","name":"Watt","func":"if (msg.payload == \"ON\"){\n msg.payload = 5000\n}\n else {\n msg.payload = 0\n }\nreturn msg;","outputs":1,"noerr":0,"x":270,"y":120,"wires":[["5ca1fa09.178854"]]},{"id":"5041b7d7.f4d068","type":"debug","z":"f841627a.c1e92","name":"Stop event","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","x":570,"y":180,"wires":[]},{"id":"7a26ef14.03737","type":"function","z":"f841627a.c1e92","name":"Save","func":"//Get global kwh\nTotkWh = global.get(\"power.VK.gang.kWh.Total\")||0;\nDaykWh = global.get(\"power.VK.gang.kWh.Day\")||0;\nWeekkWh = global.get(\"power.VK.gang.kWh.Week\")||0;\nMonthkWh = global.get(\"power.VK.gang.kWh.Month\")||0;\nYearkWh = global.get(\"power.VK.gang.kWh.Year\")||0;\n\n//Calculate and add new power usage to kWh\nTotnewkwh = TotkWh + msg.payload.energy;\nDaynewkwh = DaykWh + msg.payload.energy;\nWeeknewkwh = WeekkWh + msg.payload.energy;\nMonthnewkwh = MonthkWh + msg.payload.energy;\nYearnewkwh = YearkWh + msg.payload.energy;\n\n//Save newkwh to global\nglobal.set(\"power.VK.gang.kWh.Total\",Totnewkwh);\nglobal.set(\"power.VK.gang.kWh.Day\",Daynewkwh);\nglobal.set(\"power.VK.gang.kWh.Week\",Weeknewkwh);\nglobal.set(\"power.VK.gang.kWh.Month\",Monthnewkwh);\nglobal.set(\"power.VK.gang.kWh.Year\",Yearnewkwh);\n\nreturn msg;","outputs":1,"noerr":0,"x":550,"y":120,"wires":[["2a7a781b.971438"]]},{"id":"2a7a781b.971438","type":"debug","z":"f841627a.c1e92","name":"Saved","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","x":690,"y":120,"wires":[]}]
It is for calculating the accumulated power use of my floorheat cables. Using the node-red-contrib-power-monitor
node. I get an input event when it goes off and on and i know the Watt for the cable.
Thanks.