# Counter loop dosen't count properly

**URL:** <https://discourse.nodered.org/t/counter-loop-dosent-count-properly/42479>\
**Category:** General\
**Created:** [11 March 2021 12:50 UTC](https://discourse.nodered.org/t/counter-loop-dosent-count-properly/42479 "2021-03-11T12:50:20Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![erikthegamer1242](https://avatars.discourse-cdn.com/v4/letter/e/edb3f5/32.png) [@erikthegamer1242](https://discourse.nodered.org/u/erikthegamer1242)\
**Post date:** [11 March 2021 12:50 UTC](https://discourse.nodered.org/t/counter-loop-dosent-count-properly/42479/1 "2021-03-11T12:50:20Z")

</div>

(counter loop used from `node-red-contrib-counter : counter`)  
I have a flow that receives mqtt data from a wifi button and forwards it(after some processing and node-red-ui data forwarding) to mqtt led strip controller. It starts counting when I press the button 10, 20 in the same keypress(not a debouncing problem), but later it goes normally 30,40,50(step is 10).  
**Here is the debug output to see that there is only one keypress:**  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/d/7/d7997ae6fe852cd7e0ef7993311f96428e7ac926.png)  
**and here is my flow in "practice":**

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/8/8/886e43f6fb25fb23d9a49220cfe554e207544064.png)  
**and here is my flow in code:**

```auto
[{"id":"a75d0d97.03efa","type":"tab","label":"LED Strip","disabled":false,"info":""},{"id":"76ef8680.d847e8","type":"mqtt out","z":"a75d0d97.03efa","name":"","topic":"cmnd/led-strip/channel","qos":"2","retain":"","broker":"c50971ce.d40a7","x":680,"y":120,"wires":[]},{"id":"504583eb.4036cc","type":"ui_slider","z":"a75d0d97.03efa","name":"","label":"Cold White","tooltip":"","group":"72500c65.fc1324","order":1,"width":2,"height":5,"passthru":true,"outs":"end","topic":"","min":0,"max":"100","step":"10","x":310,"y":80,"wires":[["76ef8680.d847e8","1cf52d6.6517ad3"]]},{"id":"f7a29225.9ff13","type":"ui_slider","z":"a75d0d97.03efa","name":"","label":"Warm White","tooltip":"","group":"72500c65.fc1324","order":2,"width":2,"height":5,"passthru":true,"outs":"end","topic":"","min":0,"max":"100","step":"10","x":310,"y":160,"wires":[["76ef8680.d847e8","1cf52d6.6517ad3"]]},{"id":"2e6cdc9d.c5e4a4","type":"ui_slider","z":"a75d0d97.03efa","name":"","label":"Both","tooltip":"","group":"72500c65.fc1324","order":3,"width":2,"height":5,"passthru":true,"outs":"end","topic":"","min":0,"max":"100","step":"10","x":130,"y":120,"wires":[["f7a29225.9ff13","504583eb.4036cc"]]},{"id":"1cf52d6.6517ad3","type":"debug","z":"a75d0d97.03efa","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":1110,"y":300,"wires":[]},{"id":"8344e048.703b5","type":"counter","z":"a75d0d97.03efa","name":"","init":"0","step":"1","lower":"0","upper":"1000","mode":"increment","outputs":2,"x":700,"y":360,"wires":[["f7a29225.9ff13"],[]]},{"id":"cd31fec4.cb3ca","type":"mqtt in","z":"a75d0d97.03efa","name":"Wifi Button","topic":"stat/wifibutton1/RESULT","qos":"2","datatype":"auto","broker":"c50971ce.d40a7","x":120,"y":360,"wires":[["af2d4f31.5691c","d40d7f2c.3f843","84952dbc.3ae8c","202e1d76.e52e02","1cf52d6.6517ad3","5dc06367.57afdc"]]},{"id":"af2d4f31.5691c","type":"function","z":"a75d0d97.03efa","name":"Turn Off","func":"if (msg.payload.includes('Button4'))\n msg.reset = true;\n return msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":300,"y":460,"wires":[["8344e048.703b5"]]},{"id":"d40d7f2c.3f843","type":"function","z":"a75d0d97.03efa","name":"Single Click (by 10)","func":"if (msg.payload.includes('{\"Button2\":{\"Action\":\"SINGLE\"}}'))\n msg.increment = 10;\n return msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":330,"y":260,"wires":[["8344e048.703b5"]]},{"id":"84952dbc.3ae8c","type":"function","z":"a75d0d97.03efa","name":"Double Click (by 50)","func":"if (msg.payload.includes('{\"Button2\":{\"Action\":\"DOUBLE\"}}'))\n msg.increment = 50;\n return msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":340,"y":300,"wires":[["8344e048.703b5"]]},{"id":"202e1d76.e52e02","type":"function","z":"a75d0d97.03efa","name":"Single Click (by -5)","func":"if (msg.payload.includes('{\"Button3\":{\"Action\":\"SINGLE\"}}'))\n msg.decrement = 5;\n return msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":330,"y":360,"wires":[["8344e048.703b5"]]},{"id":"5dc06367.57afdc","type":"function","z":"a75d0d97.03efa","name":"Double Click (by -50)","func":"if (msg.payload.includes('{\"Button3\":{\"Action\":\"DOUBLE\"}}'))\n msg.decrement = 50;\n return msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":340,"y":400,"wires":[["8344e048.703b5"]]},{"id":"362af663.43d3da","type":"alexa-home","z":"a75d0d97.03efa","conf":"be2da63c.aace38","device":"135069","acknoledge":true,"name":"LED Strip","topic":"","x":120,"y":640,"wires":[["1cf52d6.6517ad3","e0c5d1d8.a149b"]]},{"id":"3aa6ae7d.d80b82","type":"comment","z":"a75d0d97.03efa","name":"Alexa Device Config: https://alexa-node-red.bm.hardill.me.uk/devices","info":"","x":260,"y":20,"wires":[]},{"id":"e0c5d1d8.a149b","type":"switch","z":"a75d0d97.03efa","name":"","property":"command","propertyType":"msg","rules":[{"t":"eq","v":"TurnOnRequest","vt":"str"},{"t":"eq","v":"TurnOffRequest","vt":"str"},{"t":"eq","v":"SetPercentageRequest","vt":"str"}],"checkall":"true","repair":false,"outputs":3,"x":290,"y":640,"wires":[["4c1f8765.789248"],["d4e34bc8.fcfb18"],["c4471719.5fafb8"]]},{"id":"d4e34bc8.fcfb18","type":"function","z":"a75d0d97.03efa","name":"Off","func":"msg.payload = 0;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":510,"y":620,"wires":[["f7a29225.9ff13"]]},{"id":"4c1f8765.789248","type":"function","z":"a75d0d97.03efa","name":"On","func":"msg.payload = 100;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":510,"y":580,"wires":[["f7a29225.9ff13"]]},{"id":"c4471719.5fafb8","type":"function","z":"a75d0d97.03efa","name":"Brightness conversion","func":"msg.payload = msg.payload;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":560,"y":660,"wires":[["f7a29225.9ff13"]]},{"id":"c50971ce.d40a7","type":"mqtt-broker","z":"","name":"public broker","broker":"localhost","port":"1884","clientid":"","usetls":false,"compatmode":false,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","closeTopic":"","closeQos":"0","closePayload":"","willTopic":"","willQos":"0","willPayload":""},{"id":"72500c65.fc1324","type":"ui_group","z":"","name":"LED Strip","tab":"378f07b9.20e6b8","order":3,"disp":true,"width":"6","collapse":false},{"id":"be2da63c.aace38","type":"alexa-home-conf","z":"","username":"erikthegamer1242"},{"id":"378f07b9.20e6b8","type":"ui_tab","z":"","name":"Home","icon":"home","order":1,"disabled":false,"hidden":false}]

```

