# Counter Resets randomly

**URL:** https://discourse.nodered.org/t/counter-resets-randomly/83505
**Category:** General
**Created:** [7 December 2023 15:32 UTC](https://discourse.nodered.org/t/counter-resets-randomly/83505 "2023-12-07T15:32:45Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![0x80000](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/0x80000/32/85610_2.png) [@0x80000](https://discourse.nodered.org/u/0x80000)
#### Post date: [7 December 2023 15:32 UTC](https://discourse.nodered.org/t/counter-resets-randomly/83505/1 "2023-12-07T15:32:45Z")

</div>

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

```auto
[{"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":[]}]

```

---

<div class="post-metadata">

### Author: ![marcus-j-davies](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/marcus-j-davies/32/103435_2.png) [@marcus-j-davies](https://discourse.nodered.org/u/marcus-j-davies)
#### Post date: [7 December 2023 15:57 UTC](https://discourse.nodered.org/t/counter-resets-randomly/83505/2 "2023-12-07T15:57:02Z")

</div>

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 🤔

---

<div class="post-metadata">

### Author: ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)
#### Post date: [7 December 2023 16:10 UTC](https://discourse.nodered.org/t/counter-resets-randomly/83505/3 "2023-12-07T16:10:47Z")

</div>

If you installed using the [recommended Debian script](https://nodered.org/docs/getting-started/raspberrypi) then you can run  
`node-red-log`  
to monitor the log and watch for restarts.

---

<div class="post-metadata">

### Author: ![dynamicdave](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dynamicdave/32/96_2.png) [@dynamicdave](https://discourse.nodered.org/u/dynamicdave)
#### Post date: [7 December 2023 19:13 UTC](https://discourse.nodered.org/t/counter-resets-randomly/83505/4 "2023-12-07T19:13:06Z")

</div>

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](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/e/b/ebd318ed518c77d8acde89f8f373d4589f425130.png)

---

<div class="post-metadata">

### Author: ![0x80000](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/0x80000/32/85610_2.png) [@0x80000](https://discourse.nodered.org/u/0x80000)
#### Post date: [8 December 2023 20:40 UTC](https://discourse.nodered.org/t/counter-resets-randomly/83505/5 "2023-12-08T20:40:10Z")

</div>

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

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)
#### Post date: [6 February 2024 20:40 UTC](https://discourse.nodered.org/t/counter-resets-randomly/83505/6 "2024-02-06T20:40:42Z")

</div>

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