Counter Resets randomly

Hi there, I have a problem with a function node (or node-red generally?). I have built a counter that writes the value to a variable and sends it to a database. This worked fine until a system update (Linux Debian) in May. Since then I have the problem that the counter resets after about 86 - 87s and starts from 0. I have updated Node-Red to 3.1.1. and also updated Node.JS. There has been no improvement. I have replaced the input (sensor via MQTT) with an Inject Node and the error is still present.

Node-Red Version V3.1.1
Node.JS Version 20.10.0

[{"id":"619db2f9830dc47b","type":"tab","label":"Testbereich","disabled":false,"info":"","env":[]},{"id":"97bba11763229d69","type":"inject","z":"619db2f9830dc47b","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"1","crontab":"","once":false,"onceDelay":"1","topic":"","payload":"1","payloadType":"num","x":170,"y":140,"wires":[["d38c745f87e7e686"]]},{"id":"d38c745f87e7e686","type":"switch","z":"619db2f9830dc47b","name":"","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"1","vt":"num"},{"t":"eq","v":"0","vt":"num"}],"checkall":"true","repair":false,"outputs":2,"x":350,"y":220,"wires":[["3c7fdb3f63de5e72"],[]]},{"id":"3c7fdb3f63de5e72","type":"function","z":"619db2f9830dc47b","name":"CounterMa20x","func":"//Definiere Variablen und ziehe gespeicherten Wert\n// wenn kein Wert vorhanden dann Wert = 0\nvar counterMa20x = flow.get(\"counterMa20x\") || 0\n\n// addiere 1 zum Zählwert\ncounterMa20x ++\n\n//speichere den Zähler Wert in der flow Variable\nflow.set(\"counterMa20x\", counterMa20x)\n\n//ändere msg.payload zum Zählwert\nmsg.payload = counterMa20x\n\n//dividiere durch Anzahl der Impulse\nmsg.payload = msg.payload*0.1425/4;\n//msg.payload = msg.payload*(Math.PI)/4;\n\nmsg.payload = Number(msg.payload.toFixed(2));\nreturn msg;","outputs":1,"timeout":"","noerr":0,"initialize":"","finalize":"","libs":[],"x":540,"y":220,"wires":[["2b9ff46fb78bcccf"]]},{"id":"2b9ff46fb78bcccf","type":"debug","z":"619db2f9830dc47b","name":"debug 8","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":860,"y":160,"wires":[]}]

Welcome to the forums @0x80000

Have you checked that Node RED is not restarting after 86 - 87s ?
I can't think of any reason why a flow variable will get wiped, other then Node RED restarting whilst not using the disc as context storage.

I would check logs, to see if there are any restarts of Node RED that would explain the pattern.

EDIT
And are you sure nothing else in your flow is setting/wiping out that flow variable to 0 :thinking:

If you installed using the recommended Debian script then you can run
node-red-log
to monitor the log and watch for restarts.

1 Like

I've taken your flow and run it. It works fine for me. Counts up to past 10 without any problems.
I'm using a RPi-4B running NR 3.1.0 and Node.js v18.18.2
counter_resets

Thank you all for your Help. I think I solved the problem by reinstalling node-red via the recommended Script. :slight_smile:

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