# Counter works just once

**URL:** <https://discourse.nodered.org/t/counter-works-just-once/17176>\
**Category:** General\
**Created:** [26 October 2019 18:53 UTC](https://discourse.nodered.org/t/counter-works-just-once/17176 "2019-10-26T18:53:08Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![NightDragon](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/nightdragon/32/13604_2.png) [@NightDragon](https://discourse.nodered.org/u/NightDragon)\
**Post date:** [26 October 2019 18:53 UTC](https://discourse.nodered.org/t/counter-works-just-once/17176/1 "2019-10-26T18:53:08Z")

</div>

Hi all,

I created a UI with a button. On click a counter shall increment from 0 to 50 and back down to 0 (let's don't mind if i just count down to 1 or whatever...)

When i press the Button the first time everything works as expected. Once the flow had run, a click at the Button doesn't lead to a count up and down anymore. instead it jumps from 0 to 50 and 0 without counting.  
I'm sure, as a Node Red Beginner, I may have forgotten or overseen something fundamentally.

Any idea what i do wrong?  
`[{"id":"2f8e61c6.63fafe","type":"tab","label":"Flow 2","disabled":false,"info":""},{"id":"ea20a537.730628","type":"while-loop","z":"2f8e61c6.63fafe","name":"","condi":"msg.count >= 50","x":610,"y":380,"wires":[["4c48d2b.208c8ac"],["bf53317d.c3bca8"]]},{"id":"bf53317d.c3bca8","type":"while-loop","z":"2f8e61c6.63fafe","name":"","condi":"msg.count <= 1","x":880,"y":640,"wires":[["e2647dd2.92887"],["686dd119.fb1ec8"]]},{"id":"357d21ff.2c4ede","type":"change","z":"2f8e61c6.63fafe","name":"","rules":[{"t":"set","p":"count","pt":"msg","to":"0","tot":"num"}],"action":"","property":"","from":"","to":"","reg":false,"x":380,"y":380,"wires":[["ea20a537.730628"]]},{"id":"3ba367d6.3496a","type":"ui_button","z":"2f8e61c6.63fafe","name":"b_test2","group":"a36c25d9.73a538","order":1,"width":0,"height":0,"passthru":false,"label":"test Alarm","tooltip":"","color":"","bgcolor":"","icon":"","payload":"","payloadType":"str","topic":"","x":200,"y":380,"wires":[["357d21ff.2c4ede"]]},{"id":"4c48d2b.208c8ac","type":"counter","z":"2f8e61c6.63fafe","name":"","init":"0","step":"1","lower":"0","upper":"50","mode":"increment","outputs":"1","x":820,"y":320,"wires":[["94838359.9bfe18","9d5928c2.9b6818"]]},{"id":"e2647dd2.92887","type":"counter","z":"2f8e61c6.63fafe","name":"","init":"50","step":"1","lower":"0","upper":"50","mode":"decrement","outputs":"1","x":1080,"y":580,"wires":[["aa0b7f6b.54b55","8245314d.dc0838"]]},{"id":"94838359.9bfe18","type":"debug","z":"2f8e61c6.63fafe","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"count","targetType":"msg","x":1070,"y":340,"wires":[]},{"id":"aa0b7f6b.54b55","type":"debug","z":"2f8e61c6.63fafe","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"count","targetType":"msg","x":1350,"y":580,"wires":[]},{"id":"686dd119.fb1ec8","type":"debug","z":"2f8e61c6.63fafe","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":1190,"y":660,"wires":[]},{"id":"9d5928c2.9b6818","type":"delay","z":"2f8e61c6.63fafe","name":"","pauseType":"delay","timeout":"100","timeoutUnits":"milliseconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":660,"y":240,"wires":[["ea20a537.730628"]]},{"id":"8245314d.dc0838","type":"delay","z":"2f8e61c6.63fafe","name":"","pauseType":"delay","timeout":"100","timeoutUnits":"milliseconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":920,"y":480,"wires":[["bf53317d.c3bca8"]]},{"id":"a36c25d9.73a538","type":"ui_group","z":"","name":"Home","tab":"fd55e205.0396f8","order":1,"disp":true,"width":"6","collapse":false},{"id":"fd55e205.0396f8","type":"ui_tab","z":"","name":"Home","icon":"dashboard","order":1,"disabled":false,"hidden":false}]`

Appreciate any help,  
BR,  
Rob

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [26 October 2019 20:47 UTC](https://discourse.nodered.org/t/counter-works-just-once/17176/2 "2019-10-26T20:47:05Z")

</div>

For the flow you posted, and from what I can determine, it is because the `count` nodes are not being reset.

(I know. It took me a while to work it out too as it doesn't show in the `context` part.)

If you add an `inject` node and a `change` node and set the `change node` to:  
`msg.reset` to `true` and then connect that to the two `count` nodes, all will work.

Flow attached.

```auto
[{"id":"1769e25f.80e366","type":"delay","z":"9245b569.9f881","name":"","pauseType":"delay","timeout":"100","timeoutUnits":"milliseconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":510,"y":300,"wires":[["29342d7e.cb24ba"]]},{"id":"dedc45f6.d3c3f","type":"counter","z":"9245b569.9f881","name":"","init":"0","step":"1","lower":"0","upper":"50","mode":"increment","outputs":"1","x":740,"y":380,"wires":[["c1887c16.21b7c","1769e25f.80e366"]]},{"id":"29342d7e.cb24ba","type":"while-loop","z":"9245b569.9f881","name":"","condi":"msg.count >= 50","x":530,"y":380,"wires":[["dedc45f6.d3c3f"],["3c581622.355d3a"]]},{"id":"c1887c16.21b7c","type":"debug","z":"9245b569.9f881","name":"Counter 1","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"count","targetType":"msg","x":910,"y":380,"wires":[]},{"id":"7382287b.d8deb8","type":"change","z":"9245b569.9f881","name":"","rules":[{"t":"set","p":"reset","pt":"msg","to":"true","tot":"bool"}],"action":"","property":"","from":"","to":"","reg":false,"x":520,"y":260,"wires":[["dedc45f6.d3c3f","637d7415.3e256c"]]},{"id":"d353526b.10f908","type":"change","z":"9245b569.9f881","name":"","rules":[{"t":"set","p":"count","pt":"msg","to":"0","tot":"num"}],"action":"","property":"","from":"","to":"","reg":false,"x":220,"y":380,"wires":[["29342d7e.cb24ba"]]},{"id":"3c581622.355d3a","type":"while-loop","z":"9245b569.9f881","name":"","condi":"msg.count <= 1","x":520,"y":520,"wires":[["637d7415.3e256c"],["80540ca2.94228"]]},{"id":"5fbf9068.7c242","type":"inject","z":"9245b569.9f881","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":340,"y":260,"wires":[["7382287b.d8deb8"]]},{"id":"637d7415.3e256c","type":"counter","z":"9245b569.9f881","name":"","init":"50","step":"1","lower":"0","upper":"50","mode":"decrement","outputs":"1","x":730,"y":520,"wires":[["4eaa3847.728c88","5b1a2de.3b79354"]]},{"id":"a71092a9.33f908","type":"ui_button","z":"9245b569.9f881","name":"b_test2","group":"a37c47f0.d5982","order":1,"width":0,"height":0,"passthru":false,"label":"test Alarm","tooltip":"","color":"","bgcolor":"","icon":"","payload":"","payloadType":"str","topic":"","x":200,"y":320,"wires":[["d353526b.10f908"]]},{"id":"80540ca2.94228","type":"debug","z":"9245b569.9f881","name":"Output","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":900,"y":570,"wires":[]},{"id":"5b1a2de.3b79354","type":"delay","z":"9245b569.9f881","name":"","pauseType":"delay","timeout":"100","timeoutUnits":"milliseconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":510,"y":460,"wires":[["3c581622.355d3a"]]},{"id":"4eaa3847.728c88","type":"debug","z":"9245b569.9f881","name":"Counter 2","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"count","targetType":"msg","x":910,"y":520,"wires":[]},{"id":"a37c47f0.d5982","type":"ui_group","z":"","name":"Home","tab":"c5a1d7ad.7858","order":1,"disp":true,"width":"6","collapse":false},{"id":"c5a1d7ad.7858","type":"ui_tab","z":"","name":"Home","icon":"dashboard","order":1,"disabled":false,"hidden":false}]

```

Or!

To make it even _simpler_ - as I am guessing you want to be able to use this over and over:  
You can adjust the `delay` node as needed. It actually may not be needed. I put it in just for the sake of making sure the signals don't trip over each other.

```auto
[{"id":"a71092a9.33f908","type":"ui_button","z":"9245b569.9f881","name":"b_test2","group":"a37c47f0.d5982","order":1,"width":0,"height":0,"passthru":false,"label":"test Alarm","tooltip":"","color":"","bgcolor":"","icon":"","payload":"","payloadType":"str","topic":"","x":200,"y":280,"wires":[["7c1c1aed.d95d3c","7382287b.d8deb8"]]},{"id":"7c1c1aed.d95d3c","type":"delay","z":"9245b569.9f881","name":"","pauseType":"delay","timeout":"200","timeoutUnits":"milliseconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":210,"y":330,"wires":[["d353526b.10f908"]]},{"id":"7382287b.d8deb8","type":"change","z":"9245b569.9f881","name":"","rules":[{"t":"set","p":"reset","pt":"msg","to":"true","tot":"bool"}],"action":"","property":"","from":"","to":"","reg":false,"x":520,"y":260,"wires":[["dedc45f6.d3c3f","637d7415.3e256c"]]},{"id":"d353526b.10f908","type":"change","z":"9245b569.9f881","name":"","rules":[{"t":"set","p":"count","pt":"msg","to":"0","tot":"num"}],"action":"","property":"","from":"","to":"","reg":false,"x":220,"y":380,"wires":[["29342d7e.cb24ba"]]},{"id":"dedc45f6.d3c3f","type":"counter","z":"9245b569.9f881","name":"","init":"0","step":"1","lower":"0","upper":"50","mode":"increment","outputs":"1","x":740,"y":380,"wires":[["c1887c16.21b7c","1769e25f.80e366"]]},{"id":"637d7415.3e256c","type":"counter","z":"9245b569.9f881","name":"","init":"50","step":"1","lower":"0","upper":"50","mode":"decrement","outputs":"1","x":730,"y":520,"wires":[["4eaa3847.728c88","5b1a2de.3b79354"]]},{"id":"29342d7e.cb24ba","type":"while-loop","z":"9245b569.9f881","name":"","condi":"msg.count >= 50","x":530,"y":380,"wires":[["dedc45f6.d3c3f"],["3c581622.355d3a"]]},{"id":"c1887c16.21b7c","type":"debug","z":"9245b569.9f881","name":"Counter 1","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"count","targetType":"msg","x":910,"y":380,"wires":[]},{"id":"1769e25f.80e366","type":"delay","z":"9245b569.9f881","name":"","pauseType":"delay","timeout":"100","timeoutUnits":"milliseconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":510,"y":300,"wires":[["29342d7e.cb24ba"]]},{"id":"3c581622.355d3a","type":"while-loop","z":"9245b569.9f881","name":"","condi":"msg.count <= 1","x":520,"y":520,"wires":[["637d7415.3e256c"],["80540ca2.94228"]]},{"id":"4eaa3847.728c88","type":"debug","z":"9245b569.9f881","name":"Counter 2","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"count","targetType":"msg","x":910,"y":520,"wires":[]},{"id":"5b1a2de.3b79354","type":"delay","z":"9245b569.9f881","name":"","pauseType":"delay","timeout":"100","timeoutUnits":"milliseconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":510,"y":460,"wires":[["3c581622.355d3a"]]},{"id":"80540ca2.94228","type":"debug","z":"9245b569.9f881","name":"Output","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":900,"y":570,"wires":[]},{"id":"a37c47f0.d5982","type":"ui_group","z":"","name":"Home","tab":"c5a1d7ad.7858","order":1,"disp":true,"width":"6","collapse":false},{"id":"c5a1d7ad.7858","type":"ui_tab","z":"","name":"Home","icon":"dashboard","order":1,"disabled":false,"hidden":false}]

```

---

<div class="post-metadata">

**Author:** ![NightDragon](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/nightdragon/32/13604_2.png) [@NightDragon](https://discourse.nodered.org/u/NightDragon)\
**Post date:** [26 October 2019 22:51 UTC](https://discourse.nodered.org/t/counter-works-just-once/17176/3 "2019-10-26T22:51:37Z")

</div>

Thanks for your help!

The first flow you posted has the same issue like mine. But your second one seems to do the trick!  
Thanks a lot!

so it seems like the count node needs to receive the msg.reset directly? I thought to pass it over the others...  
Are all nodes processing a msg.reset?

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [26 October 2019 23:26 UTC](https://discourse.nodered.org/t/counter-works-just-once/17176/4 "2019-10-26T23:26:40Z")

</div>

No, as far as I know the `change` node builds the `msg.reset` message and it is sent to the `count` node.

The message ends there.

> [@NightDragon](#):
>
> so it seems like the count node needs to receive the msg.reset directly? I thought to pass it over the others...  
> Are all nodes processing a msg.reset?

I don't understand what you mean in the bigger picture.

> I thought to pass it over the others...

_it_?  
_over_?

The `change` node is connected to the `count` node.  
That (they) is (are) the node(s) which needed to be reset.

And don't worry if you get confused. I spend a lot of time in that condition.

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [28 October 2019 20:39 UTC](https://discourse.nodered.org/t/counter-works-just-once/17176/5 "2019-10-28T20:39:45Z")

</div>

Hi again.

I was thinking about the flow and as much as I get you are new to Node-Red, I would like to offer an alternative to how you did it.

I get that the `while` node is nice for what you were/are doing, it involves a _non standard_ node.

Basically the `while` node is really doing much the same as the `switch` node.

Here is another layout of your flow but using the `switch` node.

I can get you may not want to do it this way, as you have your mind set to the way you showed it.  
I really get it.

But only if for the sake of _an alternative way_, have a look.

```auto
[{"id":"4c7b4c40.8bc634","type":"ui_button","z":"9245b569.9f881","name":"b_test2","group":"69cdc785.2fad9","order":1,"width":0,"height":0,"passthru":false,"label":"test Alarm","tooltip":"","color":"","bgcolor":"","icon":"","payload":"","payloadType":"str","topic":"","x":190,"y":1600,"wires":[["5cc3e89a.fcfb3","2ff6b3da.ea5a3c"]]},{"id":"5cc3e89a.fcfb3","type":"delay","z":"9245b569.9f881","name":"","pauseType":"delay","timeout":"200","timeoutUnits":"milliseconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":200,"y":1650,"wires":[["4a379a4.3a45e64"]]},{"id":"2ff6b3da.ea5a3c","type":"change","z":"9245b569.9f881","name":"","rules":[{"t":"set","p":"reset","pt":"msg","to":"true","tot":"bool"}],"action":"","property":"","from":"","to":"","reg":false,"x":460,"y":1580,"wires":[["b9b3943f.0d9d18","d1d3af5f.5d5e3"]]},{"id":"4a379a4.3a45e64","type":"change","z":"9245b569.9f881","name":"","rules":[{"t":"set","p":"count","pt":"msg","to":"0","tot":"num"}],"action":"","property":"","from":"","to":"","reg":false,"x":210,"y":1700,"wires":[["130ad314.7ea34d"]]},{"id":"b9b3943f.0d9d18","type":"counter","z":"9245b569.9f881","name":"","init":"0","step":"1","lower":"0","upper":"50","mode":"increment","outputs":"1","x":680,"y":1700,"wires":[["24cd247e.7c97cc","b375bd76.94b7e8"]]},{"id":"24cd247e.7c97cc","type":"debug","z":"9245b569.9f881","name":"Counter 1","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"count","targetType":"msg","x":850,"y":1700,"wires":[]},{"id":"b375bd76.94b7e8","type":"delay","z":"9245b569.9f881","name":"","pauseType":"delay","timeout":"100","timeoutUnits":"milliseconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":450,"y":1620,"wires":[["130ad314.7ea34d"]]},{"id":"130ad314.7ea34d","type":"switch","z":"9245b569.9f881","name":"","property":"count","propertyType":"msg","rules":[{"t":"lt","v":"50","vt":"num"},{"t":"eq","v":"50","vt":"num"}],"checkall":"true","repair":false,"outputs":2,"x":430,"y":1700,"wires":[["b9b3943f.0d9d18"],["50f2e8fe.98597"]]},{"id":"e2649791.051248","type":"inject","z":"9245b569.9f881","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":190,"y":1560,"wires":[["2ff6b3da.ea5a3c","5cc3e89a.fcfb3"]]},{"id":"d1d3af5f.5d5e3","type":"counter","z":"9245b569.9f881","name":"","init":"50","step":"1","lower":"0","upper":"50","mode":"decrement","outputs":"1","x":670,"y":1810,"wires":[["715e2cfb.888df4","baadca36.b8cce"]]},{"id":"715e2cfb.888df4","type":"debug","z":"9245b569.9f881","name":"Counter 2","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"count","targetType":"msg","x":850,"y":1810,"wires":[]},{"id":"baadca36.b8cce","type":"delay","z":"9245b569.9f881","name":"","pauseType":"delay","timeout":"100","timeoutUnits":"milliseconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":450,"y":1760,"wires":[["50f2e8fe.98597"]]},{"id":"e4a91d41.9ac028","type":"debug","z":"9245b569.9f881","name":"Output","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":840,"y":1870,"wires":[]},{"id":"50f2e8fe.98597","type":"switch","z":"9245b569.9f881","name":"","property":"count","propertyType":"msg","rules":[{"t":"gt","v":"1","vt":"num"},{"t":"eq","v":"1","vt":"num"}],"checkall":"true","repair":false,"outputs":2,"x":430,"y":1810,"wires":[["d1d3af5f.5d5e3"],["e4a91d41.9ac028"]]},{"id":"69cdc785.2fad9","type":"ui_group","name":"Group 1","tab":"bda2d16a.570ca","order":1,"disp":true,"width":6},{"id":"bda2d16a.570ca","type":"ui_tab","name":"Tab 35","icon":"dashboard","order":35}]

```
