Count how many times button is pressed

Hi everyone,
I am a very beginner, don't be disgusted abut my question.
I am trying to count how many times a button is pressed.
I have setup a Rpi-gpio node on a pin and connected it to a function.

[{"id":"c74669a0.6a34f8","type":"tab","label":"Flow 2","disabled":false,"info":""},{"id":"56056d7e.0a2584","type":"rpi-gpio in","z":"c74669a0.6a34f8","name":"LCD Scrolling","pin":"22","intype":"down","debounce":"250","read":false,"x":190,"y":160,"wires":[["24df9475.386c6c"]]},{"id":"48f45062.bbd3b","type":"debug","z":"c74669a0.6a34f8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":430,"y":160,"wires":[]},{"id":"24df9475.386c6c","type":"function","z":"c74669a0.6a34f8","name":"","func":"var count = msg.payload;\ncount++\n\nif (count >4)\n{ count==0\n}\n\n\nmsg.payload=count\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":350,"y":240,"wires":[["48f45062.bbd3b"]]}]

I wrote this function but is not giving me the expected result. My expectation was to see a message incrementing from 1 to 4 and then again from 1 to 4, and so on.

var count = msg.payload;
if (count >4)
{ count==1
return msg;

I keep getting 2 message in sequence 2, 1
Schermata 2021-05-16 alle 07.56.03

What I expect to get is: 1, 2, 3, 4 ... 1

I am not sure about what is wrong. Any help will be appreciated. Thank you

Please see Context - it covers a count example.

Can you format your pasted your export following these guidelines ?

I hope is correct now. Thanks for the input.

Thanks for pointing me to nodered resources. However I am not coming right. There is something I do not understand with the output of the Rpi-Gpio node.
I have added a change function to it with the aim of getting out a timestamp and manage from there.
The issue is that the change function doesn't send a timestamp when receiving a message from the Rpi-Gpio node. Looks like the function is passing exactly the same message received from Rpi-Gpio as is. On the contrary when injecting something else it issue a timestamp as expected.
I think I need to understand what the problem is with the Rpi-Gpio node first.
Why the change function doesn't send a time stamp when receiving message from Rpi-Gpio?

Here is the node I am testing:

[{"id":"c74669a0.6a34f8","type":"tab","label":"Flow 2","disabled":false,"info":""},{"id":"988afc3c.a08f98","type":"debug","z":"c74669a0.6a34f8","name":"message","active":true,"console":"false","complete":"true","x":740,"y":320,"wires":[]},{"id":"63d18e50.889e8","type":"inject","z":"c74669a0.6a34f8","name":"Test Injecting","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"whatever","payloadType":"str","x":150,"y":360,"wires":[["b026ca5.fb7fa38"]]},{"id":"b026ca5.fb7fa38","type":"change","z":"c74669a0.6a34f8","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"","tot":"date"}],"action":"","property":"","from":"","to":"","reg":false,"x":550,"y":320,"wires":[["988afc3c.a08f98"]]},{"id":"f098a7c2.2633b8","type":"rpi-gpio in","z":"c74669a0.6a34f8","name":"","pin":"22","intype":"down","debounce":"800","read":false,"x":170,"y":320,"wires":[["b026ca5.fb7fa38"]]}]


Hi Felice,
I have not tested your code, but at first sight I think it goes like this:

  1. Gpio=0 so count=0+1=1
  2. Gpio=1 so count=1+1=2
  3. Gpio=0 so count=0+1=1
  4. Gpio=1 so count=1+1=2
  5. ...

You need to store the counter in context memory. See the "storing data" section here.

Btw don't hesitate to ask if you are stuck! But it can never harm to think about it yourself first :wink:

Hi Bart,
Thanks. I am trying to put some effort on my side. However, before I start counting I need to solve the challenge explained in my answer to meeki007 just above your post. If I don't get that right the messaage coming from Gpio goes all the way trough. I don't know why.

