Reset counter function

Hi,

i how can i set when the number reach '10' then the next inject will be reset it and start from '0' ?

[{"id":"b45544f267d46e64","type":"tab","label":"Flow 6","disabled":false,"info":"","env":[]},{"id":"e9181172966c6842","type":"function","z":"b45544f267d46e64","name":"function 2","func":"if (msg.count == 10)\n    msg.count = true;\nelse if (msg.count == \"reset\") \n    \nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":780,"y":580,"wires":[["b98146ef.0ab288"]]},{"id":"33f1976d.907798","type":"inject","z":"b45544f267d46e64","name":"increment","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":540,"y":440,"wires":[["381f2c71.f7670c"]]},{"id":"381f2c71.f7670c","type":"counter","z":"b45544f267d46e64","name":"","init":"0","step":"1","lower":"","upper":"","mode":"increment","outputs":"1","x":700,"y":500,"wires":[["295107a1.67cc58","b98146ef.0ab288","e9181172966c6842"]]},{"id":"b98146ef.0ab288","type":"change","z":"b45544f267d46e64","name":"flow.counter = count ","rules":[{"t":"set","p":"counter","pt":"flow","to":"count","tot":"msg"},{"t":"set","p":"payload","pt":"msg","to":"count/1","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":900,"y":500,"wires":[["ea0c2a5f.fcd858"]]},{"id":"9b5e941a.b9cbf","type":"inject","z":"b45544f267d46e64","name":"reset","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"0","payloadType":"num","x":270,"y":500,"wires":[["6f0843b0.c5be9c"]]},{"id":"6f0843b0.c5be9c","type":"change","z":"b45544f267d46e64","name":"delete flow.counter, reset count","rules":[{"t":"set","p":"reset","pt":"msg","to":"true","tot":"bool"},{"t":"delete","p":"counter","pt":"flow"}],"action":"","property":"","from":"","to":"","reg":false,"x":470,"y":500,"wires":[["381f2c71.f7670c"]]},{"id":"295107a1.67cc58","type":"debug","z":"b45544f267d46e64","name":"","active":false,"tosidebar":true,"console":false,"tostatus":true,"complete":"count","targetType":"msg","x":870,"y":440,"wires":[]},{"id":"ea0c2a5f.fcd858","type":"debug","z":"b45544f267d46e64","name":"","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"count","statusType":"msg","x":1170,"y":500,"wires":[]}]

The Modulus operator is what you want: count %= 10
I don't have the "Counter" node in my Node-red palette, so this is how I would do it.

[{"id":"33f1976d.907798","type":"inject","z":"b45544f267d46e64","name":"increment","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":120,"y":120,"wires":[["f32cf5b318741bbd"]]},{"id":"9b5e941a.b9cbf","type":"inject","z":"b45544f267d46e64","name":"reset","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"0","payloadType":"num","x":110,"y":180,"wires":[["6f0843b0.c5be9c"]]},{"id":"6f0843b0.c5be9c","type":"change","z":"b45544f267d46e64","name":"","rules":[{"t":"delete","p":"counter","pt":"flow"}],"action":"","property":"","from":"","to":"","reg":false,"x":290,"y":180,"wires":[[]]},{"id":"ea0c2a5f.fcd858","type":"debug","z":"b45544f267d46e64","name":"","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"count","statusType":"msg","x":470,"y":120,"wires":[]},{"id":"f32cf5b318741bbd","type":"function","z":"b45544f267d46e64","name":"function 26","func":"let count = flow.get(\"counter\") ?? 0 // Fetch saved value\ncount += 1        // Increment\ncount %= 10       // Modulus 10\nflow.set (\"counter\", count)   // Save value\nmsg.payload = count\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":290,"y":120,"wires":[["ea0c2a5f.fcd858"]]}]

Hi @jbudd thanks for helping :innocent:

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