Node-RED function counter reset and function 4 outputs

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

What have you tried to reset?

For the second bit look at the logical operators section https://www.w3schools.com/js/js_comparisons.asp

1 Like

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

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

1 Like

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.