# Node-RED function counter reset and function 4 outputs

**URL:** <https://discourse.nodered.org/t/node-red-function-counter-reset-and-function-4-outputs/6593>\
**Category:** General\
**Created:** [9 January 2019 08:19 UTC](https://discourse.nodered.org/t/node-red-function-counter-reset-and-function-4-outputs/6593 "2019-01-09T08:19:02Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![IvarMvl](https://avatars.discourse-cdn.com/v4/letter/i/3e96dc/32.png) [@IvarMvl](https://discourse.nodered.org/u/IvarMvl)\
**Post date:** [9 January 2019 08:19 UTC](https://discourse.nodered.org/t/node-red-function-counter-reset-and-function-4-outputs/6593/1 "2019-01-09T08:19:02Z")

</div>

So I made a counter in Node-Red and cant figure out hot to reset the counter function when it hits a sertain number, and than to split the payload to 4 different outputs with the number from the counter.

Thanks in advance,

```
[{"id":"4d070e9f.09b0f","type":"inject","z":"e4d8382f.f4813","name":"trigger at start","topic":"","payload":"0","payloadType":"string","repeat":"","crontab":"","once":true,"x":380,"y":2960,"wires":[["8dee7ece.87a33"]]},{"id":"8dee7ece.87a33","type":"function","z":"e4d8382f.f4813","name":"counter","func":"msg.payload = parseInt(msg.payload) + 1;\nif (msg.payload == 20);\n{\n //set counter back to 1//\n}\nreturn msg;","outputs":"1","noerr":0,"x":641,"y":2962,"wires":[["d488b39a.9c3fa","ca022bfb.4577b8"]]},{"id":"d488b39a.9c3fa","type":"delay","z":"e4d8382f.f4813","name":"trigger ","pauseType":"delay","timeout":"10","timeoutUnits":"seconds","rate":"1","nbRateUnits":"","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":630,"y":2892,"wires":[["8dee7ece.87a33"]]},{"id":"afd082d7.116a58","type":"debug","z":"e4d8382f.f4813","name":"","active":true,"console":false,"complete":false,"x":1050,"y":2900,"wires":[]},{"id":"ca022bfb.4577b8","type":"function","z":"e4d8382f.f4813","name":"if statement?","func":"if (msg.payload == '1,2') \n {\n return [msg, null, null, null];\n } \nelse if (msg.payload == '3,4') \n {\n return [null, msg, null, null];\n }\nelse if (msg.payload == '5,6')\n {\n return [null, null, msg, null];\n }\nelse()\n{\n return [null, null, null, msg];\n}","outputs":4,"noerr":0,"x":850,"y":2960,"wires":[["afd082d7.116a58"],["77f6a89c.1d562"],["4aa41582.1e4da4"],["d70604f.75c3078"]]},{"id":"77f6a89c.1d562","type":"debug","z":"e4d8382f.f4813","name":"","active":true,"console":false,"complete":false,"x":1050,"y":2940,"wires":[]},{"id":"4aa41582.1e4da4","type":"debug","z":"e4d8382f.f4813","name":"","active":true,"console":false,"complete":false,"x":1050,"y":2980,"wires":[]},{"id":"d70604f.75c3078","type":"debug","z":"e4d8382f.f4813","name":"","active":true,"console":false,"complete":false,"x":1050,"y":3020,"wires":[]}]
```

---

<div class="post-metadata">

**Author:** ![ukmoose](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ukmoose/32/13_2.png) [@ukmoose](https://discourse.nodered.org/u/ukmoose)\
**Post date:** [9 January 2019 08:43 UTC](https://discourse.nodered.org/t/node-red-function-counter-reset-and-function-4-outputs/6593/2 "2019-01-09T08:43:40Z")

</div>

What have you tried to reset?

For the second bit look at the logical operators section [https://www.w3schools.com/js/js\_comparisons.asp](https://www.w3schools.com/js/js_comparisons.asp)

---

<div class="post-metadata">

**Author:** ![IvarMvl](https://avatars.discourse-cdn.com/v4/letter/i/3e96dc/32.png) [@IvarMvl](https://discourse.nodered.org/u/IvarMvl)\
**Post date:** [9 January 2019 08:50 UTC](https://discourse.nodered.org/t/node-red-function-counter-reset-and-function-4-outputs/6593/3 "2019-01-09T08:50:54Z")

</div>

thank you got the second bit sorted out.

I want the counter to start back at 1 after it hits 20 so it cant go over 20 but continiues to count

---

<div class="post-metadata">

**Author:** ![hotNipi](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/hotnipi/32/383_2.png) [@hotNipi](https://discourse.nodered.org/u/hotNipi)\
**Post date:** [9 January 2019 08:54 UTC](https://discourse.nodered.org/t/node-red-function-counter-reset-and-function-4-outputs/6593/4 "2019-01-09T08:54:59Z")

</div>

You will need to store current value in function context. In every input, read it, increase it, and store again.  
If increased value is bigger than limit (20) make it to be zero before store.

There is example of this functionality in documents: [https://nodered.org/docs/writing-functions](https://nodered.org/docs/writing-functions)

---

<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:** [9 January 2019 09:00 UTC](https://discourse.nodered.org/t/node-red-function-counter-reset-and-function-4-outputs/6593/5 "2019-01-09T09:00:48Z")

</div>

Here's a link to something similar I knocked-up for someone just before Christmas.  
It uses a number of the ideas suggested by @hotNipi.  
You will need to modify the flow to suit your particular need.

> [@Plant Factory for agriculture](https://discourse.nodered.org/t/plant-factory-for-agriculture/5891/7):
>
> Just had a few spare minutes to knock-up a quick (possible) solution using some of the basic nodes. Please note... "No FSMs were used or harmed in the making of this flow." I'm sure there are parts of the flow that can be simplified. For example, some of the functionality in the function blocks could be combined. Hope you find this useful. [{"id":"33da7582.0d26e2","type":"inject","z":"935fc4c0.e49458","name":"","topic":"","payload":"","payloadType":"num","repeat":"","crontab":"","once":tru…
