Strange variable values within a function

Hi,

it's about the variable SpieleraumHZGTime.
The function below is the only function where I set it (except one time a day where I reset it to 0).
It counts the time where a GPIO of a easyesp node, which is controlled by HTTP, is set to 1.

My issue is, that the variable has sometimes kind of overruns. this means it has then the value e.g. "887065226930285".
I cannot see a codepath where this may happen.
the "msg.payload.log" pathes happen on switching on or off (happens about 10 to 50 times a day, not more than every 10 seconds). the "msg.payload.state" is the result from periodically (30sec) asking the state of the GPIO.
I can see the variable in the nodered context already wrong, so it is not a MQTT issue on the other side.

Has somebody a hint for me what happens here?

Thanks. :slight_smile:

var SpieleraumHZGPulses = global.get('SpieleraumHZGPulses') || 0;
var SpieleraumHZG = global.get('SpieleraumHZG') || 0;
var SpieleraumHZGTimeStart = flow.get('SpieleraumHZGTimeStart') || 0;
var SpieleraumHZGTime = global.get('SpieleraumHZGTime') || 0;

msgswitch = {}
msgruntime = {}
msgpulses = {}
if (msg.payload.log == 'GPIO 2 Set to 1'){
    if (SpieleraumHZG === 0){
        global.set('SpieleraumHZG', 1);
    } else if (SpieleraumHZG === 2){
        global.set('SpieleraumHZG', 3);
    }
    flow.set('SpieleraumHZGTimeStart', Date.now());
    global.set('SpieleraumHZGPulses', SpieleraumHZGPulses+1);
        msgruntime.topic = 'smartswitch/Spieleraumheizung/runningtime';
        msgruntime.payload = SpieleraumHZGTime/1000;
} else if (msg.payload.log == 'GPIO 2 Set to 0'){
    if (SpieleraumHZG === 1){
        global.set('SpieleraumHZG', 0);
    } else if (SpieleraumHZG === 3){
        global.set('SpieleraumHZG', 2);
    }
    global.set('SpieleraumHZGTime', SpieleraumHZGTime + (Date.now() - SpieleraumHZGTimeStart));
    flow.set('SpieleraumHZGTimeStart', 0);
        msgruntime.topic = 'smartswitch/Spieleraumheizung/runningtime';
        msgruntime.payload = global.get('SpieleraumHZGTime')/1000;
} else if (msg.payload.state == 1) {
    msgruntime.topic = 'smartswitch/Spieleraumheizung/runningtime';
    var tmp = (SpieleraumHZGTime + (Date.now() - SpieleraumHZGTimeStart))/1000;
    if (tmp <= 0) { tmp = 0; }
    msgruntime.payload = tmp;
    if (((SpieleraumHZGTime + (Date.now() - SpieleraumHZGTimeStart))/1000) >= 60*60*3){
        timeover = {}
        timeover.topic = 'smartswitch/Spieleraumheizung/power/set';
        timeover.payload = '0';
        node.send(timeover);
    }
} else if (msg.payload.state === 0) {
    if (SpieleraumHZG !== 0 || SpieleraumHZG !== 2) {
        rectify = {}
        rectify.topic = 'smartswitch/Spieleraumheizung/power/set';
        rectify.payload = '0';
        node.send(rectify);
    }
    msgruntime.topic = 'smartswitch/Spieleraumheizung/runningtime';
    var tmp2 = (SpieleraumHZGTime + (Date.now() - SpieleraumHZGTimeStart))/1000;
    if (tmp2 <= 0) { tmp2 = 0; }
    msgruntime.payload = tmp2;
} else {
    msgruntime.topic = 'smartswitch/Spieleraumheizung/runningtime';
    msgruntime.payload = SpieleraumHZGTime/1000;
}
msgswitch.topic = 'smartswitch/Spieleraumheizung/power';
msgswitch.payload = SpieleraumHZG;
msgpulses.topic = 'smartswitch/Spieleraumheizung/pulses';
msgpulses.payload = SpieleraumHZGPulses
return [msgswitch, msgruntime, msgpulses]

here are the nodes where the affected function is attached to. but I think none of them affects the variable which gets wrong ...

