Counter loop dosen't count properly

(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
and here is my flow in "practice":


and here is my flow in code:

[{"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.

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.

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

if (test) {
    statements
}

Never

if (test) 
   statement
   statement

So your functions should be

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

not

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.

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

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