**EDIT:** I have found that there is a pattern to this, is goes: 10-20(together), 30(alone), 40-50, 60, 70, 80-90, 100, I have also used MQTT-Explorer to remove it being a hardware issue.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [11 March 2021 13:48 UTC](https://discourse.nodered.org/t/counter-loop-dosent-count-properly/42479/2 "2021-03-11T13:48:12Z")

</div>

In order to show that there is a problem with a node you have to look at the inputs. Connect a debug node to all the inputs to the counter and see what it shows.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [11 March 2021 14:08 UTC](https://discourse.nodered.org/t/counter-loop-dosent-count-properly/42479/3 "2021-03-11T14:08:43Z")

</div>

I suspect that the fundamental problem (which you will see when you connect up the debug node) is that you have got confused about what is inside the `if` statements in your functions. I have a rule that I never break, which is that if an `if` statement flows over mulitiple lines then I _always_ use braces. So I always use  
`if (test) statement`  
or

```auto
if (test) {
    statements
}

```

Never

```auto
if (test) 
   statement
   statement

```

So your functions should be

```auto
if (msg.payload.includes('{"Button2":{"Action":"SINGLE"}}')) {
 msg.increment = 10;
 return msg;
}

```

not

```auto
if (msg.payload.includes('{"Button2":{"Action":"SINGLE"}}'))
 msg.increment = 10;
 return msg;

```

in which the `return msg` is executed whether the test passes or not.

---

<div class="post-metadata">

**Author:** ![erikthegamer1242](https://avatars.discourse-cdn.com/v4/letter/e/edb3f5/32.png) [@erikthegamer1242](https://discourse.nodered.org/u/erikthegamer1242)\
**Post date:** [11 March 2021 14:29 UTC](https://discourse.nodered.org/t/counter-loop-dosent-count-properly/42479/4 "2021-03-11T14:29:24Z")

</div>

Yep, you were right, it now works perfectly, I was thinking of a one-line if not needing brackets, but yea the return line would also need to be in the if. Thank you soo much

---

<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:** [25 March 2021 14:29 UTC](https://discourse.nodered.org/t/counter-loop-dosent-count-properly/42479/5 "2021-03-25T14:29:52Z")

</div>

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