# How to detect multiple presses on a single button

**URL:** <https://discourse.nodered.org/t/how-to-detect-multiple-presses-on-a-single-button/20560>\
**Category:** Share Your Projects\
**Created:** [19 January 2020 07:44 UTC](https://discourse.nodered.org/t/how-to-detect-multiple-presses-on-a-single-button/20560 "2020-01-19T07:44:08Z")\
**Posts on this page:** 20\
**Page:** 2

<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:** [7 March 2021 21:57 UTC](https://discourse.nodered.org/t/how-to-detect-multiple-presses-on-a-single-button/20560/22 "2021-03-07T21:57:34Z")

</div>

Welcome to the forum @Soulflyzz.

I recommend watching this playlist: [Node-RED Essentials](https://www.youtube.com/playlist?list=PLyNBB9VCLmo1hyO-4fIZ08gqFcXBkHy-6). The videos are done by the developers of node-red. They're nice & short and to the point. You will understand a whole lot more in about 1 hour. A small investment for a lot of gain.  
I am sure that importing flows is covered in there.

---

<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:** [7 March 2021 22:00 UTC](https://discourse.nodered.org/t/how-to-detect-multiple-presses-on-a-single-button/20560/23 "2021-03-07T22:00:27Z")

</div>

Hi.

As a button lover, I'll explain how my one works.

The problem is you may accidentally press a button. This prevents that happening, as you need to multiple press it.

Depending on how many times you press it, depends on which output is activated.

You can have different things happen from the same button depending on how many times you press it in a given time.

---

<div class="post-metadata">

**Author:** ![Michielo](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/michielo/32/70268_2.png) [@Michielo](https://discourse.nodered.org/u/Michielo)\
**Post date:** [1 September 2021 21:48 UTC](https://discourse.nodered.org/t/how-to-detect-multiple-presses-on-a-single-button/20560/24 "2021-09-01T21:48:24Z")

</div>

Hi everyone,

It's with great interest that I read this topic and deployed the optimised flow provided by Trying\_to\_learn in my project. Thanks for that!

Objective of my project: control 3 sets of lamps (in the hallway, 3 floors, so 1 set of lamps/floor) with a Ikea Tradfri button switch (one like [this](https://www.ikea.com/be/nl/images/products/tradfri-draadloze-dimmer-wit__0710157_pe727359_s5.jpg?f=xl)) controlled via zigbee2mqtt on an old Raspberry Pi. The switch has 4 functions (on, off, dim up and dim down), but (for now) I'm only using the on & off functions (currently depicted as the on & off inject nodes). Using Trying\_to\_learn's flow, I came up with the following:

1. One "on" click - lights downstairs go on
2. Two "on" clicks - lights on the first floor go on
3. Three "on" clicks - lights on the second floor go on
4. One "off" click - all hallway lights go out (irrespective of whether their state was on or not)

So far, so good and this all seems to work. Now, I'm trying to add some additional functionality, being:

1. The lights should go out automatically after 10 minutes (with a reset of the counter, if I turn the lights off manually). In the flow below, the interval is 1 minute for testing purposes.
2. Between 1 am and 6 am, the lights should come on at 30%. Outside that time interval, they should come on at 50%. In the flow below, the time range is between 10 pm and 11 pm for testing purposes.

Here's my current flow:

 ![Node-red (2)](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/e/3/e3d8b2127cb600bb243f62999b18bb00ac6d38eb.jpeg)

As you can see, I replaced Trying\_to\_learn's "Single/Double/Triple" press Change nodes with "On (50%)" Function nodes. Then between the "count presses" Switch node and the Function node, I added a Trigger node to send the "off" command directly to the lamps after 10 minutes and a Time Range Node to incorporate the lights on at 30% at night functionality. For each set of lamps to control, I duplicated that process. The lights off Function node at the top contains a msg.payload "off" and a msg.reset "1" with the msg.payload directly going to the lamps and the msg.reset going to the Trigger node to reset the auto-off function.

Although everything works as expected, I'm wondering if this is the correct and most efficient way (mainly the duplication for each set of lamps) to realize the additional functionality?

If you would have any comments or suggestions on my project, do let me know. It's my first Node-red project, so I would really like to hear your thoughts.

I've included my flo below (latitude and longitude values on the Time Range Node are dummy values).

```auto
[{"id":"220f5f63f38618da","type":"tab","label":"Flow 1","disabled":false,"info":""},{"id":"10061cfe77e897bf","type":"change","z":"220f5f63f38618da","name":"Reset","rules":[{"t":"set","p":"reset","pt":"msg","to":"foo","tot":"str"},{"t":"delete","p":"payload","pt":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":430,"y":500,"wires":[["9640b5e0cd03470b"]]},{"id":"1d7574774def2726","type":"inject","z":"220f5f63f38618da","name":"Reset","props":[{"p":"payload","v":"","vt":"date"},{"p":"topic","v":"","vt":"string"}],"repeat":"","crontab":"","once":true,"onceDelay":"0.5","topic":"","payload":"","payloadType":"date","x":250,"y":440,"wires":[["10061cfe77e897bf"]]},{"id":"9640b5e0cd03470b","type":"counter","z":"220f5f63f38618da","name":"","init":"0","step":"1","lower":null,"upper":null,"mode":"increment","outputs":"1","x":440,"y":440,"wires":[["b421692c8d37eab1"]]},{"id":"f36f6e065eb2dd8a","type":"switch","z":"220f5f63f38618da","name":"count presses","property":"count","propertyType":"flow","rules":[{"t":"eq","v":"1","vt":"num"},{"t":"eq","v":"2","vt":"num"},{"t":"eq","v":"3","vt":"num"},{"t":"eq","v":"4","vt":"num"}],"checkall":"true","repair":false,"outputs":4,"x":600,"y":560,"wires":[["b8857d0f96fc3179"],["af2c8cce149add1b"],["d594f5180610c6ca"],[]],"info":"Just expand the flow.count to how ever many\npresses you want/need."},{"id":"c6c4ef109bd4d35d","type":"trigger","z":"220f5f63f38618da","name":"","op1":"","op2":"0","op1type":"nul","op2type":"str","duration":"1","extend":true,"overrideDelay":false,"units":"s","reset":"","bytopic":"all","topic":"topic","outputs":1,"x":260,"y":560,"wires":[["b2dc820740135300","f36f6e065eb2dd8a"]]},{"id":"b421692c8d37eab1","type":"change","z":"220f5f63f38618da","name":"Flow set","rules":[{"t":"set","p":"count","pt":"flow","to":"count","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":600,"y":440,"wires":[[]]},{"id":"b2dc820740135300","type":"delay","z":"220f5f63f38618da","name":"Delay","pauseType":"delay","timeout":"1","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"allowrate":false,"x":250,"y":500,"wires":[["10061cfe77e897bf"]]},{"id":"17fbf340f131352f","type":"function","z":"220f5f63f38618da","name":"On (50%)","func":"msg.payload = { state: 'on', color: 'normal', brightness: 125};\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1200,"y":460,"wires":[["979a175ac9df199d"]]},{"id":"7172d6396d017a71","type":"function","z":"220f5f63f38618da","name":"Lights on","func":"msg.payload.Time = new Date()\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":540,"y":120,"wires":[["1eac149f385c8c10"]]},{"id":"802170cf168f5958","type":"function","z":"220f5f63f38618da","name":"Lights off","func":"msg.payload = { state: 'off'};\nmsg.reset = 1;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":540,"y":200,"wires":[["03861acd3568feb9"]]},{"id":"1eac149f385c8c10","type":"link out","z":"220f5f63f38618da","name":"Lights on","links":["97e1707c40b7587e"],"x":645,"y":120,"wires":[]},{"id":"03861acd3568feb9","type":"link out","z":"220f5f63f38618da","name":"Lights off","links":["26e0c7d9755ec2a4","51830c9a86991d5a"],"x":645,"y":200,"wires":[]},{"id":"97e1707c40b7587e","type":"link in","z":"220f5f63f38618da","name":"Lights on","links":["1eac149f385c8c10"],"x":55,"y":520,"wires":[["c6c4ef109bd4d35d","9640b5e0cd03470b"]]},{"id":"b8857d0f96fc3179","type":"trigger","z":"220f5f63f38618da","name":"Auto off (10 min)","op1":"on","op2":"off","op1type":"str","op2type":"str","duration":"1","extend":false,"overrideDelay":false,"units":"min","reset":"off","bytopic":"all","topic":"topic","outputs":2,"x":820,"y":460,"wires":[["e4b829336ede814d"],["979a175ac9df199d"]]},{"id":"e4b829336ede814d","type":"time-range-switch","z":"220f5f63f38618da","name":"","lat":"0.000","lon":"0.000","startTime":"22:00","endTime":"23:00","startOffset":0,"endOffset":0,"x":1010,"y":440,"wires":[["5fecaa0a23a2c025"],["17fbf340f131352f"]]},{"id":"5fecaa0a23a2c025","type":"function","z":"220f5f63f38618da","name":"On (30%)","func":"msg.payload = { state: 'on', color: 'normal', brightness: 76};\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1200,"y":420,"wires":[["979a175ac9df199d"]]},{"id":"3ad8f6e71fdc3921","type":"function","z":"220f5f63f38618da","name":"On (50%)","func":"msg.payload = { state: 'on', color: 'normal', brightness: 125};\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1200,"y":560,"wires":[["99c163c6e963a621"]]},{"id":"af2c8cce149add1b","type":"trigger","z":"220f5f63f38618da","name":"Auto off (10 min)","op1":"on","op2":"off","op1type":"str","op2type":"str","duration":"1","extend":false,"overrideDelay":false,"units":"min","reset":"off","bytopic":"all","topic":"topic","outputs":2,"x":820,"y":560,"wires":[["b4dd3588c226d34c"],["99c163c6e963a621"]]},{"id":"b4dd3588c226d34c","type":"time-range-switch","z":"220f5f63f38618da","name":"","lat":"0.000","lon":"0.000","startTime":"22:00","endTime":"23:00","startOffset":0,"endOffset":0,"x":1010,"y":540,"wires":[["e9fb5b82e1f3f0ed"],["3ad8f6e71fdc3921"]]},{"id":"e9fb5b82e1f3f0ed","type":"function","z":"220f5f63f38618da","name":"On (30%)","func":"msg.payload = { state: 'on', color: 'normal', brightness: 76};\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1200,"y":520,"wires":[["99c163c6e963a621"]]},{"id":"8afebffddb348d6b","type":"function","z":"220f5f63f38618da","name":"On (50%)","func":"msg.payload = { state: 'on', color: 'normal', brightness: 125};\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1200,"y":660,"wires":[["94c8353245208648"]]},{"id":"d594f5180610c6ca","type":"trigger","z":"220f5f63f38618da","name":"Auto off (10 min)","op1":"on","op2":"off","op1type":"str","op2type":"str","duration":"1","extend":false,"overrideDelay":false,"units":"min","reset":"off","bytopic":"all","topic":"topic","outputs":2,"x":820,"y":660,"wires":[["2bf3e94bcd5b2715"],["94c8353245208648"]]},{"id":"2bf3e94bcd5b2715","type":"time-range-switch","z":"220f5f63f38618da","name":"","lat":"0.000","lon":"0.000","startTime":"22:00","endTime":"23:00","startOffset":0,"endOffset":0,"x":1010,"y":640,"wires":[["e9dcafd4edd1a3c1"],["8afebffddb348d6b"]]},{"id":"e9dcafd4edd1a3c1","type":"function","z":"220f5f63f38618da","name":"On (30%)","func":"msg.payload = { state: 'on', color: 'normal', brightness: 76};\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1200,"y":620,"wires":[["94c8353245208648"]]},{"id":"51830c9a86991d5a","type":"link in","z":"220f5f63f38618da","name":"Lights off","links":["03861acd3568feb9"],"x":1295,"y":320,"wires":[["99c163c6e963a621","94c8353245208648","b8857d0f96fc3179","979a175ac9df199d","af2c8cce149add1b","d594f5180610c6ca"]]},{"id":"979a175ac9df199d","type":"debug","z":"220f5f63f38618da","name":"Light 1","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1350,"y":440,"wires":[]},{"id":"99c163c6e963a621","type":"debug","z":"220f5f63f38618da","name":"Light 2","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1370,"y":540,"wires":[]},{"id":"94c8353245208648","type":"debug","z":"220f5f63f38618da","name":"Light 3","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1370,"y":640,"wires":[]},{"id":"dbc963f9d842e091","type":"inject","z":"220f5f63f38618da","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"on","payloadType":"str","x":130,"y":140,"wires":[["7172d6396d017a71"]]},{"id":"a03dfef9ee46c9f0","type":"inject","z":"220f5f63f38618da","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"off","payloadType":"str","x":130,"y":180,"wires":[["802170cf168f5958"]]}]

```

Thanks everyone for your input.

Obviously, if you'd like to use the flow, feel free to do so.

Kind regards,

Michielo

---

<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:** [1 September 2021 22:21 UTC](https://discourse.nodered.org/t/how-to-detect-multiple-presses-on-a-single-button/20560/25 "2021-09-01T22:21:32Z")

</div>

You are close getting to what you want with number 1:

> The lights should go out automatically after 10 minutes (with a reset of the counter, if I turn the lights off manually).

The output of the `trigger` nodes doesn't look quite right.  
You should need a `function` node to make the message to turn off the lights.

Further looking, I am wrong. You send the `OFF` message there.  
Sorry.

Similar to what you have on the output of the `time range node`.

Following up on the last, bit, you don't need to reset the counter... After `10` minutes....  
You turn the lights on and before the `10` minutes is up you turn them off.  
An `OFF` message is sent when you turn the light/s off.  
The `trigger` node sends another `OFF` message after the designated `10` minutes.  
No harm.

The rest looks ok.

Sorry I am not more helpful but just now but I am busy doing something else.  
That will be another 3 hours from time of posting.  
Then another thing happening for the next 2 hours after that.

If you don't get any other help in the mean time, I will get back to you later on.

(Argh)

Ok, the `reset`..... When the `off` button is pressed, connect a _wire_ from it to the `change` node as shown below.

![Screenshot from 2021-09-02 08-24-37](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/0/e/0e856fc5d69430b62263a97039ca424e94eb0321.png)

---

<div class="post-metadata">

**Author:** ![Michielo](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/michielo/32/70268_2.png) [@Michielo](https://discourse.nodered.org/u/Michielo)\
**Post date:** [3 September 2021 12:30 UTC](https://discourse.nodered.org/t/how-to-detect-multiple-presses-on-a-single-button/20560/26 "2021-09-03T12:30:47Z")

</div>

Thanks for your comments, Trying\_to\_learn. Highly appreciated.

Regarding the separate off message to the Auto-off (10 min) Trigger node, I added that because, for some reason, the lights won't respond to an on click if I manually turn them off and need to turn them back on within the 10 min interval. Sending an off message to the Auto-off (10 min) Trigger node seemed to solve that.

Regarding the direct wire from the off inject Node to the Reset Change Node, I'll include that in my project. I assume that, after that, I no longer need the Reset inject Node. Correct?

Any other comments/suggestions on how to optimize the flow, particularly relating to the duplication of the Trigger/timestamp/function nodes for each set of lamps. Any way I could move them earlier in the flow and/or merge them into a single stream of Trigger/timestamp/function nodes to avoid duplication of each stream at the end (obviously without needing to duplicate other nodes since that would result in replacing one stream of nodes with another stream of nodes)?

Thanks,

Michielo

---

<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:** [3 September 2021 21:20 UTC](https://discourse.nodered.org/t/how-to-detect-multiple-presses-on-a-single-button/20560/27 "2021-09-03T21:20:09Z")

</div>

Hi.

It has been a while since I wrote that flow/part.

The _reset_ line/link I showed you may not really be needed. I put it there to be sure the `counter` node was reset if/when you turn off the lights.

Not that it makes much difference, but the `function` node you have after the **turn off lights** inject node has a bit too much code.

Only to show you, this works - compared to what you have:

```auto
[{"id":"802170cf168f5958","type":"function","z":"220f5f63f38618da","name":"Lights off","func":"//msg.payload = { state: 'off'};\nmsg.reset = 1;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":540,"y":200,"wires":[["03861acd3568feb9"]]}]

```

> [@Michielo](#):
>
> Any way I could move them earlier in the flow and/or merge them into a single stream of Trigger/timestamp/function nodes to avoid duplication of each stream at the end (obviously without needing to duplicate other nodes since that would result in replacing one stream of nodes with another stream of nodes)?

~~I don't think that is/would be possible.  
Why?  
Well, think about it:  
you have 3 different lights to control.  
At some stage.....~~

This will need work, but for the sake of sharing....

```auto
[{"id":"3188173c81bdd0b7","type":"change","z":"220f5f63f38618da","name":"Reset","rules":[{"t":"set","p":"reset","pt":"msg","to":"foo","tot":"str"},{"t":"delete","p":"payload","pt":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":430,"y":1290,"wires":[["a5f31cee2ef83d48"]]},{"id":"e21f570d49f4ee6a","type":"inject","z":"220f5f63f38618da","name":"Reset","props":[{"p":"payload","v":"","vt":"date"},{"p":"topic","v":"","vt":"string"}],"repeat":"","crontab":"","once":true,"onceDelay":"0.5","topic":"","payload":"","payloadType":"date","x":250,"y":1230,"wires":[["3188173c81bdd0b7"]]},{"id":"a5f31cee2ef83d48","type":"counter","z":"220f5f63f38618da","name":"","init":"0","step":"1","lower":null,"upper":null,"mode":"increment","outputs":"1","x":440,"y":1230,"wires":[["4a66e3483b888316"]]},{"id":"9a337c4074760641","type":"trigger","z":"220f5f63f38618da","name":"","op1":"","op2":"0","op1type":"nul","op2type":"str","duration":"1","extend":true,"overrideDelay":false,"units":"s","reset":"","bytopic":"all","topic":"topic","outputs":1,"x":260,"y":1350,"wires":[["f8247188fa92c67d","15c92e649e92e01d"]]},{"id":"4a66e3483b888316","type":"change","z":"220f5f63f38618da","name":"Flow set","rules":[{"t":"set","p":"count","pt":"flow","to":"count","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":600,"y":1230,"wires":[[]]},{"id":"f8247188fa92c67d","type":"delay","z":"220f5f63f38618da","name":"Delay","pauseType":"delay","timeout":"1","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"allowrate":false,"x":250,"y":1290,"wires":[["3188173c81bdd0b7"]]},{"id":"65ddd4f0eb7b3d56","type":"function","z":"220f5f63f38618da","name":"On (50%)","func":"msg.payload = { state: 'on', color: 'normal', brightness: 125};\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1280,"y":1350,"wires":[["4d17002fbad446cf"]]},{"id":"f385b2e144f009e7","type":"function","z":"220f5f63f38618da","name":"Lights on","func":"msg.payload.Time = new Date()\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":540,"y":1030,"wires":[["c1d9f2d9479de7f7"]]},{"id":"4c919645a048562c","type":"function","z":"220f5f63f38618da","name":"Lights off","func":"//msg.payload = { state: 'off'};\nmsg.reset = 1;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":540,"y":1110,"wires":[["c729364191f5c4e4"]]},{"id":"c1d9f2d9479de7f7","type":"link out","z":"220f5f63f38618da","name":"Lights on","links":["d4df578d358f4a94"],"x":645,"y":1030,"wires":[]},{"id":"c729364191f5c4e4","type":"link out","z":"220f5f63f38618da","name":"Lights off","links":["26e0c7d9755ec2a4","26fb42af474b05ee"],"x":645,"y":1110,"wires":[]},{"id":"d4df578d358f4a94","type":"link in","z":"220f5f63f38618da","name":"Lights on","links":["c1d9f2d9479de7f7"],"x":55,"y":1310,"wires":[["9a337c4074760641","a5f31cee2ef83d48"]]},{"id":"6db2e35e412b108a","type":"trigger","z":"220f5f63f38618da","name":"Auto off (10 min)","op1":"on","op2":"off","op1type":"str","op2type":"str","duration":"1","extend":false,"overrideDelay":false,"units":"min","reset":"off","bytopic":"all","topic":"topic","outputs":2,"x":800,"y":1350,"wires":[["4373b4066797b93e"],["4d17002fbad446cf"]]},{"id":"7629e69ef8987cf6","type":"function","z":"220f5f63f38618da","name":"On (30%)","func":"msg.payload = { state: 'on', color: 'normal', brightness: 76};\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1280,"y":1310,"wires":[["4d17002fbad446cf"]]},{"id":"26fb42af474b05ee","type":"link in","z":"220f5f63f38618da","name":"Lights off","links":["c729364191f5c4e4"],"x":875,"y":1220,"wires":[["15c92e649e92e01d"]]},{"id":"4d17002fbad446cf","type":"debug","z":"220f5f63f38618da","name":"Light 1","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1430,"y":1330,"wires":[]},{"id":"732a9e68f0e782ac","type":"inject","z":"220f5f63f38618da","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"on","payloadType":"str","x":130,"y":1050,"wires":[["f385b2e144f009e7"]]},{"id":"ecb854ad4c426a17","type":"inject","z":"220f5f63f38618da","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"off","payloadType":"str","x":130,"y":1090,"wires":[["4c919645a048562c"]]},{"id":"4373b4066797b93e","type":"time-range-switch","z":"220f5f63f38618da","name":"","lat":"0.000","lon":"0.000","startTime":"22:00","endTime":"23:00","startOffset":0,"endOffset":0,"x":1040,"y":1330,"wires":[["7629e69ef8987cf6"],["65ddd4f0eb7b3d56"]]},{"id":"15c92e649e92e01d","type":"change","z":"220f5f63f38618da","name":"Select bulb to control","rules":[{"t":"set","p":"bulb","pt":"msg","to":"count","tot":"flow"}],"action":"","property":"","from":"","to":"","reg":false,"x":560,"y":1350,"wires":[["6db2e35e412b108a"]]},{"id":"97ac67ed523e46f6","type":"comment","z":"220f5f63f38618da","name":"Read me! *","info":"This is where things get tricky.\n\nYou need to know how the bulbs are controlled.\n","x":560,"y":1390,"wires":[]}]

```

What's happening?

Ok:  
1 - I am not sure you need the _wire_ which you had from the `link-in` node to the `debug` nodes to turn the bulbs off.  
You may need to check that yourself.  
2 - Looking at it and optimising things, you look for things which are duplicated and try to make them more generic and find what makes them unique and use that to control what is going on.

You will have to post the entire message which is used to turn the bulb/s on/off.  
Because if there isn't anything: you can't do this at all.

What mine (the original) does:  
It counts how many times you press the button - to select the bulb you want to control.  
Depending on the count, it uses the `switch` node to route the message to the code that then controls the bulb. (Tuning it on or off)

You haven't shown the next part of your flow where the message is actually sent to the bulb.

Say you use `MQTT` to control the bulb.  
You would/may have something like this at the next step/stage of things:

```auto
[{"id":"afa8cb4367fce11c","type":"mqtt out","z":"220f5f63f38618da","name":"","topic":"BULB1","qos":"","retain":"","respTopic":"","contentType":"","userProps":"","correl":"","expiry":"","broker":"8941f4c3.0f151","x":1440,"y":1290,"wires":[]},{"id":"8941f4c3.0f151","type":"mqtt-broker","name":"Local","broker":"127.0.0.1","port":"1883","clientid":"","usetls":false,"compatmode":false,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","closeTopic":"","closeQos":"0","closePayload":"","willTopic":"","willQos":"0","willPayload":""}]

```

Where the `topic` controls which bulb is actioned.

Until that is known, I can't really help you more.

---

<div class="post-metadata">

**Author:** ![Michielo](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/michielo/32/70268_2.png) [@Michielo](https://discourse.nodered.org/u/Michielo)\
**Post date:** [4 September 2021 09:12 UTC](https://discourse.nodered.org/t/how-to-detect-multiple-presses-on-a-single-button/20560/28 "2021-09-04T09:12:40Z")

</div>

Not at my computer now, but in the actual project, the on and off inject nodes represent the IKEA button switch which sends msg.payload.click "on" or "off" to a switch node with dual output going into the function nodes you see at the top of the flow. The debug nodes labelled light 1, light 2 and light 3 represent the actual mqtt in nodes of the lights. As such, they directly receive the payload msgs that are currently going into the debug nodes at the end and coming from the link node (turn off manually), the function nodes (turn on at 30% or 50%) or the auto-off switch node.

So the flow I posted is the full picture where I replaced the IKEA switch with 2 inject nodes and the actual lights with the debug nodes.

I'll try posting the "live" flow later today.

---

<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:** [5 September 2021 21:37 UTC](https://discourse.nodered.org/t/how-to-detect-multiple-presses-on-a-single-button/20560/29 "2021-09-05T21:37:46Z")

</div>

> [@Michielo](#):
>
> The debug nodes labelled light 1, light 2 and light 3 represent the actual mqtt in nodes of the lights.

That is the _magic_ missing thing.

And is what you need to further optimise the flow.

So, you need to tell me the topic/s for the three bulbs.

BTW, it won't solve the entire problem, but it will fix a lot of unneeded node duplication.

For the sake of it, let's say the bulbs are `BULB1` `BULB2` and `BULB3`

This is a working example of doing what you want.

# BUT!

There is a _hidden trap_ doing it this way.

I won't tell you - not to spite you, but to try and help you get a better understanding of what is going on.

```auto
[{"id":"31908c93ac6213f5","type":"change","z":"2074fa2103de0d09","name":"Reset","rules":[{"t":"set","p":"reset","pt":"msg","to":"foo","tot":"str"},{"t":"delete","p":"payload","pt":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":580,"y":1200,"wires":[["da37345a794ad2c2"]]},{"id":"b38c749c0e2d9042","type":"inject","z":"2074fa2103de0d09","name":"Reset","props":[{"p":"payload","v":"","vt":"date"},{"p":"topic","v":"","vt":"string"}],"repeat":"","crontab":"","once":true,"onceDelay":"0.5","topic":"","payload":"","payloadType":"date","x":400,"y":1160,"wires":[["31908c93ac6213f5"]]},{"id":"da37345a794ad2c2","type":"counter","z":"2074fa2103de0d09","name":"","init":"0","step":"1","lower":null,"upper":null,"mode":"increment","outputs":"1","x":590,"y":1160,"wires":[["28b90dfb3da2a2d6"]]},{"id":"abb15621e8aaa116","type":"trigger","z":"2074fa2103de0d09","name":"","op1":"","op2":"0","op1type":"nul","op2type":"str","duration":"1","extend":true,"overrideDelay":false,"units":"s","reset":"","bytopic":"all","topic":"topic","outputs":1,"x":410,"y":1280,"wires":[["5379d824e919f3e3","36e8d0b4c2eeccf6"]]},{"id":"6665161cc5a5ed06","type":"change","z":"2074fa2103de0d09","name":"Flow set","rules":[{"t":"set","p":"count","pt":"flow","to":"count","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":1050,"y":1160,"wires":[[]]},{"id":"5379d824e919f3e3","type":"delay","z":"2074fa2103de0d09","name":"Delay","pauseType":"delay","timeout":"1","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"allowrate":false,"x":400,"y":1200,"wires":[["31908c93ac6213f5"]]},{"id":"e6939197572e0cb8","type":"function","z":"2074fa2103de0d09","name":"On (50%)","func":"msg.payload = { state: 'on', color: 'normal', brightness: 125};\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1060,"y":1280,"wires":[["5d3f24b1433ba9c2"]]},{"id":"c6b036344224ed9d","type":"function","z":"2074fa2103de0d09","name":"Lights on","func":"msg.payload.Time = new Date()\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1030,"y":950,"wires":[["3bef75933fd3f749"]]},{"id":"80ab5a1f868baef5","type":"function","z":"2074fa2103de0d09","name":"Lights off","func":"msg.payload = { state: 'off'};\nmsg.reset = 1;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1030,"y":1030,"wires":[["a65fa1e3ac6fdd8f"]]},{"id":"3bef75933fd3f749","type":"link out","z":"2074fa2103de0d09","name":"Lights on","links":["3e7851e26079d05b"],"x":1135,"y":950,"wires":[]},{"id":"a65fa1e3ac6fdd8f","type":"link out","z":"2074fa2103de0d09","name":"Lights off","links":["26e0c7d9755ec2a4","b1f25a79bb53bfe4","5d129908d383c1df"],"x":1135,"y":1030,"wires":[]},{"id":"3e7851e26079d05b","type":"link in","z":"2074fa2103de0d09","name":"Lights on","links":["3bef75933fd3f749"],"x":205,"y":1240,"wires":[["abb15621e8aaa116","da37345a794ad2c2"]]},{"id":"36e8d0b4c2eeccf6","type":"trigger","z":"2074fa2103de0d09","name":"Auto off (10 min)","op1":"on","op2":"off","op1type":"str","op2type":"str","duration":"1","extend":false,"overrideDelay":false,"units":"min","reset":"off","bytopic":"all","topic":"topic","outputs":2,"x":610,"y":1280,"wires":[["9d1435e9c5f303a2"],["5d3f24b1433ba9c2"]]},{"id":"b3f8e94eb3d62267","type":"function","z":"2074fa2103de0d09","name":"On (30%)","func":"msg.payload = { state: 'on', color: 'normal', brightness: 76};\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1060,"y":1240,"wires":[["5d3f24b1433ba9c2"]]},{"id":"b1f25a79bb53bfe4","type":"link in","z":"2074fa2103de0d09","name":"Lights off","links":["a65fa1e3ac6fdd8f"],"x":545,"y":1240,"wires":[["36e8d0b4c2eeccf6"]]},{"id":"712af1bd71a472cb","type":"debug","z":"2074fa2103de0d09","name":"Light 1","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":1500,"y":1260,"wires":[]},{"id":"1e8b13d233dd3e14","type":"inject","z":"2074fa2103de0d09","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"on","payloadType":"str","x":620,"y":970,"wires":[["c6b036344224ed9d"]]},{"id":"09ec278b76f607d3","type":"inject","z":"2074fa2103de0d09","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"off","payloadType":"str","x":620,"y":1010,"wires":[["80ab5a1f868baef5"]]},{"id":"9d1435e9c5f303a2","type":"time-range-switch","z":"2074fa2103de0d09","name":"","lat":"0.000","lon":"0.000","startTime":"22:00","endTime":"23:00","startOffset":0,"endOffset":0,"x":850,"y":1260,"wires":[["b3f8e94eb3d62267"],["e6939197572e0cb8"]]},{"id":"cc9a87b80310f81c","type":"comment","z":"2074fa2103de0d09","name":"Read me! *","info":"This is where things get tricky.\n\nYou need to know how the bulbs are controlled.\n","x":1090,"y":1360,"wires":[]},{"id":"70773054ea19b204","type":"template","z":"2074fa2103de0d09","name":"","field":"count","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"BULB{{count}}","output":"str","x":870,"y":1160,"wires":[["6665161cc5a5ed06"]]},{"id":"5d3f24b1433ba9c2","type":"change","z":"2074fa2103de0d09","name":"Select bulb to control","rules":[{"t":"set","p":"topic","pt":"msg","to":"count","tot":"flow"}],"action":"","property":"","from":"","to":"","reg":false,"x":1100,"y":1320,"wires":[["712af1bd71a472cb"]]},{"id":"5d129908d383c1df","type":"link in","z":"2074fa2103de0d09","name":"Lights off","links":["a65fa1e3ac6fdd8f"],"x":1275,"y":1160,"wires":[["5d3f24b1433ba9c2"]]},{"id":"28b90dfb3da2a2d6","type":"switch","z":"2074fa2103de0d09","name":"","property":"count","propertyType":"msg","rules":[{"t":"gt","v":"0","vt":"num"}],"checkall":"true","repair":false,"outputs":1,"x":730,"y":1160,"wires":[["70773054ea19b204"]]}]

```

Optimising is not always a good thing and there are times and places to do it.

---

<div class="post-metadata">

**Author:** ![Michielo](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/michielo/32/70268_2.png) [@Michielo](https://discourse.nodered.org/u/Michielo)\
**Post date:** [11 September 2021 21:13 UTC](https://discourse.nodered.org/t/how-to-detect-multiple-presses-on-a-single-button/20560/30 "2021-09-11T21:13:07Z")

</div>

Sorry, had some computer issues.

Here is the actual flow (with some sensitive data being replaced).

I haven't made any changes so for and will look into those over the coming days.

```auto
[{"id":"65c9b63cb09879a0","type":"tab","label":"Flow 2","disabled":false,"info":""},{"id":"1139a03d.d1ca","type":"change","z":"65c9b63cb09879a0","name":"Reset","rules":[{"t":"set","p":"reset","pt":"msg","to":"foo","tot":"str"},{"t":"delete","p":"payload","pt":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":430,"y":500,"wires":[["fcb3cb17.693a9"]]},{"id":"fcb3cb17.693a9","type":"counter","z":"65c9b63cb09879a0","name":"","init":"0","step":"1","lower":null,"upper":null,"mode":"increment","outputs":"1","x":440,"y":440,"wires":[["9faf97df.4ce4a"]]},{"id":"4189a6d1.44073","type":"switch","z":"65c9b63cb09879a0","name":"count presses","property":"count","propertyType":"flow","rules":[{"t":"eq","v":"1","vt":"num"},{"t":"eq","v":"2","vt":"num"},{"t":"eq","v":"3","vt":"num"},{"t":"eq","v":"4","vt":"num"}],"checkall":"true","repair":false,"outputs":4,"x":600,"y":560,"wires":[["4b346299aded303f"],["9e68369a85e89910"],["319fd1482f217395"],[]],"info":"Just expand the flow.count to how ever many\npresses you want/need."},{"id":"ba72e20d.299b8","type":"trigger","z":"65c9b63cb09879a0","name":"","op1":"","op2":"0","op1type":"nul","op2type":"str","duration":"1","extend":true,"overrideDelay":false,"units":"s","reset":"","bytopic":"all","topic":"topic","outputs":1,"x":260,"y":560,"wires":[["62860129.ec7bd","4189a6d1.44073"]]},{"id":"9faf97df.4ce4a","type":"change","z":"65c9b63cb09879a0","name":"Flow set","rules":[{"t":"set","p":"count","pt":"flow","to":"count","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":600,"y":440,"wires":[[]]},{"id":"a1783324.3f9f","type":"comment","z":"65c9b63cb09879a0","name":"DELAY - Readme","info":"The DELAY time has to be a bit longer than how\nlong it takes you to press the button the\nmaximum number of times.\nRead information in the SWITCH node too!","x":460,"y":360,"wires":[]},{"id":"62860129.ec7bd","type":"delay","z":"65c9b63cb09879a0","name":"Delay","pauseType":"delay","timeout":"1","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"allowrate":false,"x":250,"y":500,"wires":[["1139a03d.d1ca"]]},{"id":"2f05171a0b501cfc","type":"function","z":"65c9b63cb09879a0","name":"On (50%)","func":"msg.payload = { state: 'on', color: 'normal', brightness: 125};\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1200,"y":460,"wires":[["5ad5353f077667e4","fda71aa47391a95e"]]},{"id":"5ad5353f077667e4","type":"tradfri-out","z":"65c9b63cb09879a0","name":"Dressing_1","dtype":"device","tradfri_id":"65547","hub":"f7462bcc3e35f81d","output":false,"x":1390,"y":440,"wires":[]},{"id":"b13dca185f5973db","type":"zigbee2mqtt-in","z":"65c9b63cb09879a0","name":"","server":"fc1c5c0fa4dfb0a4","friendly_name":"ON_OFF Test Switch","device_id":"0xb4e3f9fffeaee21d","state":"0","outputAtStartup":true,"x":140,"y":160,"wires":[["af5a6c71aad07e57","60d9ce35eb1c660a"]]},{"id":"af5a6c71aad07e57","type":"switch","z":"65c9b63cb09879a0","name":"payload.click","property":"payload.click","propertyType":"msg","rules":[{"t":"eq","v":"on","vt":"str"},{"t":"eq","v":"off","vt":"str"}],"checkall":"true","repair":false,"outputs":2,"x":360,"y":160,"wires":[["47b7f66d060874bf"],["b7f1086d7ed56641"]]},{"id":"47b7f66d060874bf","type":"function","z":"65c9b63cb09879a0","name":"Lights on","func":"msg.payload.Time = new Date()\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":540,"y":120,"wires":[["e0465b654d645770"]]},{"id":"b7f1086d7ed56641","type":"function","z":"65c9b63cb09879a0","name":"Lights off","func":"msg.payload = { state: 'off'};\nmsg.reset = 1;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":540,"y":200,"wires":[["79e04a367ae31c1a"]]},{"id":"e0465b654d645770","type":"link out","z":"65c9b63cb09879a0","name":"Lights on","links":["3a058dc49f4238c1"],"x":645,"y":120,"wires":[]},{"id":"79e04a367ae31c1a","type":"link out","z":"65c9b63cb09879a0","name":"Lights off","links":["a70169bcfe279017"],"x":645,"y":200,"wires":[]},{"id":"6c091b564ffbb4b3","type":"comment","z":"65c9b63cb09879a0","name":"Switch control","info":"","x":110,"y":40,"wires":[]},{"id":"c893051dcda91de5","type":"comment","z":"65c9b63cb09879a0","name":"Switch control","info":"","x":110,"y":340,"wires":[]},{"id":"3a058dc49f4238c1","type":"link in","z":"65c9b63cb09879a0","name":"Lights on","links":["e0465b654d645770"],"x":55,"y":520,"wires":[["ba72e20d.299b8","fcb3cb17.693a9"]]},{"id":"4b346299aded303f","type":"trigger","z":"65c9b63cb09879a0","name":"Auto off (10 min)","op1":"on","op2":"off","op1type":"str","op2type":"str","duration":"1","extend":false,"overrideDelay":false,"units":"min","reset":"off","bytopic":"all","topic":"topic","outputs":2,"x":820,"y":460,"wires":[["0c1733e2f48d81bd"],["5ad5353f077667e4","fda71aa47391a95e"]]},{"id":"0c1733e2f48d81bd","type":"time-range-switch","z":"65c9b63cb09879a0","name":"","lat":"55.555","lon":"5.555","startTime":"22:00","endTime":"23:00","startOffset":0,"endOffset":0,"x":1010,"y":440,"wires":[["96d9aef7f7a2bf15"],["2f05171a0b501cfc"]]},{"id":"96d9aef7f7a2bf15","type":"function","z":"65c9b63cb09879a0","name":"On (30%)","func":"msg.payload = { state: 'on', color: 'normal', brightness: 76};\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1200,"y":420,"wires":[["5ad5353f077667e4","fda71aa47391a95e"]]},{"id":"919a27e8cbdec579","type":"function","z":"65c9b63cb09879a0","name":"On (50%)","func":"msg.payload = { state: 'on', color: 'normal', brightness: 125};\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1200,"y":560,"wires":[["835ab6d24f6a1881","b4e9496fcb739307"]]},{"id":"835ab6d24f6a1881","type":"tradfri-out","z":"65c9b63cb09879a0","name":"Dressing_2","dtype":"device","tradfri_id":"65546","hub":"f7462bcc3e35f81d","output":false,"x":1390,"y":540,"wires":[]},{"id":"9e68369a85e89910","type":"trigger","z":"65c9b63cb09879a0","name":"Auto off (10 min)","op1":"on","op2":"off","op1type":"str","op2type":"str","duration":"1","extend":false,"overrideDelay":false,"units":"min","reset":"off","bytopic":"all","topic":"topic","outputs":2,"x":820,"y":560,"wires":[["cfaf602f18f910c0"],["835ab6d24f6a1881","b4e9496fcb739307"]]},{"id":"cfaf602f18f910c0","type":"time-range-switch","z":"65c9b63cb09879a0","name":"","lat":"55.555","lon":"5.555","startTime":"22:00","endTime":"23:00","startOffset":0,"endOffset":0,"x":1010,"y":540,"wires":[["ecb1aa477a2efaf4"],["919a27e8cbdec579"]]},{"id":"ecb1aa477a2efaf4","type":"function","z":"65c9b63cb09879a0","name":"On (30%)","func":"msg.payload = { state: 'on', color: 'normal', brightness: 76};\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1200,"y":520,"wires":[["835ab6d24f6a1881","b4e9496fcb739307"]]},{"id":"db18d277aa6631d3","type":"function","z":"65c9b63cb09879a0","name":"On (50%)","func":"msg.payload = { state: 'on', color: 'normal', brightness: 125};\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1200,"y":660,"wires":[["c8a0eb100c95c0cc","0b527b3814ea5232"]]},{"id":"c8a0eb100c95c0cc","type":"tradfri-out","z":"65c9b63cb09879a0","name":"Kleerkast","dtype":"device","tradfri_id":"65545","hub":"f7462bcc3e35f81d","output":false,"x":1380,"y":640,"wires":[]},{"id":"319fd1482f217395","type":"trigger","z":"65c9b63cb09879a0","name":"Auto off (10 min)","op1":"on","op2":"off","op1type":"str","op2type":"str","duration":"1","extend":false,"overrideDelay":false,"units":"min","reset":"off","bytopic":"all","topic":"topic","outputs":2,"x":820,"y":660,"wires":[["bd649c06e040eb0b"],["c8a0eb100c95c0cc","0b527b3814ea5232"]]},{"id":"bd649c06e040eb0b","type":"time-range-switch","z":"65c9b63cb09879a0","name":"","lat":"55.555","lon":"5.555","startTime":"22:00","endTime":"23:00","startOffset":0,"endOffset":0,"x":1010,"y":640,"wires":[["4640deb8d9c49602"],["db18d277aa6631d3"]]},{"id":"4640deb8d9c49602","type":"function","z":"65c9b63cb09879a0","name":"On (30%)","func":"msg.payload = { state: 'on', color: 'normal', brightness: 76};\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1200,"y":620,"wires":[["c8a0eb100c95c0cc","0b527b3814ea5232"]]},{"id":"a70169bcfe279017","type":"link in","z":"65c9b63cb09879a0","name":"Lights off","links":["79e04a367ae31c1a"],"x":1295,"y":320,"wires":[["5ad5353f077667e4","fda71aa47391a95e","835ab6d24f6a1881","b4e9496fcb739307","c8a0eb100c95c0cc","0b527b3814ea5232","4b346299aded303f","9e68369a85e89910","319fd1482f217395"]]},{"id":"fda71aa47391a95e","type":"debug","z":"65c9b63cb09879a0","name":"Light 1","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1370,"y":480,"wires":[]},{"id":"b4e9496fcb739307","type":"debug","z":"65c9b63cb09879a0","name":"Light 2","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1370,"y":580,"wires":[]},{"id":"0b527b3814ea5232","type":"debug","z":"65c9b63cb09879a0","name":"Light 3","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1370,"y":680,"wires":[]},{"id":"60d9ce35eb1c660a","type":"debug","z":"65c9b63cb09879a0","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":340,"y":100,"wires":[]},{"id":"f33cfcaf1d83f349","type":"inject","z":"65c9b63cb09879a0","name":"Reset","props":[{"p":"payload","v":"","vt":"date"},{"p":"topic","v":"","vt":"string"}],"repeat":"","crontab":"","once":true,"onceDelay":"0.5","topic":"","payload":"","payloadType":"date","x":250,"y":440,"wires":[["1139a03d.d1ca"]]},{"id":"f7462bcc3e35f81d","type":"tradfri-config","name":"Ikea Hub","hubip":"xxx.xxx.xxx.xxx","sid":"a0a0a0a0a0a0a0a0","coap":"/home/pi/.node-red/node_modules/node-tradfri-argon/lib/coap-client-raspbian","identity":"","preshared_key":""},{"id":"","type":"zigbee2mqtt-server","name":"Raspi","host":"xxx.xxx.xxx.xxx","mqtt_port":"1883","mqtt_username":"","mqtt_password":"","tls":"","usetls":false,"base_topic":"zigbee2mqtt"}]

```

---

<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:** [11 September 2021 21:16 UTC](https://discourse.nodered.org/t/how-to-detect-multiple-presses-on-a-single-button/20560/31 "2021-09-11T21:16:42Z")

</div>

It would help if you left the `inject` nodes to turn the lights on/off, or explain what their message/s are.  
(Though the button messages don't really come into play in how things are working in the flow)

I can't do anything with that because I don't know what the messages are.

And as you are using _foreign_ MQTT nodes, I can't see what they are doing.

---

<div class="post-metadata">

**Author:** ![Michielo](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/michielo/32/70268_2.png) [@Michielo](https://discourse.nodered.org/u/Michielo)\
**Post date:** [11 September 2021 21:47 UTC](https://discourse.nodered.org/t/how-to-detect-multiple-presses-on-a-single-button/20560/32 "2021-09-11T21:47:45Z")

</div>

This is what the button sends on one click

```auto
msg.payload : Object
object
action: "on"
battery: 87
click: "on"
linkquality: 0
update: object
state: "idle"
update_available: false

```

---

<div class="post-metadata">

**Author:** ![Michielo](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/michielo/32/70268_2.png) [@Michielo](https://discourse.nodered.org/u/Michielo)\
**Post date:** [11 September 2021 21:49 UTC](https://discourse.nodered.org/t/how-to-detect-multiple-presses-on-a-single-button/20560/33 "2021-09-11T21:49:42Z")

</div>

An this is what the mqtt light node receives

```auto
11-9-2021 23:45:42node: Light 1
msg.payload : Object
object
state: "on"
color: "normal"
brightness: 125

```

---

<div class="post-metadata">

**Author:** ![Michielo](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/michielo/32/70268_2.png) [@Michielo](https://discourse.nodered.org/u/Michielo)\
**Post date:** [11 September 2021 21:55 UTC](https://discourse.nodered.org/t/how-to-detect-multiple-presses-on-a-single-button/20560/34 "2021-09-11T21:55:51Z")

</div>

The payload.click `switch` node will filter on msg.payload.click for value "on" or "off" and sends that to either the "lights on" or "lights off" `function` node

[Update 1]: I'm looking at/testing your last flow now, which looks a lot cleaner than mine. The timer and auto-off function work perfectly, so thanks a lot for that. Now trying to figure out how to ensure the correct bulbs are turned on. It works with one, but adding a second one and double-clicking the button just turns both on. Haven't found your hidden trap yet. As I said, I'm new to node-red, but I appreciate the challenge and you pushing me to do more than just copy-paste your code 😅.

[Update 2]: I added a `switch` node after the "select bulb to control" `change` node routing the output to the right bulb. Not sure if that is the correct way, but I noted that with one click on the "on" `inject` node `BULB1` comes on. With two clicks (starting from all off), `BULB2` comes on. If click the "off" `inject` node, it only sends the off signal to the last bulb that was turned on. Double clicking the "on" `inject` node after `BULB1` was turned on will however not turn on `BULB2`. Furthermore, if I then click the "off" `inject` node, it sends the off signal to `BULB2` only, so I can no longer turn off `BULB1`

---

<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:** [11 September 2021 22:32 UTC](https://discourse.nodered.org/t/how-to-detect-multiple-presses-on-a-single-button/20560/35 "2021-09-11T22:32:10Z")

</div>

Ok... So I'm confused now.

Originally the flow worked - didn't it?  
You **COULD** control all 3 bulbs as needed.

Now you are saying it doesn't work.

That seems strange.

There is a fine line between learning and getting frustrated. I've been there a lot.

> [@Michielo](#):
>
> Now trying to figure out how to ensure the correct bulbs are turned on. It works with one, but adding a second one and double-clicking the button just turns both on.

How the flow works, it shouldn't be able to turn on all/both lights.

Watch the `trigger` nodes after the `switch` node.  
Press the button (the REAL one) and see what all those `trigger` nodes are doing.

I am suspicious the problem is after that.

Off topic - kind of:  
Why are you using foreign `MQTT` nodes?  
What is wrong with the ones supplied?  
I am suspicious the problem is how you have them set up.

Can you open one of them (as in _edit_ the node) and take a screen shot and post it?

Sorry, it is just as much as I have a very extensive collection of _foreign_ nodes..... I have to draw the line somewhere with how many I have and don't use.

---

<div class="post-metadata">

**Author:** ![Michielo](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/michielo/32/70268_2.png) [@Michielo](https://discourse.nodered.org/u/Michielo)\
**Post date:** [11 September 2021 22:53 UTC](https://discourse.nodered.org/t/how-to-detect-multiple-presses-on-a-single-button/20560/36 "2021-09-11T22:53:36Z")

</div>

Note that the updates that I'm posting start from the flow you posted on 5/9, so

```auto
[{"id":"afa8cb4367fce11c","type":"mqtt out","z":"220f5f63f38618da","name":"","topic":"BULB1","qos":"","retain":"","respTopic":"","contentType":"","userProps":"","correl":"","expiry":"","broker":"8941f4c3.0f151","x":1440,"y":1290,"wires":[]},{"id":"8941f4c3.0f151","type":"mqtt-broker","name":"Local","broker":"127.0.0.1","port":"1883","clientid":"","usetls":false,"compatmode":false,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","closeTopic":"","closeQos":"0","closePayload":"","willTopic":"","willQos":"0","willPayload":""}]

```

My original flow (the one I posted tonight) still works and does what it should do, i.e. one click BULB1 on, 2 clicks BULB2 on, three clicks BULB3 on and one off click turns all lights off.

The reason why I'm using a specific `mqtt-in` node is because this is a special node for the IKEA Tradfri series allowing direct communication between MQTT and the IKEA Hub. The button control is paired with my MQTT server, whereas the lights are paired to the IKEA Hub. The reason why I left the IKEA hub there, is because I want to retain the possibility to control the bulbs through IKEA's home smart app and Google Home.

```auto
Why are you using foreign `MQTT` nodes?
Can you open one of them (as in *edit* the node) and take a screen shot and post it?

```

Do you need a print screen from the `MQTT` nodes for the bulbs or the button or both?

To finish, I relay appreciate the time and effort you're putting in to help me out!

---

<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:** [11 September 2021 22:55 UTC](https://discourse.nodered.org/t/how-to-detect-multiple-presses-on-a-single-button/20560/37 "2021-09-11T22:55:02Z")

</div>

I need to see the MQTT node that sends the message to the bulb.

I really think that is where the problem is.

But until I see the node - being edited - I can't say for sure.

---

<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:** [11 September 2021 23:05 UTC](https://discourse.nodered.org/t/how-to-detect-multiple-presses-on-a-single-button/20560/38 "2021-09-11T23:05:07Z")

</div>

# UPDATE!

Ok, I imported your exported `MQTT out` node. (Why didn't you have that on the first flow you recently posted?)

I can see where you may be having the problem. But again, can't be sure.

Look at what I posted back in reply # 28.

Where the `debug` node is (incorrectly) labelled `Light 1`, put a `MQTT out` node and ONLY set the broker.

LEAVE THE TOPIC BLANK!  
Try that.

---

<div class="post-metadata">

**Author:** ![Michielo](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/michielo/32/70268_2.png) [@Michielo](https://discourse.nodered.org/u/Michielo)\
**Post date:** [11 September 2021 23:39 UTC](https://discourse.nodered.org/t/how-to-detect-multiple-presses-on-a-single-button/20560/39 "2021-09-11T23:39:08Z")

</div>

> [@Trying\_to\_learn](#):
>
> Where the `debug` node is (incorrectly) labelled `Light 1` , put a `MQTT out` node and ONLY set the broker.

Wouldn't this mean that the bulbs should be connected to the MQTT broker? As I indicated earlier, they're not directly paired to the MQTT broker (and I prefer not to change that for the reasons stated earlier). The msg goes to the `tradfri-out` node which is the IKEA hub that controls the lamp. This is probably where things don't go exactly as expected. There is no topic in the tradfri-out node, probably because it works different than a standard `MQTT-out`. The `tradfri-out` node only has a config section (where you select the IKEA Hub) and a device section (where you select the bulb). The IKEA hub works through CoAP.

As I stated earlier, in your flow + the `switch` node I added after the "select bulb to control" `change` node the commands work individually. If I combine commands (i.e. first on click for bulb1 and then a double click for bulb2, bulb2 won't come on). I'll play with it some more, to see if I can figure it out myself.

---

<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:** [11 September 2021 23:46 UTC](https://discourse.nodered.org/t/how-to-detect-multiple-presses-on-a-single-button/20560/40 "2021-09-11T23:46:55Z")

</div>

> [@Michielo](#):
>
> There is no topic in the tradfri-out node, probably because it works different than a standard `MQTT-out` .

Ok, but SOMEWHERE - be it in the IKEA HUB or else where - the message **MUST** contain information that distinguishes which bulb is to be controlled.

HAS TO BE!

You need to find that part of the message and you can optimise the flow so you get rid of the discreet section for each bulb.

Until you find that part, you can't optimise any further.

That's all I am saying. I hope you understand that.  
Then, you modify the code I showed you and you get a smaller bit of code.

Yeah, ok: In that example I use the `topic` to determine which bulb is to be used.  
(And could be why when you used it ALL bulbs were being turned on, or what ever.)

I can't help you because I have no idea how that system you have works.  
But I hope I have explained what you need to find to get what I showed you to work.  
Given you want to optimise the code more than your original example.

Good luck.

---

<div class="post-metadata">

**Author:** ![danube](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/danube/32/34348_2.png) [@danube](https://discourse.nodered.org/u/danube)\
**Post date:** [26 September 2021 18:02 UTC](https://discourse.nodered.org/t/how-to-detect-multiple-presses-on-a-single-button/20560/41 "2021-09-26T18:02:17Z")

</div>

this is great, thanks!

[Previous page](https://discourse.nodered.org/t/how-to-detect-multiple-presses-on-a-single-button/20560.md?page=1)

[Next page](https://discourse.nodered.org/t/how-to-detect-multiple-presses-on-a-single-button/20560.md?page=3)