attach a debug node (set to display the complete msg object) to the output of the rpi-gpio-in node and copy the debug output
Screen Shot 2021-05-16 at 7.23.04 AM
and paste the results in a reply

Hello @FeliceM,

I agree that it would help to have a debug output from your GPIO. Nevertheless concerning your example to count I would like to give you an example, which should take in mind, what @meeki007 mentioned. You have to store the current value of the counter, otherwise the count will always start with the value in your payload and only put 1 more. Indeed you do not need a special payload as you - if I understood you well - only want to count the events (number of outputs):

// Get the current counter value from storage
var myCount = flow.get("count") || 0;
// Add 1 to the counter
// Check, if 4 times is passed and reset the counter to 1
if (myCount >4)
  myCount=1 // EDITED: was «myCount==1» before
// write back the current counter value to storage
flow.set("count", myCount);
// Prepare and return msg
return msg;

BTW: Maybe it does make sense to include also a node.warn(msg.payload); to see, what arrives in the node before calculating with it :slight_smile:


this is the debug message I get injecting from Inject

and these are the debug outputs I get from Rpi-Gpio-gpio when pressing the button:

5/16/2021, 3:09:19 PMnode: d7338736.b648c8
pi/22 : msg.payload : number

5/16/2021, 3:09:20 PMnode: d7338736.b648c8
pi/22 : msg.payload : number

[{"id":"c74669a0.6a34f8","type":"tab","label":"Flow 2","disabled":false,"info":""},{"id":"988afc3c.a08f98","type":"debug","z":"c74669a0.6a34f8","name":"message","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":620,"y":440,"wires":[]},{"id":"63d18e50.889e8","type":"inject","z":"c74669a0.6a34f8","name":"Test Injecting","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"whatever","payloadType":"str","x":210,"y":420,"wires":[["b026ca5.fb7fa38"]]},{"id":"b026ca5.fb7fa38","type":"change","z":"c74669a0.6a34f8","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"","tot":"date"}],"action":"","property":"","from":"","to":"","reg":false,"x":550,"y":320,"wires":[["988afc3c.a08f98"]]},{"id":"f098a7c2.2633b8","type":"rpi-gpio in","z":"c74669a0.6a34f8","name":"","pin":"22","intype":"down","debounce":"800","read":false,"x":170,"y":320,"wires":[["b026ca5.fb7fa38"]]}]

Since the change function is set to output a timestamp, I was expecting to get the exactly same message on the debug.

Hi Ranki,

thanks a lot for the code. After being pointed to the documentation by @meeki007 I did try something similar and then is when I experienced the issue described in my other question. I tested also your code and it counts perfectly if I inject a timestamp. However if I inject from gpio it doesn't work. See my reply to @zenofmud. Same with your code and funny the limit to 4 doesn't work.
_msgid: "40846c77.65ba94"
payload: 7
topic: ""

Did you set the debug node - on the output of the gpoi node - to display the complete msg object?

Hi Paul,
yes. I gave it above:
5/16/2021, 3:09:19 PMnode: d7338736.b648c8
pi/22 : msg.payload : number

5/16/2021, 3:09:20 PMnode: d7338736.b648c8
pi/22 : msg.payload : number

Hi @FeliceM ,

oh, yes; there was a mistake and I corrected it in my post. Concerning the other issue, I do not see, why it should behave in a different way, if the payload changes, because the function is independent from the payload it receives - as it counts only the number of events (payloads received).

Can you export your flow again? The one I saw above only changes the msg.payload to a timestamp, if a payload is received.


I have changed the gpio pin, rebooted everything and seems it is now behaving as expected. It is a mystery I cannot explain.

So, the output of the gpio now is:
the two messages are posted together every time the button is pressed.
Still a mystery for me why the function written by Ranky doesn't stop the count above 4.

The error was mycount == 1; instead of mycount = 1; :slight_smile:

1 Like

Hi @Ranki.
it was so small that I also didn't see it. Thanks a million.
At this point looks like it is all working.
And thanks also to the other people.

1 Like

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