# "Buttons", multi-press detection and problems with flow

**URL:** <https://discourse.nodered.org/t/buttons-multi-press-detection-and-problems-with-flow/23128>\
**Category:** General\
**Created:** [15 March 2020 04:28 UTC](https://discourse.nodered.org/t/buttons-multi-press-detection-and-problems-with-flow/23128 "2020-03-15T04:28:18Z")\
**Posts on this page:** 14\
**Page:** 1

<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:** [15 March 2020 04:28 UTC](https://discourse.nodered.org/t/buttons-multi-press-detection-and-problems-with-flow/23128/1 "2020-03-15T04:28:18Z")

</div>

(For reasons unknown to me, I'm looking at **trying** to get multiple press detection of a button)

The button can't be the usual one, but one which was shown to me recently as I need two messages from it to detect it being pressed and released. Or that's how I am doing it for now.

(Extra node used: `node-red-contrib-counter`)

Flow (picture):

 ![Screenshot from 2020-03-15 15-18-32](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/e/b/eb67938393ebd4a5bae891b602cec508bca3e944.png)

Code:

```auto
[{"id":"a3f9e42f.70b308","type":"trigger","z":"a941e919.4a7d88","op1":"1","op2":"0","op1type":"str","op2type":"str","duration":"-250","extend":false,"units":"ms","reset":"up","bytopic":"all","name":"Repeate","x":360,"y":571,"wires":[["f686a5c1.12ef3"]]},{"id":"fe868a29.aa1c58","type":"ui_template","z":"a941e919.4a7d88","group":"15a0e304.1a21ad","name":"Button","order":6,"width":"5","height":1,"format":"<button ng-mousedown=\"send({payload: 'down'})\" ng-mouseup=\"send({payload: 'up'})\"><i class=\"material-icons\"> PRESS </i></button>\n","storeOutMessages":true,"fwdInMessages":true,"templateScope":"local","x":160,"y":531,"wires":[["a3f9e42f.70b308","28d60a04.2aab4e"]]},{"id":"f686a5c1.12ef3","type":"debug","z":"a941e919.4a7d88","name":"Button presses","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":690,"y":571,"wires":[]},{"id":"28d60a04.2aab4e","type":"switch","z":"a941e919.4a7d88","name":"up","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"up","vt":"str"}],"checkall":"true","repair":false,"outputs":1,"x":350,"y":531,"wires":[["1db43ee9.553791","835f76d9.f0e3"]]},{"id":"1db43ee9.553791","type":"counter","z":"a941e919.4a7d88","name":"","init":"0","step":"1","lower":null,"upper":null,"mode":"increment","outputs":"1","x":490,"y":531,"wires":[["caa43354.3368","9cbb5de4.cc3e68"]]},{"id":"835f76d9.f0e3","type":"debug","z":"a941e919.4a7d88","name":"Up","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":440,"y":481,"wires":[]},{"id":"caa43354.3368","type":"debug","z":"a941e919.4a7d88","name":"Counter","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"count","targetType":"msg","x":630,"y":481,"wires":[]},{"id":"87140ed2.a7fb98","type":"change","z":"a941e919.4a7d88","name":"Reset","rules":[{"t":"set","p":"reset","pt":"msg","to":"true","tot":"bool"}],"action":"","property":"","from":"","to":"","reg":false,"x":520,"y":421,"wires":[["1db43ee9.553791"]]},{"id":"9cbb5de4.cc3e68","type":"trigger","z":"a941e919.4a7d88","op1":"","op2":"","op1type":"nul","op2type":"payl","duration":"300","extend":false,"units":"ms","reset":"","bytopic":"all","name":"","x":690,"y":531,"wires":[["f4123919.3c7a38"]]},{"id":"4d5b33f4.1c16bc","type":"inject","z":"a941e919.4a7d88","name":"","topic":"","payload":"foo","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":380,"y":421,"wires":[["87140ed2.a7fb98"]]},{"id":"f4123919.3c7a38","type":"debug","z":"a941e919.4a7d88","name":"Counter 1","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"count","targetType":"msg","x":860,"y":481,"wires":[]},{"id":"15a0e304.1a21ad","type":"ui_group","name":"Group 1","tab":"e6a111dc.7a3e8","order":1,"disp":true,"width":6},{"id":"e6a111dc.7a3e8","type":"ui_tab","z":"","name":"WOL Buttons","icon":"dashboard","order":46,"disabled":false,"hidden":false}]

```

Idea:  
_Button_ is pressed. It sends a `down` message. When released it sends a `up` message.  
(Not rocket science at this stage)

Forgetting the lower part:  
Only the `up` signals are passed.  
The number are counted (by the `counter` node) then it is sent to the `trigger` node.  
Why?  
Well, reading what is says - and how I set it - when it receives a message; it sends nothing.  
It then waits 300 ms and then sends the **last received message**  
So, the thinking is:  
As I am pressing the button, a stream of incrementing numbers is sent and after 300ms the last (highest) is on sent.

It isn't working.

When I reset the `counter` node the counter is reset to `0`.  
Pressing the button 3 times I get a `1` and then a `3` from the `counter 1` debug node.

Looking at the debug window (right side) I see the three `up` messages and I see things looking right, but something is illuding (?speling?) me.

Have I missed any elephants? (I usually do that now and then)

---

<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:** [15 March 2020 05:05 UTC](https://discourse.nodered.org/t/buttons-multi-press-detection-and-problems-with-flow/23128/2 "2020-03-15T05:05:25Z")

</div>

After a bit more messing around, I am even more confused.

(New flow)

 ![Screenshot from 2020-03-15 15-50-35](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/c/a/ca50305bc9edd4f96e2ea06d0733b602df8da36d.png)

_What's happened/happening?_

Ok, the two `trigger` nodes (at the top of the flow: _Detected_ and _Reset_) are there for seeing what is going through the flow.  
There is a problem there.

Given _all is good_ and things are reset:  
The `up` message goes through the `switch` node. (First thing seen at the right)  
That goes up and increments the `counter` node (above it) and that nodes sends a `1` to the next `switch` node. (See blue line)

That goes into the `change` node to generate a reset signal to the other `counter` node - I'll get to that shortly - you can see the message (blue line) going out of the `switch` node and into the `change` node. It also _triggers_ the `trigger` node called _detected_.

From the `change` node there are two _debug_ nodes: an actual debug node and another `trigger` node. That one hasn't been _triggered_.... Why?  
You can see the `msg` coming out of the `switch` node.  
And the `debug` node isn't showing any message arriving.

From before - the "I'll get to that shorly part:  
This is the only _work around_ that I can get to reset the _flow_ when it is invoked.  
Yes, I will lose one of the clicks, but that can be got around with code. (I'll just increment the final count by one.)  
I left the `inject` node there, now called: `(old)`

Yes, I still need to get the _first_ reset working (the one called `foo`), but for now: it can wait.

So, I now kind of have a couple of problems I can't understand.  
Yeah, I know: Break it down and see what is happening at a less complex level.  
That's why I left the (old) `inject` node there.  
I press it and don't get an output from the `reset` (change) node.  
So I think I have that base covered.

Oh, sorry: The code:

```auto
[{"id":"679e44c.740e43c","type":"switch","z":"a941e919.4a7d88","name":"1","property":"count","propertyType":"msg","rules":[{"t":"eq","v":"1","vt":"num"}],"checkall":"true","repair":false,"outputs":1,"x":360,"y":410,"wires":[["87140ed2.a7fb98","af04bf80.9a63f","cb9dba45.fa879"]]},{"id":"da08efba.e1dde","type":"counter","z":"a941e919.4a7d88","name":"","init":"0","step":"1","lower":null,"upper":null,"mode":"increment","outputs":"1","x":360,"y":450,"wires":[["679e44c.740e43c"]]},{"id":"87140ed2.a7fb98","type":"change","z":"a941e919.4a7d88","name":"Reset","rules":[{"t":"set","p":"reset","pt":"msg","to":"true","tot":"bool"}],"action":"","property":"","from":"","to":"","reg":false,"x":500,"y":410,"wires":[["1db43ee9.553791","6e6cec93.d69c0c","639d235d.52bc2c"]]},{"id":"af04bf80.9a63f","type":"trigger","z":"a941e919.4a7d88","op1":"1","op2":"0","op1type":"str","op2type":"str","duration":"60","extend":false,"units":"s","reset":"","bytopic":"all","name":"Detected","x":520,"y":270,"wires":[[]]},{"id":"cb9dba45.fa879","type":"debug","z":"a941e919.4a7d88","name":"XXXX Counter","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"count","targetType":"msg","x":540,"y":230,"wires":[]},{"id":"28d60a04.2aab4e","type":"switch","z":"a941e919.4a7d88","name":"up","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"up","vt":"str"}],"checkall":"true","repair":false,"outputs":1,"x":350,"y":531,"wires":[["1db43ee9.553791","835f76d9.f0e3","da08efba.e1dde"]]},{"id":"2ec5b0c8.e1958","type":"change","z":"a941e919.4a7d88","name":"Reset","rules":[{"t":"set","p":"reset","pt":"msg","to":"true","tot":"bool"}],"action":"","property":"","from":"","to":"","reg":false,"x":200,"y":450,"wires":[["da08efba.e1dde"]]},{"id":"4d5b33f4.1c16bc","type":"inject","z":"a941e919.4a7d88","name":"(Old)","topic":"","payload":"foo","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":360,"y":370,"wires":[["87140ed2.a7fb98"]]},{"id":"1db43ee9.553791","type":"counter","z":"a941e919.4a7d88","name":"","init":"0","step":"1","lower":null,"upper":null,"mode":"increment","outputs":"1","x":550,"y":531,"wires":[["caa43354.3368","9cbb5de4.cc3e68"]]},{"id":"6e6cec93.d69c0c","type":"trigger","z":"a941e919.4a7d88","op1":"1","op2":"0","op1type":"str","op2type":"str","duration":"10","extend":false,"units":"s","reset":"","bytopic":"all","name":"Reset","x":640,"y":320,"wires":[[]]},{"id":"639d235d.52bc2c","type":"debug","z":"a941e919.4a7d88","name":"Out of reset node","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":680,"y":360,"wires":[]},{"id":"fe868a29.aa1c58","type":"ui_template","z":"a941e919.4a7d88","group":"15a0e304.1a21ad","name":"Button","order":6,"width":"5","height":1,"format":"<button ng-mousedown=\"send({payload: 'down'})\" ng-mouseup=\"send({payload: 'up'})\"><i class=\"material-icons\"> PRESS </i></button>\n","storeOutMessages":true,"fwdInMessages":true,"templateScope":"local","x":160,"y":531,"wires":[["a3f9e42f.70b308","28d60a04.2aab4e"]]},{"id":"835f76d9.f0e3","type":"debug","z":"a941e919.4a7d88","name":"Up","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":350,"y":490,"wires":[]},{"id":"79554e44.816978","type":"inject","z":"a941e919.4a7d88","name":"","topic":"","payload":"foo","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":110,"y":400,"wires":[["2ec5b0c8.e1958"]]},{"id":"caa43354.3368","type":"debug","z":"a941e919.4a7d88","name":"Counter","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"count","targetType":"msg","x":690,"y":481,"wires":[]},{"id":"9cbb5de4.cc3e68","type":"trigger","z":"a941e919.4a7d88","op1":"","op2":"","op1type":"nul","op2type":"payl","duration":"500","extend":false,"units":"ms","reset":"","bytopic":"all","name":"","x":750,"y":531,"wires":[["f4123919.3c7a38"]]},{"id":"a3f9e42f.70b308","type":"trigger","z":"a941e919.4a7d88","op1":"1","op2":"0","op1type":"str","op2type":"str","duration":"-250","extend":false,"units":"ms","reset":"up","bytopic":"all","name":"Repeate","x":360,"y":571,"wires":[["f686a5c1.12ef3"]]},{"id":"f4123919.3c7a38","type":"debug","z":"a941e919.4a7d88","name":"Counter 1","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"count","targetType":"msg","x":920,"y":481,"wires":[]},{"id":"f686a5c1.12ef3","type":"debug","z":"a941e919.4a7d88","name":"Button presses","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":630,"y":571,"wires":[]},{"id":"15a0e304.1a21ad","type":"ui_group","name":"Group 1","tab":"e6a111dc.7a3e8","order":1,"disp":true,"width":6},{"id":"e6a111dc.7a3e8","type":"ui_tab","z":"","name":"WOL Buttons","icon":"dashboard","order":46,"disabled":false,"hidden":false}]

```

And a bit more:  
I up-ed the last `trigger` node to 1000ms to allow any _delay_ (lag?) getting the message through.

So, you can't see the `up` node, but you can see the messages (blue) coming through.  
Then you see the counts of how many times it was pressed (minus 1) in light blue.  
But the final node only gets a `1` (red)

The times are underlined in red and it all happens in less than a second, so that shouldn't be a problem.

 ![Screenshot from 2020-03-15 16-17-06](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/8/1/81024ba250a30080c389397b09f06143f3d44501.png)

---

<div class="post-metadata">

**Author:** ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)\
**Post date:** [15 March 2020 06:02 UTC](https://discourse.nodered.org/t/buttons-multi-press-detection-and-problems-with-flow/23128/3 "2020-03-15T06:02:20Z")

</div>

Lots of text, I am trying to extract your problem:

> [@Trying\_to\_learn](#):
>
> I'm looking at **trying** to get multiple press detection of a button

How many ?

~~edit it is too early here~~

---

<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:** [15 March 2020 06:39 UTC](https://discourse.nodered.org/t/buttons-multi-press-detection-and-problems-with-flow/23128/4 "2020-03-15T06:39:09Z")

</div>

![Screenshot from 2020-03-15 17-35-18](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/c/5/c5cd8e92da808e7e92b64b4cdc4a3b4046b6ef8d.png)

To me that means:

On receipt of first message it will send: `nothing`  
It will wait 500ms  
Then send `the latest msg object`  
That will be done for `all messages`

So that is what I want - or how I understand it.

It is getting messages coming in.

It ignores the first one, waits 500ms and sends the last one it got in that time period.

Where am I not understanding?  
(ok, I didn't use the correct highlighting for the things being sent. Please read around that as it is not really the problem.)

---

<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:** [15 March 2020 07:01 UTC](https://discourse.nodered.org/t/buttons-multi-press-detection-and-problems-with-flow/23128/5 "2020-03-15T07:01:36Z")

</div>

This is another flow I made to test the `trigger` node:

```auto
[{"id":"4190a1b5.8a64","type":"moment","z":"a941e919.4a7d88","name":"","topic":"","input":"payload","inputType":"msg","inTz":"Australia/Sydney","adjAmount":0,"adjType":"days","adjDir":"add","format":"HH:mm:ss","locale":"en_AU","output":"payload","outputType":"msg","outTz":"Australia/Sydney","x":330,"y":1070,"wires":[["c63e84df.3d0de8","42cb4221.b258cc"]]},{"id":"baa93534.30216","type":"inject","z":"a941e919.4a7d88","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":120,"y":1070,"wires":[["4190a1b5.8a64"]]},{"id":"c63e84df.3d0de8","type":"trigger","z":"a941e919.4a7d88","op1":"","op2":"","op1type":"nul","op2type":"payl","duration":"10","extend":false,"units":"s","reset":"","bytopic":"all","name":"","x":360,"y":1130,"wires":[["197a1c12.79234c"]]},{"id":"42cb4221.b258cc","type":"debug","z":"a941e919.4a7d88","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":540,"y":1070,"wires":[]},{"id":"197a1c12.79234c","type":"debug","z":"a941e919.4a7d88","name":" ***********LOOK FOR THIS**************","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":620,"y":1130,"wires":[]}]

```

Look at the picture and it shows that it is holding the messages and only showing the last one received:

 ![Screenshot from 2020-03-15 17-59-12](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/1/8/1817cb00c0dfbddea87f34bdd115f689fcf269f1.png)

---

<div class="post-metadata">

**Author:** ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)\
**Post date:** [15 March 2020 07:31 UTC](https://discourse.nodered.org/t/buttons-multi-press-detection-and-problems-with-flow/23128/6 "2020-03-15T07:31:14Z")

</div>

Isn’t this distracting from your topic?

You want to detect multiple presses, how many?

---

<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:** [15 March 2020 07:33 UTC](https://discourse.nodered.org/t/buttons-multi-press-detection-and-problems-with-flow/23128/7 "2020-03-15T07:33:30Z")

</div>

No, it shows that my thoughts of what the `trigger` node does.

That is used to count how many presses in a given time happen.

Maybe up to 3.

---

<div class="post-metadata">

**Author:** ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)\
**Post date:** [15 March 2020 07:52 UTC](https://discourse.nodered.org/t/buttons-multi-press-detection-and-problems-with-flow/23128/8 "2020-03-15T07:52:43Z")

</div>

Trigger node receives message, applies the settings/time ignores additional messages within that timeframe, unless “extend” has been checked and/or it handles individual topics.

---

<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:** [15 March 2020 07:54 UTC](https://discourse.nodered.org/t/buttons-multi-press-detection-and-problems-with-flow/23128/9 "2020-03-15T07:54:01Z")

</div>

So why don't the first couple of flows work?

---

<div class="post-metadata">

**Author:** ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)\
**Post date:** [15 March 2020 07:56 UTC](https://discourse.nodered.org/t/buttons-multi-press-detection-and-problems-with-flow/23128/10 "2020-03-15T07:56:29Z")

</div>

Because the “presses” need to happen within that timeframe? I am at a loss what you are trying to achieve: you work with a timeframe + number of presses?

---

<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:** [15 March 2020 07:59 UTC](https://discourse.nodered.org/t/buttons-multi-press-detection-and-problems-with-flow/23128/11 "2020-03-15T07:59:16Z")

</div>

> [@bakman2](#):
>
> you work with a timeframe + number of presses

Correct.

I have/had it set to count how many presses in .5 seconds. (3 presses. Not impossible.)  
Though I think I extended it to longer time and always got/get a 1 as a result/output.

---

<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:** [15 March 2020 08:11 UTC](https://discourse.nodered.org/t/buttons-multi-press-detection-and-problems-with-flow/23128/12 "2020-03-15T08:11:09Z")

</div>

Further observations on what is happening.

(Again) for reasons unknown I added a bit more to the flow I posted:

(Picture)

 ![Screenshot from 2020-03-15 19-04-46](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/7/9/793414966d2deb3fdd70a309579e734ba0d5a138.png)

 ![Screenshot from 2020-03-15 19-07-09](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/2/d/2d5e9b33ba7117f592f9fa4572b2b38e5298ba25.png)

Thinking the `trigger` node may be _biased_ to look at `msg.payload`.

You can see the `msg.count` (blue) incrementing as the button is pressed.  
That goes through the `change` node. (Move msg.count -\> msg.payload) Shown in second picture.  
You then see the modified message (though it looks like an _object_ now.) (Pink lines)  
The new message is fed into the `trigger` node and it spits out an `undefined` message.

Code:

```auto
[{"id":"caa43354.3368","type":"debug","z":"a941e919.4a7d88","name":"Counter","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"count","targetType":"msg","x":690,"y":481,"wires":[]},{"id":"1db43ee9.553791","type":"counter","z":"a941e919.4a7d88","name":"","init":"0","step":"1","lower":null,"upper":null,"mode":"increment","outputs":"1","x":550,"y":531,"wires":[["caa43354.3368","cadab3f.26cf5d"]]},{"id":"cadab3f.26cf5d","type":"change","z":"a941e919.4a7d88","name":"","rules":[{"t":"move","p":"count","pt":"msg","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":740,"y":531,"wires":[["9cbb5de4.cc3e68","20470f14.8b39d"]]},{"id":"9cbb5de4.cc3e68","type":"trigger","z":"a941e919.4a7d88","op1":"","op2":"","op1type":"nul","op2type":"payl","duration":"1000","extend":false,"units":"ms","reset":"","bytopic":"all","name":"","x":930,"y":531,"wires":[["f4123919.3c7a38"]]},{"id":"20470f14.8b39d","type":"debug","z":"a941e919.4a7d88","name":"Button presses","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":920,"y":570,"wires":[]},{"id":"f4123919.3c7a38","type":"debug","z":"a941e919.4a7d88","name":"Counter 1","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"count","targetType":"msg","x":1100,"y":481,"wires":[]}]

```

---

<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:** [15 March 2020 09:19 UTC](https://discourse.nodered.org/t/buttons-multi-press-detection-and-problems-with-flow/23128/13 "2020-03-15T09:19:16Z")

</div>

Further head bashing.

This works:

```auto
[{"id":"fe868a29.aa1c58","type":"ui_template","z":"a941e919.4a7d88","group":"15a0e304.1a21ad","name":"Button","order":6,"width":"5","height":1,"format":"<button ng-mousedown=\"send({payload: 'down'})\" ng-mouseup=\"send({payload: 'up'})\"><i class=\"material-icons\"> PRESS </i></button>\n","storeOutMessages":true,"fwdInMessages":true,"templateScope":"local","x":170,"y":400,"wires":[["a3f9e42f.70b308","28d60a04.2aab4e"]]},{"id":"28d60a04.2aab4e","type":"switch","z":"a941e919.4a7d88","name":"down / up","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"down","vt":"str"},{"t":"eq","v":"up","vt":"str"}],"checkall":"true","repair":false,"outputs":2,"x":370,"y":400,"wires":[["835f76d9.f0e3","2ac9c835.8f4e48"],["7b12565f.296678"]]},{"id":"835f76d9.f0e3","type":"debug","z":"a941e919.4a7d88","name":"Up","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":360,"y":359,"wires":[]},{"id":"a1614e03.32aa68","type":"inject","z":"a941e919.4a7d88","name":"Reset","topic":"RESET","payload":"Reset","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":520,"y":400,"wires":[["c6646b5c.2ef1c8"]]},{"id":"c6646b5c.2ef1c8","type":"function","z":"a941e919.4a7d88","name":"set count","func":"let c = 0;\nif (msg.topic == \"RESET\")\n{\n context.set('count',0);\n node.status('0');\n return;\n} else\nif (msg.topic == \"down\")\n{\n c = context.get('count') || 0;\n c = c + 1;\n context.set('count',c);\n// msg.payload = c;\n node.status(c);\n return;\n} else\nif (msg.topic == \"up\")\n{\n //\n c = context.get('count');\n msg.payload = c;\n node.status(c);\n return msg;\n}\n","outputs":1,"noerr":0,"x":690,"y":400,"wires":[["596266d5.593608"]]},{"id":"2ac9c835.8f4e48","type":"change","z":"a941e919.4a7d88","name":"Down","rules":[{"t":"set","p":"topic","pt":"msg","to":"down","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":520,"y":360,"wires":[["c6646b5c.2ef1c8"]]},{"id":"8b7b4eb.6207b3","type":"trigger","z":"a941e919.4a7d88","op1":"","op2":"up","op1type":"nul","op2type":"str","duration":"400","extend":true,"units":"ms","reset":"","bytopic":"all","name":"","x":680,"y":440,"wires":[["c6646b5c.2ef1c8"]]},{"id":"7b12565f.296678","type":"change","z":"a941e919.4a7d88","name":"Down","rules":[{"t":"set","p":"topic","pt":"msg","to":"up","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":520,"y":440,"wires":[["8b7b4eb.6207b3"]]},{"id":"596266d5.593608","type":"debug","z":"a941e919.4a7d88","name":"Presses.","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":950,"y":400,"wires":[]},{"id":"15a0e304.1a21ad","type":"ui_group","name":"Group 1","tab":"e6a111dc.7a3e8","order":1,"disp":true,"width":6},{"id":"e6a111dc.7a3e8","type":"ui_tab","z":"","name":"WOL Buttons","icon":"dashboard","order":46,"disabled":false,"hidden":false}]

```

---

<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:** [15 March 2020 10:10 UTC](https://discourse.nodered.org/t/buttons-multi-press-detection-and-problems-with-flow/23128/14 "2020-03-15T10:10:24Z")

</div>

After even more head banging, it is so close now.

Known _bug_:  
Sometimes when the button is released, it _flashes_ the next one higher on the count.

Use:

Single press/hold sets off #1 _flashing_.  
Click and press/hold sets off #2 _flashing_  
(and so on)

But sometimes when you release the button, you get a click on the next (higher numbered) LED.

Code:

```auto
[{"id":"a1614e03.32aa68","type":"inject","z":"a941e919.4a7d88","name":"Reset","topic":"RESET","payload":"Reset","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":910,"y":360,"wires":[["9f5301cc.e8f418"]]},{"id":"9f5301cc.e8f418","type":"change","z":"a941e919.4a7d88","name":"Reset","rules":[{"t":"set","p":"topic","pt":"msg","to":"RESET","tot":"str"},{"t":"set","p":"payload","pt":"msg","to":"blah","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":1100,"y":400,"wires":[["c6646b5c.2ef1c8"]]},{"id":"c6646b5c.2ef1c8","type":"function","z":"a941e919.4a7d88","name":"set count","func":"let c = 0;\nif (msg.topic == \"RESET\")\n{\n context.set('count',0);\n node.status('0');\n msg.payload = \"1\";\n msg.topic = \"control\";\n return msg;\n} else\nif (msg.topic == \"down\")\n{\n c = context.get('count') || 0;\n c = c + 1;\n context.set('count',c);\n// msg.payload = c;\n// node.status(c);\n return;\n} else\nif (msg.topic == \"up\")\n{\n //\n c = context.get('count');\n \n //\n // Test line to get all gates closed\n c = c + 1;\n //\n \n msg.payload = String(c);\n msg.topic = \"CONTROL\";\n node.status(c);\n return msg;\n}\n","outputs":1,"noerr":0,"x":1270,"y":400,"wires":[["fcf665dd.ff8278","a56393cb.30da88"]]},{"id":"b7df123f.c8c96","type":"trigger","z":"a941e919.4a7d88","op1":"","op2":"0","op1type":"nul","op2type":"str","duration":"300","extend":true,"units":"ms","reset":"","bytopic":"all","name":"Repeat","x":920,"y":440,"wires":[["9f5301cc.e8f418"]]},{"id":"2ac9c835.8f4e48","type":"change","z":"a941e919.4a7d88","name":"Down","rules":[{"t":"set","p":"topic","pt":"msg","to":"down","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":1100,"y":440,"wires":[["c6646b5c.2ef1c8"]]},{"id":"8b7b4eb.6207b3","type":"trigger","z":"a941e919.4a7d88","op1":"","op2":"up","op1type":"nul","op2type":"str","duration":"200","extend":true,"units":"ms","reset":"","bytopic":"all","name":"","x":1250,"y":360,"wires":[["c6646b5c.2ef1c8"]]},{"id":"fcf665dd.ff8278","type":"delay","z":"a941e919.4a7d88","name":"Delay","pauseType":"delay","timeout":"2","timeoutUnits":"milliseconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":1410,"y":400,"wires":[["596266d5.593608","e53a6bf2.e783a"]]},{"id":"a56393cb.30da88","type":"ui_text","z":"a941e919.4a7d88","group":"15a0e304.1a21ad","order":3,"width":2,"height":1,"name":"Click count","label":"","format":"{{msg.payload}}","layout":"row-spread","x":1500,"y":320,"wires":[]},{"id":"44c1552b.437b1c","type":"trigger","z":"a941e919.4a7d88","op1":"1","op2":"0","op1type":"str","op2type":"str","duration":"-150","extend":false,"units":"ms","reset":"up","bytopic":"all","name":"Repeat","x":710,"y":500,"wires":[["dca52edc.44544","b7df123f.c8c96"]]},{"id":"28d60a04.2aab4e","type":"switch","z":"a941e919.4a7d88","name":"Up / Down","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"up","vt":"str"},{"t":"eq","v":"down","vt":"str"}],"checkall":"true","repair":false,"outputs":2,"x":930,"y":400,"wires":[["7b12565f.296678"],["2ac9c835.8f4e48"]]},{"id":"7b12565f.296678","type":"change","z":"a941e919.4a7d88","name":"Up","rules":[{"t":"set","p":"topic","pt":"msg","to":"up","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":1100,"y":360,"wires":[["8b7b4eb.6207b3"]]},{"id":"596266d5.593608","type":"debug","z":"a941e919.4a7d88","name":"Presses.","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":1590,"y":400,"wires":[]},{"id":"e53a6bf2.e783a","type":"trigger","z":"a941e919.4a7d88","op1":"close","op2":"","op1type":"str","op2type":"pay","duration":"1","extend":false,"units":"ms","reset":"","bytopic":"all","name":"","x":1600,"y":440,"wires":[["fff1b459.485bc8"]]},{"id":"dca52edc.44544","type":"fan","z":"a941e919.4a7d88","x":920,"y":710,"wires":[["cc67eb37.2342f8","1b0f3f80.afffa9","d9c86f9c.ad24b8","fdf69d1e.6bc6f"]]},{"id":"71a9f4f0.0d0b94","type":"trigger","z":"a941e919.4a7d88","op1":"","op2":"","op1type":"nul","op2type":"payl","duration":"250","extend":false,"units":"ms","reset":"","bytopic":"all","name":"Try","x":720,"y":450,"wires":[["44c1552b.437b1c"]]},{"id":"fe868a29.aa1c58","type":"ui_template","z":"a941e919.4a7d88","group":"15a0e304.1a21ad","name":"Button","order":1,"width":5,"height":1,"format":"<button ng-mousedown=\"send({payload: 'down'})\" ng-mouseup=\"send({payload: 'up'})\"><i class=\"material-icons\"> PRESS </i></button>\n","storeOutMessages":true,"fwdInMessages":true,"templateScope":"local","x":720,"y":400,"wires":[["28d60a04.2aab4e","71a9f4f0.0d0b94"]]},{"id":"ecda157e.7598","type":"inject","z":"a941e919.4a7d88","name":"up","topic":"","payload":"up","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":720,"y":350,"wires":[["28d60a04.2aab4e"]]},{"id":"fff1b459.485bc8","type":"function","z":"a941e919.4a7d88","name":"","func":"// This line needs to reflect the number of outputs\nlet output = [null,null,null,null,null];\nif (msg.payload === 'close') {\n // As does this line.\n node.send([msg,msg,msg,msg,msg]);\n} else {\n output[msg.payload - 1] = msg;\n node.send(output);\n}\n","outputs":5,"noerr":0,"x":1620,"y":500,"wires":[["95db009.74a48"],["34d5daad.0eb1de"],["951dfb8a.dcca58"],["7b7a0b6d.29393c"],[]]},{"id":"cc67eb37.2342f8","type":"gate","z":"a941e919.4a7d88","name":"Gate 1","controlTopic":"control","defaultState":"closed","openCmd":"1","closeCmd":"close","toggleCmd":"toggle","defaultCmd":"default","persist":false,"x":1100,"y":530,"wires":[["7b998928.3599c"]]},{"id":"1b0f3f80.afffa9","type":"gate","z":"a941e919.4a7d88","name":"Gate 2","controlTopic":"control","defaultState":"closed","openCmd":"2","closeCmd":"close","toggleCmd":"toggle","defaultCmd":"default","persist":false,"x":1100,"y":620,"wires":[["35253efd.a0d7e2"]]},{"id":"d9c86f9c.ad24b8","type":"gate","z":"a941e919.4a7d88","name":"Gate 3","controlTopic":"control","defaultState":"closed","openCmd":"3","closeCmd":"close","toggleCmd":"toggle","defaultCmd":"default","persist":false,"x":1100,"y":710,"wires":[["36d58e32.58b1fa"]]},{"id":"fdf69d1e.6bc6f","type":"gate","z":"a941e919.4a7d88","name":"Gate 4","controlTopic":"control","defaultState":"closed","openCmd":"4","closeCmd":"close","toggleCmd":"toggle","defaultCmd":"default","persist":false,"x":1100,"y":800,"wires":[["1dba84b7.3b1623"]]},{"id":"34d5daad.0eb1de","type":"link out","z":"a941e919.4a7d88","name":"","links":["d3a88124.1c1a7","e7178238.5559e8"],"x":1885,"y":480,"wires":[]},{"id":"951dfb8a.dcca58","type":"link out","z":"a941e919.4a7d88","name":"","links":["f212e6f5.f7de6","da8d9615.654ec8"],"x":1885,"y":520,"wires":[]},{"id":"7b7a0b6d.29393c","type":"link out","z":"a941e919.4a7d88","name":"","links":["577ab48d.59899c","51891e3.14f576"],"x":1885,"y":560,"wires":[]},{"id":"95db009.74a48","type":"link out","z":"a941e919.4a7d88","name":"","links":["d3a88124.1c1a7","33f2a475.423eb4"],"x":1885,"y":430,"wires":[]},{"id":"33f2a475.423eb4","type":"link in","z":"a941e919.4a7d88","name":"","links":["95db009.74a48"],"x":1025,"y":490,"wires":[["cc67eb37.2342f8"]]},{"id":"7b998928.3599c","type":"function","z":"a941e919.4a7d88","name":"Pulse","func":"let c = context.get('count') || 0;\nc = c + 1\nc = c % 2;\nnode.status(c);\ncontext.set('count',c);\nmsg.payload = c;\nreturn msg;","outputs":1,"noerr":0,"x":1240,"y":530,"wires":[["82bb34e8.144e","c79f0100.78d4e"]]},{"id":"e7178238.5559e8","type":"link in","z":"a941e919.4a7d88","name":"","links":["34d5daad.0eb1de"],"x":1025,"y":590,"wires":[["1b0f3f80.afffa9"]]},{"id":"35253efd.a0d7e2","type":"function","z":"a941e919.4a7d88","name":"Pulse","func":"let c = context.get('count') || 0;\nc = c + 1\nc = c % 2;\nnode.status(c);\ncontext.set('count',c);\nmsg.payload = c;\nreturn msg;","outputs":1,"noerr":0,"x":1240,"y":620,"wires":[["488d0f0.53679f","8ced5380.6a4838"]]},{"id":"da8d9615.654ec8","type":"link in","z":"a941e919.4a7d88","name":"","links":["951dfb8a.dcca58"],"x":1025,"y":680,"wires":[["d9c86f9c.ad24b8"]]},{"id":"36d58e32.58b1fa","type":"function","z":"a941e919.4a7d88","name":"Pulse","func":"let c = context.get('count') || 0;\nc = c + 1\nc = c % 2;\nnode.status(c);\ncontext.set('count',c);\nmsg.payload = c;\nreturn msg;","outputs":1,"noerr":0,"x":1240,"y":710,"wires":[["4b9629c8.9d672","be31f56f.1b2218"]]},{"id":"51891e3.14f576","type":"link in","z":"a941e919.4a7d88","name":"","links":["7b7a0b6d.29393c"],"x":1025,"y":770,"wires":[["fdf69d1e.6bc6f"]]},{"id":"1dba84b7.3b1623","type":"function","z":"a941e919.4a7d88","name":"Pulse","func":"let c = context.get('count') || 0;\nc = c + 1\nc = c % 2;\nnode.status(c);\ncontext.set('count',c);\nmsg.payload = c;\nreturn msg;","outputs":1,"noerr":0,"x":1240,"y":800,"wires":[["a30d8953.a9def","45ae3ea.e65b0c"]]},{"id":"82bb34e8.144e","type":"ui_led","z":"a941e919.4a7d88","group":"15a0e304.1a21ad","order":4,"width":2,"height":2,"label":"0","labelPlacement":"left","labelAlignment":"left","colorForValue":[{"color":"red","value":"1","valueType":"num"},{"color":"green","value":"0","valueType":"num"}],"allowColorForValueInMessage":false,"name":"0","x":1370,"y":570,"wires":[]},{"id":"c79f0100.78d4e","type":"debug","z":"a941e919.4a7d88","name":"0","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":1370,"y":530,"wires":[]},{"id":"488d0f0.53679f","type":"debug","z":"a941e919.4a7d88","name":"1","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":1370,"y":620,"wires":[]},{"id":"8ced5380.6a4838","type":"ui_led","z":"a941e919.4a7d88","group":"15a0e304.1a21ad","order":5,"width":2,"height":2,"label":"1","labelPlacement":"left","labelAlignment":"left","colorForValue":[{"color":"red","value":"1","valueType":"num"},{"color":"green","value":"0","valueType":"num"}],"allowColorForValueInMessage":false,"name":"1","x":1370,"y":660,"wires":[]},{"id":"4b9629c8.9d672","type":"debug","z":"a941e919.4a7d88","name":"2","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":1370,"y":710,"wires":[]},{"id":"be31f56f.1b2218","type":"ui_led","z":"a941e919.4a7d88","group":"15a0e304.1a21ad","order":6,"width":2,"height":2,"label":"2","labelPlacement":"left","labelAlignment":"left","colorForValue":[{"color":"red","value":"1","valueType":"num"},{"color":"green","value":"0","valueType":"num"}],"allowColorForValueInMessage":false,"name":"2","x":1370,"y":750,"wires":[]},{"id":"a30d8953.a9def","type":"debug","z":"a941e919.4a7d88","name":"3","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":1370,"y":800,"wires":[]},{"id":"45ae3ea.e65b0c","type":"ui_led","z":"a941e919.4a7d88","group":"15a0e304.1a21ad","order":7,"width":2,"height":2,"label":"3","labelPlacement":"left","labelAlignment":"left","colorForValue":[{"color":"red","value":"1","valueType":"num"},{"color":"green","value":"0","valueType":"num"}],"allowColorForValueInMessage":false,"name":"3","x":1370,"y":840,"wires":[]},{"id":"15a0e304.1a21ad","type":"ui_group","z":"","name":"Group 1","tab":"e6a111dc.7a3e8","order":1,"disp":true,"width":"9","collapse":false},{"id":"e6a111dc.7a3e8","type":"ui_tab","z":"","name":"WOL Buttons","icon":"dashboard","order":46,"disabled":false,"hidden":false}]

```
