# Count how many times button is pressed

**URL:** https://discourse.nodered.org/t/count-how-many-times-button-is-pressed/45708
**Category:** General
**Created:** [16 May 2021 06:03 UTC](https://discourse.nodered.org/t/count-how-many-times-button-is-pressed/45708 "2021-05-16T06:03:04Z")
**Posts on this page:** 19
**Page:** 1

<div class="post-metadata">

### Author: ![FeliceM](https://avatars.discourse-cdn.com/v4/letter/f/ec9cab/32.png) [@FeliceM](https://discourse.nodered.org/u/FeliceM)
#### Post date: [16 May 2021 06:03 UTC](https://discourse.nodered.org/t/count-how-many-times-button-is-pressed/45708/1 "2021-05-16T06:03:05Z")

</div>

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.

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

```auto
var count = msg.payload;
count++
if (count >4)
{ count==1
}
msg.payload=count
return msg;

```

I keep getting 2 message in sequence 2, 1  
 ![Schermata 2021-05-16 alle 07.56.03](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/6/c/6c98ee383629f9173d22a48ca86651b8a8e7c1df.png)

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

---

<div class="post-metadata">

### Author: ![meeki007](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/meeki007/32/12499_2.png) [@meeki007](https://discourse.nodered.org/u/meeki007)
#### Post date: [16 May 2021 06:30 UTC](https://discourse.nodered.org/t/count-how-many-times-button-is-pressed/45708/2 "2021-05-16T06:30:29Z")

</div>

Please see Context - it covers a count example.

[https://nodered.org/docs/user-guide/writing-functions#storing-data](https://nodered.org/docs/user-guide/writing-functions#storing-data)

---

<div class="post-metadata">

### Author: ![meeki007](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/meeki007/32/12499_2.png) [@meeki007](https://discourse.nodered.org/u/meeki007)
#### Post date: [16 May 2021 06:37 UTC](https://discourse.nodered.org/t/count-how-many-times-button-is-pressed/45708/3 "2021-05-16T06:37:04Z")

</div>

Can you format your pasted your export following these guidelines ?

> [@How to share code or flow json](https://discourse.nodered.org/t/how-to-share-code-or-flow-json/506):
>
> To share code samples or flow json in this forum, you need to take care to format it properly so that it is displayed correctly. The easiest way to do that is to click the 'Preformatted Text' button in the toolbar: [image] Then paste in your code or flow json. You should end up with three back-tick characters - ``` - on their own line before and after the code. On standard US English keyboards, this is on the same key as the ~ character. Using the backticks will ens…

---

<div class="post-metadata">

### Author: ![FeliceM](https://avatars.discourse-cdn.com/v4/letter/f/ec9cab/32.png) [@FeliceM](https://discourse.nodered.org/u/FeliceM)
#### Post date: [16 May 2021 07:53 UTC](https://discourse.nodered.org/t/count-how-many-times-button-is-pressed/45708/4 "2021-05-16T07:53:10Z")

</div>

I hope is correct now. Thanks for the input.

---

<div class="post-metadata">

### Author: ![FeliceM](https://avatars.discourse-cdn.com/v4/letter/f/ec9cab/32.png) [@FeliceM](https://discourse.nodered.org/u/FeliceM)
#### Post date: [16 May 2021 08:06 UTC](https://discourse.nodered.org/t/count-how-many-times-button-is-pressed/45708/5 "2021-05-16T08:06:49Z")

</div>

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:

```auto
[{"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"]]}]

```

Thanks

---

<div class="post-metadata">

### Author: ![BartButenaers](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bartbutenaers/32/10476_2.png) [@BartButenaers](https://discourse.nodered.org/u/BartButenaers)
#### Post date: [16 May 2021 08:17 UTC](https://discourse.nodered.org/t/count-how-many-times-button-is-pressed/45708/6 "2021-05-16T08:17:29Z")

</div>

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](https://nodered.org/docs/user-guide/writing-functions).  
Bart

---

<div class="post-metadata">

### Author: ![BartButenaers](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bartbutenaers/32/10476_2.png) [@BartButenaers](https://discourse.nodered.org/u/BartButenaers)
#### Post date: [16 May 2021 09:38 UTC](https://discourse.nodered.org/t/count-how-many-times-button-is-pressed/45708/7 "2021-05-16T09:38:26Z")

</div>

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

---

<div class="post-metadata">

### Author: ![FeliceM](https://avatars.discourse-cdn.com/v4/letter/f/ec9cab/32.png) [@FeliceM](https://discourse.nodered.org/u/FeliceM)
#### Post date: [16 May 2021 10:41 UTC](https://discourse.nodered.org/t/count-how-many-times-button-is-pressed/45708/8 "2021-05-16T10:41:40Z")

</div>

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.  
Thanks

---

<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: [16 May 2021 11:23 UTC](https://discourse.nodered.org/t/count-how-many-times-button-is-pressed/45708/9 "2021-05-16T11:23:57Z")

</div>

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](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/5/3/5348564bb92ffb48ba1c6b0bf861eb9c311c1c9c.png)  
and paste the results in a reply

---

<div class="post-metadata">

### Author: ![Ranki](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ranki/32/41282_2.png) [@Ranki](https://discourse.nodered.org/u/Ranki)
#### Post date: [16 May 2021 13:07 UTC](https://discourse.nodered.org/t/count-how-many-times-button-is-pressed/45708/10 "2021-05-16T13:07:35Z")

</div>

> [@FeliceM](#):
>
> ```auto
> var count = msg.payload;
> count++
> if (count >4)
> { count==1
> }
> msg.payload=count
> return msg;
> 
> ```

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):

```auto
// Get the current counter value from storage
var myCount = flow.get("count") || 0;
// Add 1 to the counter
myCount++
// 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
msg.payload=myCount
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 🙂

Cheers  
Ranki

---

<div class="post-metadata">

### Author: ![FeliceM](https://avatars.discourse-cdn.com/v4/letter/f/ec9cab/32.png) [@FeliceM](https://discourse.nodered.org/u/FeliceM)
#### Post date: [16 May 2021 13:12 UTC](https://discourse.nodered.org/t/count-how-many-times-button-is-pressed/45708/11 "2021-05-16T13:12:55Z")

</div>

Hi,  
this is the debug message I get injecting from Inject  
{"\_msgid":"df81cc0a.ac9e9","payload":1621170410439,"topic":""}

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  
2

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

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

---

<div class="post-metadata">

### Author: ![FeliceM](https://avatars.discourse-cdn.com/v4/letter/f/ec9cab/32.png) [@FeliceM](https://discourse.nodered.org/u/FeliceM)
#### Post date: [16 May 2021 13:42 UTC](https://discourse.nodered.org/t/count-how-many-times-button-is-pressed/45708/12 "2021-05-16T13:42:25Z")

</div>

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: ""

---

<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: [16 May 2021 14:07 UTC](https://discourse.nodered.org/t/count-how-many-times-button-is-pressed/45708/13 "2021-05-16T14:07:44Z")

</div>

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

---

<div class="post-metadata">

### Author: ![FeliceM](https://avatars.discourse-cdn.com/v4/letter/f/ec9cab/32.png) [@FeliceM](https://discourse.nodered.org/u/FeliceM)
#### Post date: [16 May 2021 14:41 UTC](https://discourse.nodered.org/t/count-how-many-times-button-is-pressed/45708/14 "2021-05-16T14:41:12Z")

</div>

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

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

---

<div class="post-metadata">

### Author: ![Ranki](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ranki/32/41282_2.png) [@Ranki](https://discourse.nodered.org/u/Ranki)
#### Post date: [16 May 2021 15:09 UTC](https://discourse.nodered.org/t/count-how-many-times-button-is-pressed/45708/15 "2021-05-16T15:09:09Z")

</div>

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.

Cheers  
Ranki

---

<div class="post-metadata">

### Author: ![FeliceM](https://avatars.discourse-cdn.com/v4/letter/f/ec9cab/32.png) [@FeliceM](https://discourse.nodered.org/u/FeliceM)
#### Post date: [16 May 2021 15:11 UTC](https://discourse.nodered.org/t/count-how-many-times-button-is-pressed/45708/16 "2021-05-16T15:11:35Z")

</div>

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:  
{"topic":"pi/36","payload":1,"\_msgid":"da5ef1e4.4cbd8"}  
and  
{"topic":"pi/36","payload":0,"\_msgid":"3d614ba0.14c944"}  
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.

---

<div class="post-metadata">

### Author: ![Ranki](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ranki/32/41282_2.png) [@Ranki](https://discourse.nodered.org/u/Ranki)
#### Post date: [16 May 2021 15:13 UTC](https://discourse.nodered.org/t/count-how-many-times-button-is-pressed/45708/17 "2021-05-16T15:13:08Z")

</div>

The error was `mycount == 1;` instead of `mycount = 1;` 🙂

---

<div class="post-metadata">

### Author: ![FeliceM](https://avatars.discourse-cdn.com/v4/letter/f/ec9cab/32.png) [@FeliceM](https://discourse.nodered.org/u/FeliceM)
#### Post date: [16 May 2021 15:20 UTC](https://discourse.nodered.org/t/count-how-many-times-button-is-pressed/45708/18 "2021-05-16T15:20:55Z")

</div>

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.

---

<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: [30 May 2021 15:21 UTC](https://discourse.nodered.org/t/count-how-many-times-button-is-pressed/45708/19 "2021-05-30T15:21:50Z")

</div>

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