# Reset counter - help

**URL:** <https://discourse.nodered.org/t/reset-counter-help/17893>\
**Category:** General\
**Created:** [13 November 2019 13:10 UTC](https://discourse.nodered.org/t/reset-counter-help/17893 "2019-11-13T13:10:49Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ki-va](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ki-va/32/22283_2.png) [@Ki-va](https://discourse.nodered.org/u/Ki-va)\
**Post date:** [13 November 2019 13:10 UTC](https://discourse.nodered.org/t/reset-counter-help/17893/1 "2019-11-13T13:10:49Z")

</div>

I wrote a simple counter,

```auto
[{"id":"ee88e75c.aa2b98","type":"tab","label":"Flow 2","disabled":false,"info":""},{"id":"152e6284.dd693d","type":"inject","z":"ee88e75c.aa2b98","name":"","topic":"","payload":"","payloadType":"date","repeat":"1","crontab":"","once":false,"onceDelay":0.1,"x":170,"y":200,"wires":[[]]},{"id":"2cad8f34.84dce","type":"debug","z":"ee88e75c.aa2b98","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":790,"y":200,"wires":[]},{"id":"5c999071.f713f","type":"counter","z":"ee88e75c.aa2b98","name":"","init":"0","step":"1","lower":"","upper":"","mode":"increment","outputs":1,"x":580,"y":200,"wires":[["2cad8f34.84dce","847329c2.4298c8"]]},{"id":"847329c2.4298c8","type":"ui_text","z":"ee88e75c.aa2b98","group":"7a3f46eb.b30dc8","order":20,"width":0,"height":0,"name":"","label":"count=","format":"{{msg.count}}","layout":"row-left","x":800,"y":260,"wires":[]},{"id":"fa8c110d.da02e","type":"ui_button","z":"ee88e75c.aa2b98","name":"","group":"7a3f46eb.b30dc8","order":20,"width":0,"height":0,"passthru":true,"label":"button","tooltip":"","color":"","bgcolor":"","icon":"","payload":"count= 0;","payloadType":"str","topic":"","x":390,"y":200,"wires":[["5c999071.f713f"]]},{"id":"5114837f.ea1a1c","type":"switch","z":"ee88e75c.aa2b98","name":"Switch","property":"payload","propertyType":"msg","rules":[{"t":"lt","v":"5000","vt":"num"},{"t":"else"}],"checkall":"true","repair":false,"outputs":2,"x":150,"y":320,"wires":[["e174855f.3300a8","7af0bd47.ec6bb4"],[]]},{"id":"f1b9cfcd.3a17b","type":"delay","z":"ee88e75c.aa2b98","name":"","pauseType":"delay","timeout":"1","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":430,"y":540,"wires":[["e510ec8b.96cfb"]]},{"id":"e510ec8b.96cfb","type":"function","z":"ee88e75c.aa2b98","name":"increment value","func":"var value;\nvalue = parseInt(msg.payload);\nvalue = value + 1;\nmsg.payload = value;\nreturn msg;","outputs":1,"noerr":0,"x":670,"y":540,"wires":[["e034b8ef.463e08","aeca2078.e5242","4d7bbfae.23c69"]]},{"id":"72b726cc.c96898","type":"inject","z":"ee88e75c.aa2b98","name":"Demo","topic":"","payload":"0","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":270,"y":470,"wires":[["e174855f.3300a8","a6723e45.430e6"]]},{"id":"e034b8ef.463e08","type":"delay","z":"ee88e75c.aa2b98","name":"","pauseType":"rate","timeout":"5","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":440,"y":630,"wires":[["f1b9cfcd.3a17b"]]},{"id":"aeca2078.e5242","type":"debug","z":"ee88e75c.aa2b98","name":"","active":true,"console":"false","complete":"false","x":680,"y":630,"wires":[]},{"id":"e174855f.3300a8","type":"function","z":"ee88e75c.aa2b98","name":"reset other nodes","func":"msg.reset = true;\nreturn msg;","outputs":1,"noerr":0,"x":230,"y":540,"wires":[["f1b9cfcd.3a17b","e034b8ef.463e08"]]},{"id":"a6723e45.430e6","type":"delay","z":"ee88e75c.aa2b98","name":"","pauseType":"delay","timeout":"1","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":430,"y":470,"wires":[["e510ec8b.96cfb"]]},{"id":"7af0bd47.ec6bb4","type":"function","z":"ee88e75c.aa2b98","name":"set payload","func":"msg.payload = 0;\nreturn msg;","outputs":1,"noerr":0,"x":250,"y":430,"wires":[["a6723e45.430e6"]]},{"id":"3b31125b.1173ee","type":"ui_button","z":"ee88e75c.aa2b98","name":"","group":"7a3f46eb.b30dc8","order":21,"width":0,"height":0,"passthru":false,"label":"button","tooltip":"","color":"","bgcolor":"","icon":"","payload":"value=0","payloadType":"str","topic":"","x":440,"y":360,"wires":[["e510ec8b.96cfb"]]},{"id":"4d7bbfae.23c69","type":"ui_text","z":"ee88e75c.aa2b98","group":"7a3f46eb.b30dc8","order":22,"width":0,"height":0,"name":"","label":"text","format":"{{msg.payload}}","layout":"col-center","x":690,"y":440,"wires":[]},{"id":"7a3f46eb.b30dc8","type":"ui_group","z":"","name":"Sensors","tab":"83c97623.2a6ff8","disp":true,"width":"12","collapse":false},{"id":"83c97623.2a6ff8","type":"ui_tab","z":"","name":"Sensors","icon":"dashboard","disabled":false,"hidden":false}]

```

I want to reset the count to 0 whenever I press the button.  
I also need it to start automatically and start counting again from 0.

But instead every click set the counter 1 step higher. If I double click it the count will go up by 2.

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [13 November 2019 13:37 UTC](https://discourse.nodered.org/t/reset-counter-help/17893/2 "2019-11-13T13:37:56Z")

</div>

Go to your flow. Click on the counter node. Click on the info button in the right hand column. Scroll to the bottom.

What does it tell you?

---

<div class="post-metadata">

**Author:** ![Ki-va](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ki-va/32/22283_2.png) [@Ki-va](https://discourse.nodered.org/u/Ki-va)\
**Post date:** [14 November 2019 07:51 UTC](https://discourse.nodered.org/t/reset-counter-help/17893/3 "2019-11-14T07:51:41Z")

</div>

thank you that was helpful.  
I have another question: How do I stop the counter without resting it so i can watch the current count ?

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [14 November 2019 10:46 UTC](https://discourse.nodered.org/t/reset-counter-help/17893/4 "2019-11-14T10:46:18Z")

</div>

> How do I stop the counter without resting it so i can watch the current count ?

You can do it with that node, so you are going to have to code up a way to save the counter.

Maybe have two outputs from the counter: one goes to a `ui-text` node to display running count, the other goes thru some code and ends up at a `ui-text` node that is labeled "High Count"

Then have a button that would clear the reset the "High Count" to the running count. So you would need the msg going to the 'High Count' to go thru a switch that checked to see if the button was pushed to see if the value should be passed thru.

---

<div class="post-metadata">

**Author:** ![jupiter8](https://avatars.discourse-cdn.com/v4/letter/j/34f0e0/32.png) [@jupiter8](https://discourse.nodered.org/u/jupiter8)\
**Post date:** [18 November 2019 13:18 UTC](https://discourse.nodered.org/t/reset-counter-help/17893/5 "2019-11-18T13:18:03Z")

</div>

You could also try this with the excellent node-red-contrib-dsm. It work only with numbers though.

just inject the setup counter first. If you want it to persist across reboot you'll have to enable the context storage as per the docs.

```auto
[{"id":"1b0b7b04.6cfd85","type":"dsm","z":"558d3c99.18ac74","name":"Event counter","sm_config":"","x":980,"y":400,"wires":[["20aab2eb.04fe6e"]]},{"id":"d729045e.3476e8","type":"inject","z":"558d3c99.18ac74","name":"reset counter","topic":"reset","payload":"","payloadType":"str","repeat":"","crontab":"00 00 * * *","once":false,"onceDelay":0.1,"x":760,"y":440,"wires":[["1b0b7b04.6cfd85"]]},{"id":"425f611e.29359","type":"inject","z":"558d3c99.18ac74","name":"setup counter","topic":"set","payload":"{\"data\":{\"counter\":0},\"methods\":{\"reset\":[\"sm.data.counter = 0;\",\"node.send(msg);\"],\"onTransition\":[\"if (msg.payload === 1) {\",\" sm.data.counter++;\",\" msg.payload = sm.data.counter;\",\" output = false;\",\"node.send(msg);\",\"}\"],\"status\":{\"fill\":\"blue\",\"shape\":\"dot\",\"text\":{\"get\":\"sm.data.counter;\"}}}}","payloadType":"json","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":990,"y":340,"wires":[["1b0b7b04.6cfd85"]]},{"id":"20aab2eb.04fe6e","type":"debug","z":"558d3c99.18ac74","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","x":1210,"y":400,"wires":[]},{"id":"f9d3922f.a289d","type":"inject","z":"558d3c99.18ac74","name":"","topic":"","payload":"1","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":770,"y":360,"wires":[["1b0b7b04.6cfd85"]]}]

```
