# Reset counter function

**URL:** <https://discourse.nodered.org/t/reset-counter-function/76160>\
**Category:** General\
**Created:** [6 March 2023 06:19 UTC](https://discourse.nodered.org/t/reset-counter-function/76160 "2023-03-06T06:19:50Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![kelvin](https://avatars.discourse-cdn.com/v4/letter/k/7ba0ec/32.png) [@kelvin](https://discourse.nodered.org/u/kelvin)\
**Post date:** [6 March 2023 06:19 UTC](https://discourse.nodered.org/t/reset-counter-function/76160/1 "2023-03-06T06:19:50Z")

</div>

Hi,

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

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

```

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [6 March 2023 06:57 UTC](https://discourse.nodered.org/t/reset-counter-function/76160/2 "2023-03-06T06:57:55Z")

</div>

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.

 ![Untitled 1](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/b/9/b9378232b33486206c882bffd3bb13690b615bc0.jpeg)

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

```

---

<div class="post-metadata">

**Author:** ![kelvin](https://avatars.discourse-cdn.com/v4/letter/k/7ba0ec/32.png) [@kelvin](https://discourse.nodered.org/u/kelvin)\
**Post date:** [6 March 2023 07:07 UTC](https://discourse.nodered.org/t/reset-counter-function/76160/3 "2023-03-06T07:07:36Z")

</div>

Hi @jbudd thanks for helping 😇

---

<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:** [5 May 2023 07:08 UTC](https://discourse.nodered.org/t/reset-counter-function/76160/4 "2023-05-05T07:08:37Z")

</div>

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