[{"id":"91f24e0f.fe5f9","type":"function","z":"62182fe8.227b8","name":"SetSpieleraumHZGState","func":"var SpieleraumHZGPulses = global.get('SpieleraumHZGPulses') || 0;\nvar SpieleraumHZG = global.get('SpieleraumHZG') || 0;\nvar SpieleraumHZGTimeStart = flow.get('SpieleraumHZGTimeStart') || 0;\nvar SpieleraumHZGTime = global.get('SpieleraumHZGTime') || 0;\n\nmsgswitch = {}\nmsgruntime = {}\nmsgpulses = {}\nif (msg.payload.log == 'GPIO 2 Set to 1'){\n    if (SpieleraumHZG === 0){\n        global.set('SpieleraumHZG', 1);\n    } else if (SpieleraumHZG === 2){\n        global.set('SpieleraumHZG', 3);\n    }\n    flow.set('SpieleraumHZGTimeStart', Date.now());\n    global.set('SpieleraumHZGPulses', SpieleraumHZGPulses+1);\n        msgruntime.topic = 'smartswitch/Spieleraumheizung/runningtime';\n        msgruntime.payload = SpieleraumHZGTime/1000;\n} else if (msg.payload.log == 'GPIO 2 Set to 0'){\n    if (SpieleraumHZG === 1){\n        global.set('SpieleraumHZG', 0);\n    } else if (SpieleraumHZG === 3){\n        global.set('SpieleraumHZG', 2);\n    }\n    global.set('SpieleraumHZGTime', SpieleraumHZGTime + (Date.now() - SpieleraumHZGTimeStart));\n    flow.set('SpieleraumHZGTimeStart', 0);\n        msgruntime.topic = 'smartswitch/Spieleraumheizung/runningtime';\n        msgruntime.payload = global.get('SpieleraumHZGTime')/1000;\n} else if (msg.payload.state == 1) {\n    msgruntime.topic = 'smartswitch/Spieleraumheizung/runningtime';\n    var tmp = (SpieleraumHZGTime + (Date.now() - SpieleraumHZGTimeStart))/1000;\n    if (tmp <= 0) { tmp = 0; }\n    msgruntime.payload = tmp;\n    if (((SpieleraumHZGTime + (Date.now() - SpieleraumHZGTimeStart))/1000) >= 60*60*3){\n        timeover = {}\n        timeover.topic = 'smartswitch/Spieleraumheizung/power/set';\n        timeover.payload = '0';\n        node.send(timeover);\n    }\n} else if (msg.payload.state === 0) {\n    if (SpieleraumHZG !== 0 || SpieleraumHZG !== 2) {\n        rectify = {}\n        rectify.topic = 'smartswitch/Spieleraumheizung/power/set';\n        rectify.payload = '0';\n        node.send(rectify);\n    }\n    msgruntime.topic = 'smartswitch/Spieleraumheizung/runningtime';\n    var tmp2 = (SpieleraumHZGTime + (Date.now() - SpieleraumHZGTimeStart))/1000;\n    if (tmp2 <= 0) { tmp2 = 0; }\n    msgruntime.payload = tmp2;\n} else {\n    msgruntime.topic = 'smartswitch/Spieleraumheizung/runningtime';\n    msgruntime.payload = SpieleraumHZGTime/1000; // was: global.get...\n}\nmsgswitch.topic = 'smartswitch/Spieleraumheizung/power';\nmsgswitch.payload = SpieleraumHZG; // was: global.get...\nmsgpulses.topic = 'smartswitch/Spieleraumheizung/pulses';\nmsgpulses.payload = SpieleraumHZGPulses // was: global.get...\nreturn [msgswitch, msgruntime, msgpulses]","outputs":3,"noerr":0,"x":470,"y":620,"wires":[["b10bc0d9.f50c7","e7b3dc14.c3c77"],["b10bc0d9.f50c7","e7b3dc14.c3c77"],["b10bc0d9.f50c7","e7b3dc14.c3c77"]]},{"id":"634ecb74.719354","type":"http request","z":"62182fe8.227b8","name":"ESPEasy-KGHzgSwitch","method":"GET","ret":"obj","paytoqs":false,"url":"","tls":"","persist":true,"proxy":"","authType":"","x":470,"y":580,"wires":[["91f24e0f.fe5f9"]]},{"id":"a4cff97b.f82aa8","type":"function","z":"62182fe8.227b8","name":"MQTTtoHTTP-ESPEasy","func":"if (msg.topic == 'smartswitch/Spieleraumheizung/power/set') {\n    if (msg.payload == '0'){\n        global.set('SpieleraumHZG', 0);\n        msg.url = 'http://192.168.10.243/control?cmd=GPIO,2,0';\n    } else if (msg.payload == '1'){\n        global.set('SpieleraumHZG', 1);\n        msg.url = 'http://192.168.10.243/control?cmd=GPIO,2,1';\n    } else if (msg.payload == '2') {\n        global.set('SpieleraumHZG', 2);\n        msg.url = 'http://192.168.10.243/control?cmd=GPIO,2,0';\n    } else if (msg.payload == '3') {\n        global.set('SpieleraumHZG', 3);\n        msg.url = 'http://192.168.10.243/control?cmd=GPIO,2,1';\n    } else {\n        return null;\n    }\n} else if (msg.topic == 'smartswitch/Spieleraumheizung/power/get') {\n    msg.url = 'http://192.168.10.243/control?cmd=status,GPIO,2'\n} else {\n    return null;\n}\nreturn msg;","outputs":1,"noerr":0,"x":470,"y":520,"wires":[["634ecb74.719354"]]},{"id":"f365a47f.f16dc8","type":"inject","z":"62182fe8.227b8","name":"Man ein","topic":"smartswitch/Spieleraumheizung/power/set","payload":"3","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":230,"y":520,"wires":[["a4cff97b.f82aa8"]]},{"id":"7467738f.359c0c","type":"inject","z":"62182fe8.227b8","name":"Man aus","topic":"smartswitch/Spieleraumheizung/power/set","payload":"2","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":240,"y":560,"wires":[["a4cff97b.f82aa8"]]},{"id":"8d11274e.bcc998","type":"inject","z":"62182fe8.227b8","name":"Auto ein","topic":"smartswitch/Spieleraumheizung/power/set","payload":"1","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":100,"y":540,"wires":[["a4cff97b.f82aa8"]]},{"id":"7acc15f3.3d2e9c","type":"inject","z":"62182fe8.227b8","name":"Auto aus","topic":"smartswitch/Spieleraumheizung/power/set","payload":"0","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":100,"y":580,"wires":[["a4cff97b.f82aa8"]]},{"id":"77e21e2.3f52fe","type":"inject","z":"62182fe8.227b8","name":"Spieleraumheizung check","topic":"smartswitch/Spieleraumheizung/power/get","payload":"","payloadType":"str","repeat":"30","crontab":"","once":false,"onceDelay":0.1,"x":160,"y":620,"wires":[["a4cff97b.f82aa8"]]},{"id":"40578f98.93477","type":"change","z":"62182fe8.227b8","name":"add_\"set\"","rules":[{"t":"change","p":"topic","pt":"msg","from":"smartswitch/Spieleraumheizung/power","fromt":"str","to":"smartswitch/Spieleraumheizung/power/set","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":460,"y":480,"wires":[["a4cff97b.f82aa8"]]},{"id":"50f9cadd.53b654","type":"ui_switch","z":"62182fe8.227b8","name":"","label":"Spieleraumheizung","tooltip":"","group":"252987a.7b89478","order":2,"width":0,"height":0,"passthru":false,"decouple":"true","topic":"smartswitch/Spieleraumheizung/power","style":"","onvalue":"1","onvalueType":"str","onicon":"","oncolor":"","offvalue":"0","offvalueType":"str","officon":"","offcolor":"","x":390,"y":440,"wires":[["40578f98.93477"]]},{"id":"30e7e512.b64d6a","type":"mqtt in","z":"62182fe8.227b8","name":"","topic":"smartswitch/Spieleraumheizung/#","qos":"2","datatype":"auto","broker":"fd11ab5a.2acbb8","x":150,"y":480,"wires":[["50f9cadd.53b654","a4cff97b.f82aa8"]]},{"id":"29e66db7.8b3cf2","type":"function","z":"62182fe8.227b8","name":"KGHzgSpeedSwitcher","func":"var SpieleraumHZG = global.get('SpieleraumHZG') || 0;\n\nmsg2 = {};\n\nif (SpieleraumHZG >= 2 || global.get('SpieleraumHZGTime') >= 60*60*3*1000) {\n    return null;\n}\n\nif (msg.payload < -1500 && SpieleraumHZG !== 1 /*&& global.get('WWrunning') !== 1*/) {\n    msg2.url = 'http://192.168.10.243/control?cmd=GPIO,2,1';\n    return msg2;\n} else if (msg.payload > 100 && SpieleraumHZG !== 0) {\n    msg2.url = 'http://192.168.10.243/control?cmd=GPIO,2,0';\n    return msg2;\n} else {\n    return null;\n}","outputs":1,"noerr":0,"x":660,"y":480,"wires":[["634ecb74.719354"]]},{"id":"45b72aea.ae8544","type":"function","z":"62182fe8.227b8","name":"PV_Live","func":"var P_Grid=flow.get('P_Grid') || 0;\nvar P_Load=flow.get('P_Load') || 0;\nvar P_PV=flow.get('P_PV') || 0;\n\nmsg1 = {};\nmsg2 = {};\nmsg3 = {};\nP_Grid = parseFloat(msg.payload.Body.Data.Site.P_Grid);\nflow.set('P_Grid',P_Grid);\nP_Load = parseFloat(msg.payload.Body.Data.Site.P_Load);\nflow.set('P_Load',P_Load);\nP_PV = parseFloat(msg.payload.Body.Data.Site.P_PV);\nflow.set('P_PV',P_PV);\n\nmsg1.payload = P_Grid;\nmsg1.topic = 'P_Grid';\nmsg2.payload = P_Load;\nmsg2.topic = 'P_Load';\nmsg3.payload = P_PV;\nmsg3.topic = 'P_PV';\nreturn [msg1, msg2, msg3];","outputs":3,"noerr":0,"x":360,"y":80,"wires":[["f1e6e131.3cab4","29e66db7.8b3cf2"],["5847a9d8.d42878"],["51c353f1.72dc5c"]]},{"id":"dbbce9d2.fb7bf8","type":"http request","z":"62182fe8.227b8","name":"PowerFlowRealtimeData","method":"GET","ret":"obj","paytoqs":false,"url":"http://192.168.0.249/solar_api/v1/GetPowerFlowRealtimeData.fcgi","tls":"","persist":true,"proxy":"","authType":"","x":330,"y":40,"wires":[["45b72aea.ae8544"]]},{"id":"2cc6b667.12b11a","type":"inject","z":"62182fe8.227b8","name":"Fronius-P-Live","topic":"Fronius-P-Live","payload":"5sec","payloadType":"str","repeat":"10","crontab":"","once":false,"onceDelay":0.1,"x":120,"y":40,"wires":[["dbbce9d2.fb7bf8"]]},{"id":"252987a.7b89478","type":"ui_group","z":"","name":"House","tab":"11084d5f.9a2d73","disp":true,"width":"10","collapse":false},{"id":"fd11ab5a.2acbb8","type":"mqtt-broker","z":"","name":"","broker":"localhost","port":"1883","clientid":"","usetls":false,"compatmode":false,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","closeTopic":"","closeQos":"0","closePayload":"","willTopic":"","willQos":"0","willPayload":""},{"id":"11084d5f.9a2d73","type":"ui_tab","z":"","name":"Home","icon":"dashboard","disabled":false,"hidden":false}]

I suggest that before this line you insert the line

node.warn("SpieleraumHZGTime: " + SpieleraumHZGTime)
node.warn("SpieleraumHZGTimeStart: " + SpieleraumHZGTimeStart)

and after it

node.warn("After, SpieleraumHZGTime: " + global.get('SpieleraumHZGTime')

Then the values will appear in the side bar and you can follow what is going on.
My guess is that it is the subtraction that is causing the problem. In cases like this I have often found it safer to put into the context the integer time (Date().getTime()) rather that the Date object. It may not be that though.

thank you colin. did not know the node.warn which helped a lot.
The issue was not the Date.now(), it was the case where SpieleraumHZGTimeStart was 0 (because never started) and therefore 0 - Date.now()-0 happened.
with some "node.warn('I am here') I could find all the unwanted pathes and fix them. :slight_smile:

best regards

1 Like

Have a good read through Writing Functions : Node-RED there is lots of useful stuff there in addition to the use of node.warn() for debugging.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.