# Long press before release

**URL:** <https://discourse.nodered.org/t/long-press-before-release/31459>\
**Category:** General\
**Created:** [12 August 2020 07:32 UTC](https://discourse.nodered.org/t/long-press-before-release/31459 "2020-08-12T07:32:11Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![matisok](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/matisok/32/3445_2.png) [@matisok](https://discourse.nodered.org/u/matisok)\
**Post date:** [12 August 2020 07:32 UTC](https://discourse.nodered.org/t/long-press-before-release/31459/1 "2020-08-12T07:32:11Z")

</div>

Hi all,

I am making a coffee grinder that has a short press to turn the grinder on and a long press on the same gpio button that will start a 4min timer.

How do I make the flow with the desired logic? It is important that the code flows on once the internal long press timer (eg. 1 sec) has been reached an not when the button itself is released.

I have included some flow code but it needs the logic part.

Boiled down the logic is something like this

If button has been pressed down for more than 1 second then go immediately to output 1 (long press)  
If button has been released within 1 second then go to output 2 (short press)

Thanks  
Mathias

```auto
[{"id":"abecf103.a2149","type":"rpi-gpio in","z":"670f6ab3.f74a04","name":"","pin":"12","intype":"up","debounce":"25","read":false,"x":120,"y":220,"wires":[["31c3e88.b99fa18"]]},{"id":"632828bc.b82138","type":"rpi-gpio out","z":"670f6ab3.f74a04","name":"","pin":"13","set":true,"level":"0","freq":"","out":"out","x":560,"y":180,"wires":[]},{"id":"18694305.67c49d","type":"rpi-gpio out","z":"670f6ab3.f74a04","name":"","pin":"16","set":true,"level":"0","freq":"","out":"out","x":560,"y":260,"wires":[]},{"id":"31c3e88.b99fa18","type":"switch","z":"670f6ab3.f74a04","name":"","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"","vt":"str"},{"t":"eq","v":"","vt":"str"}],"checkall":"true","repair":false,"outputs":2,"x":310,"y":220,"wires":[["632828bc.b82138"],["18694305.67c49d"]]},{"id":"2e27f540.45fcda","type":"comment","z":"670f6ab3.f74a04","name":"Desired logic","info":"If button has been pressed down for more than 1 second then go imediately to output 1 (long press)\n\nIf button has been released within 1 second then go to ouput 2 (short press)","x":410,"y":140,"wires":[]},{"id":"ab3e274e.c68dd8","type":"inject","z":"670f6ab3.f74a04","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"0","payloadType":"num","x":150,"y":360,"wires":[["31c3e88.b99fa18"]]},{"id":"b3c32bd8.1489c8","type":"inject","z":"670f6ab3.f74a04","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"1","payloadType":"num","x":150,"y":400,"wires":[["31c3e88.b99fa18"]]},{"id":"72a22fcc.90af5","type":"comment","z":"670f6ab3.f74a04","name":"Simulated gpio press","info":"","x":120,"y":320,"wires":[]},{"id":"55081202.311eac","type":"comment","z":"670f6ab3.f74a04","name":"Relay 2","info":"","x":610,"y":140,"wires":[]},{"id":"7789d5d0.ac47fc","type":"comment","z":"670f6ab3.f74a04","name":"Relay 2","info":"","x":610,"y":220,"wires":[]}]

```

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [12 August 2020 07:46 UTC](https://discourse.nodered.org/t/long-press-before-release/31459/2 "2020-08-12T07:46:13Z")

</div>

Try this...

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/4/5/450a600b1c50bf33060ae2ed5afbeab630b35268.png)

```auto
[{"id":"ac6a38fc.7a4058","type":"inject","z":"c7164579.39cd78","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"0","payloadType":"num","x":500,"y":440,"wires":[["ec332bcd.dfd158"]]},{"id":"2194e923.160d16","type":"inject","z":"c7164579.39cd78","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"1","payloadType":"num","x":500,"y":480,"wires":[["6dc67dc0.8cf714"]]},{"id":"898d8409.5c98b8","type":"comment","z":"c7164579.39cd78","name":"Simulated gpio press","info":"","x":480,"y":400,"wires":[]},{"id":"b095250c.05cf58","type":"debug","z":"c7164579.39cd78","name":"RELAY 1","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1210,"y":420,"wires":[]},{"id":"80b1513c.55d07","type":"debug","z":"c7164579.39cd78","name":"RELAY 2","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1210,"y":500,"wires":[]},{"id":"ec332bcd.dfd158","type":"change","z":"c7164579.39cd78","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"","tot":"date"},{"t":"set","p":"topic","pt":"msg","to":"release","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":680,"y":440,"wires":[["5ea211ee.03b66"]]},{"id":"6dc67dc0.8cf714","type":"change","z":"c7164579.39cd78","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"","tot":"date"},{"t":"set","p":"topic","pt":"msg","to":"press","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":680,"y":480,"wires":[["5ea211ee.03b66"]]},{"id":"5ea211ee.03b66","type":"join","z":"c7164579.39cd78","name":"","mode":"custom","build":"object","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":true,"timeout":"","count":"2","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":860,"y":460,"wires":[["37bf1b69.577f44"]]},{"id":"37bf1b69.577f44","type":"function","z":"c7164579.39cd78","name":"","func":"if(msg.payload.press < msg.payload.release) {\n //good, release came after press :)\n var diff = msg.payload.release - msg.payload.press;\n \n //output 1: when button press between 0 ~ 1 sec\n //output 2: when button press between 1 ~ 3 sec\n \n if(diff > 0 && diff <= 1000) {\n return [msg, null]; //fire output 2\n } else if (diff > 1000 && diff <= 3000) {\n return [null, msg]; //fire output 2\n } \n \n}\n","outputs":2,"noerr":0,"initialize":"","finalize":"","x":1020,"y":460,"wires":[["b095250c.05cf58"],["80b1513c.55d07"]]},{"id":"c7e137eb.864838","type":"comment","z":"c7164579.39cd78","name":"Set payload to timestamp","info":"","x":690,"y":520,"wires":[]},{"id":"41ca66db.6b8c08","type":"comment","z":"c7164579.39cd78","name":"join them into one object","info":"","x":850,"y":400,"wires":[]},{"id":"73e95d73.dcca04","type":"comment","z":"c7164579.39cd78","name":"compare time stamps","info":"","x":1020,"y":520,"wires":[]}]

```

---

<div class="post-metadata">

**Author:** ![matisok](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/matisok/32/3445_2.png) [@matisok](https://discourse.nodered.org/u/matisok)\
**Post date:** [12 August 2020 08:11 UTC](https://discourse.nodered.org/t/long-press-before-release/31459/3 "2020-08-12T08:11:21Z")

</div>

Hi Steve,

Thanks. I tried it and hooked it up to my pin12 but it doesn't seem to fire the long press output while I am still holding the button pressed in. In your simulated gpio press this equates to sending the 0 and then I need to trigger the Relay 2 once 1 second has passed (ignoring the button release (1) on afterwards).

Hope this makes sense.

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [12 August 2020 08:31 UTC](https://discourse.nodered.org/t/long-press-before-release/31459/4 "2020-08-12T08:31:14Z")

</div>

Try this instead...

```auto
[{"id":"ac6a38fc.7a4058","type":"inject","z":"c7164579.39cd78","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"0","payloadType":"num","x":500,"y":440,"wires":[["ec332bcd.dfd158","261c6466.a5a16c"]]},{"id":"2194e923.160d16","type":"inject","z":"c7164579.39cd78","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"1","payloadType":"num","x":500,"y":480,"wires":[["6dc67dc0.8cf714"]]},{"id":"898d8409.5c98b8","type":"comment","z":"c7164579.39cd78","name":"Simulated gpio press","info":"","x":460,"y":400,"wires":[]},{"id":"b095250c.05cf58","type":"debug","z":"c7164579.39cd78","name":"Short Press RELAY","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1290,"y":400,"wires":[]},{"id":"80b1513c.55d07","type":"debug","z":"c7164579.39cd78","name":"Long Press RELAY","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1290,"y":480,"wires":[]},{"id":"ec332bcd.dfd158","type":"change","z":"c7164579.39cd78","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"","tot":"date"},{"t":"set","p":"topic","pt":"msg","to":"release","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":680,"y":440,"wires":[["5ea211ee.03b66"]]},{"id":"6dc67dc0.8cf714","type":"change","z":"c7164579.39cd78","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"","tot":"date"},{"t":"set","p":"topic","pt":"msg","to":"press","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":680,"y":480,"wires":[["5ea211ee.03b66","37b95f96.2a217"]]},{"id":"5ea211ee.03b66","type":"join","z":"c7164579.39cd78","name":"","mode":"custom","build":"object","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":true,"timeout":"","count":"2","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":910,"y":400,"wires":[["37bf1b69.577f44"]]},{"id":"37bf1b69.577f44","type":"function","z":"c7164579.39cd78","name":"","func":"if(msg.payload.press < msg.payload.release) {\n //good, release came after press :)\n var diff = msg.payload.release - msg.payload.press;\n \n //when button press between 0 ~ 1 sec\n\n if(diff > 0 && diff <= 1000) {\n msg.payload = true\n msg.topic = \"short-press\"\n return msg; //fire output 1\n } \n \n}\n","outputs":1,"noerr":0,"initialize":"","finalize":"","x":1060,"y":400,"wires":[["b095250c.05cf58"]]},{"id":"37b95f96.2a217","type":"delay","z":"c7164579.39cd78","name":"","pauseType":"delay","timeout":"1","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":920,"y":480,"wires":[["2136c7d.ed6bf38"]]},{"id":"261c6466.a5a16c","type":"change","z":"c7164579.39cd78","name":"","rules":[{"t":"set","p":"reset","pt":"msg","to":"true","tot":"bool"}],"action":"","property":"","from":"","to":"","reg":false,"x":680,"y":400,"wires":[["37b95f96.2a217"]]},{"id":"2136c7d.ed6bf38","type":"change","z":"c7164579.39cd78","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"true","tot":"bool"},{"t":"set","p":"topic","pt":"msg","to":"long-press","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":1080,"y":480,"wires":[["80b1513c.55d07"]]}]

```

---

<div class="post-metadata">

**Author:** ![matisok](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/matisok/32/3445_2.png) [@matisok](https://discourse.nodered.org/u/matisok)\
**Post date:** [12 August 2020 08:53 UTC](https://discourse.nodered.org/t/long-press-before-release/31459/5 "2020-08-12T08:53:55Z")

</div>

Brilliant. Thanks.

---

<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:** [12 August 2020 09:33 UTC](https://discourse.nodered.org/t/long-press-before-release/31459/6 "2020-08-12T09:33:55Z")

</div>

I am just seeing if I can come up with a neater solution. Which way up is your button? Normally you would have a pullup so 0 is pressed and 1 is released.

---

<div class="post-metadata">

**Author:** ![matisok](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/matisok/32/3445_2.png) [@matisok](https://discourse.nodered.org/u/matisok)\
**Post date:** [12 August 2020 09:49 UTC](https://discourse.nodered.org/t/long-press-before-release/31459/7 "2020-08-12T09:49:16Z")

</div>

Well spotted - it is as you describe. 0 on press 1 on release. (which means that in Steve's code I need to reverse the 0-1 before it enters his flow)

---

<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:** [12 August 2020 10:43 UTC](https://discourse.nodered.org/t/long-press-before-release/31459/8 "2020-08-12T10:43:55Z")

</div>

Here is another implementation, I don't know whether it is an improvement. It avoids using a function node though, which is generally worth doing if the same functionality can be done easily with standard nodes. It does use node-red-contrib-simple-gate though.

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/6/c/6cdcc19ace20fd585609df0821ad59f04837305a.png)

```auto
[{"id":"c1c5c340.d39768","type":"inject","z":"bdd7be38.d3b55","name":"Press 0","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"0","payloadType":"num","x":90,"y":1580,"wires":[["c8336735.61d09"]]},{"id":"ef7552ad.74313","type":"inject","z":"bdd7be38.d3b55","name":"Release 1","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"1","payloadType":"num","x":100,"y":1620,"wires":[["c8336735.61d09"]]},{"id":"caf24fc8.b6f0c8","type":"debug","z":"bdd7be38.d3b55","name":"Short Press RELAY","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":930,"y":1700,"wires":[]},{"id":"2319a6a3.a0dcd2","type":"debug","z":"bdd7be38.d3b55","name":"Long Press RELAY","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":930,"y":1560,"wires":[]},{"id":"3c9642ff.7e9136","type":"trigger","z":"bdd7be38.d3b55","name":"","op1":"","op2":"true","op1type":"nul","op2type":"bool","duration":"1","extend":false,"units":"s","reset":"","bytopic":"all","topic":"topic","outputs":1,"x":580,"y":1560,"wires":[["7ad58bf8.67e754"]]},{"id":"c8336735.61d09","type":"switch","z":"bdd7be38.d3b55","name":"Press?","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"0","vt":"num"},{"t":"else"}],"checkall":"true","repair":false,"outputs":2,"x":250,"y":1580,"wires":[["3c9642ff.7e9136"],["3e1c7632.c31512","52e5fb05.e7585c"]]},{"id":"2f8b1620.54195a","type":"comment","z":"bdd7be38.d3b55","name":"Press","info":"","x":350,"y":1540,"wires":[]},{"id":"3e1c7632.c31512","type":"change","z":"bdd7be38.d3b55","name":"","rules":[{"t":"set","p":"reset","pt":"msg","to":"","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":430,"y":1600,"wires":[["3c9642ff.7e9136"]]},{"id":"a5574be2.a40f1","type":"status","z":"bdd7be38.d3b55","name":"","scope":["3c9642ff.7e9136"],"x":580,"y":1600,"wires":[["b4c4b240.23e4a8"]]},{"id":"b4c4b240.23e4a8","type":"change","z":"bdd7be38.d3b55","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"status.fill ? 'open' : 'close'","tot":"jsonata"},{"t":"set","p":"topic","pt":"msg","to":"control","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":540,"y":1660,"wires":[["52e5fb05.e7585c"]]},{"id":"52e5fb05.e7585c","type":"gate","z":"bdd7be38.d3b55","name":"","controlTopic":"control","defaultState":"closed","openCmd":"open","closeCmd":"close","toggleCmd":"toggle","defaultCmd":"default","persist":false,"x":510,"y":1700,"wires":[["fcca8c7d.9b518"]]},{"id":"fcca8c7d.9b518","type":"change","z":"bdd7be38.d3b55","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"true","tot":"bool"},{"t":"set","p":"topic","pt":"msg","to":"short-press","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":720,"y":1700,"wires":[["caf24fc8.b6f0c8"]]},{"id":"7ad58bf8.67e754","type":"change","z":"bdd7be38.d3b55","name":"","rules":[{"t":"set","p":"topic","pt":"msg","to":"long-press","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":730,"y":1560,"wires":[["2319a6a3.a0dcd2"]]},{"id":"7ffbab9e.b6570c","type":"comment","z":"bdd7be38.d3b55","name":"Release","info":"","x":290,"y":1640,"wires":[]},{"id":"41e03420.c49ed4","type":"comment","z":"bdd7be38.d3b55","name":"Simulated gpio press","info":"","x":110,"y":1540,"wires":[]}]

```

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [12 August 2020 11:13 UTC](https://discourse.nodered.org/t/long-press-before-release/31459/9 "2020-08-12T11:13:07Z")

</div>

You avoid a function by adding a contrib node (gate) - I'd stick with a function tbh Colin. It wasn't really doing anything more than checking time difference.

And it also looks like you're using the status node for part of the process? Not sure that is wise (not sure why I think that's not wise but I do 😉 )

@matisok  
As for the logic inversion, that's a simple fix.

---

<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:** [12 August 2020 12:56 UTC](https://discourse.nodered.org/t/long-press-before-release/31459/10 "2020-08-12T12:56:17Z")

</div>

I wasn't sure about using the Status either, but as long as the status output is considered part of the stable spec then it shouldn't be a problem.  
The simple gate node is one of the few contrib nodes that I consider essential. I use it regularly, along with its sibling queue-gate.  
The reason I prefer to avoid a function node is that it is much more likely to have a hidden bug than other nodes. Also there are significant overheads in the runtime using a function node, though that is not normally an issue. On the other hand there are advantages in encouraging new users to use function nodes as a way of learning javascript, so there are pros and cons.  
I did start my post saying that I wasn't sure which was preferable, but thought it worth posting as an alternative.

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [12 August 2020 13:34 UTC](https://discourse.nodered.org/t/long-press-before-release/31459/11 "2020-08-12T13:34:39Z")

</div>

> [@Colin](#):
>
> Also there are significant overheads in the runtime using a function node

RE: contrib node vs built in function - I'd put money on the function being lighter and faster than any contrib node (no additional `require` / `on.event` overheads etc). I like to think of the function node as a lean custom node - doing its sole singular purpose.

Dont get me wrong, the gate node is a great node (i use it too) but i dont think the overhead is worth it here (dont mid being proven wrong though)

> [@Colin](#):
>
> I did start my post saying that I wasn't sure which was preferable, but thought it worth posting as an alternative.

No worries Colin, I don't mid being challenged - I've learned a lot myself from you guys.

---

<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:** [12 August 2020 14:09 UTC](https://discourse.nodered.org/t/long-press-before-release/31459/12 "2020-08-12T14:09:13Z")

</div>

I believe the function node sets up some sort of VM or something each time it runs which makes it a lot slower than a normal node.

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [12 August 2020 14:40 UTC](https://discourse.nodered.org/t/long-press-before-release/31459/13 "2020-08-12T14:40:52Z")

</div>

It sets up a sandbox first time it is called, and then reuses it after that. So yes there is some small overhead going in and out.

---

<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:** [12 August 2020 15:05 UTC](https://discourse.nodered.org/t/long-press-before-release/31459/14 "2020-08-12T15:05:21Z")

</div>

In this instance of course it is completely irrelevant.

---

<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:** [12 August 2020 15:19 UTC](https://discourse.nodered.org/t/long-press-before-release/31459/15 "2020-08-12T15:19:14Z")

</div>

> [@Steve-Mcl](#):
>
> I don't mid being challenged

My alternative flow was not intended in any way to challenge yours. I started off thinking that I might end up with a significantly simpler flow using the trigger node, but in the end it worked out very similar in complexity, due to having to mess about with the status. I still have a feeling there aught to be a neater way though.

---

<div class="post-metadata">

**Author:** ![matisok](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/matisok/32/3445_2.png) [@matisok](https://discourse.nodered.org/u/matisok)\
**Post date:** [12 August 2020 15:25 UTC](https://discourse.nodered.org/t/long-press-before-release/31459/16 "2020-08-12T15:25:13Z")

</div>

Both flow works beautifully here so thanks. There's definitely and advantage for me if it can be done without any custom nodes, but I am equally fond of as simple node structures as possible. Performance is purely academic in this application as noted. (Therefore of interest of course).

Now the next logic step presents itself for me: once the relays are "on" then I want the subsequent press to turn the relay off. And to make matters a little bit more challenging then I would like the "off" press to be a short click for _both_ the Short Relay and Long Relay.

Mathias

---

<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:** [12 August 2020 15:33 UTC](https://discourse.nodered.org/t/long-press-before-release/31459/17 "2020-08-12T15:33:00Z")

</div>

Before looking at that, I have improved mine a bit by removing the status node requirement. Still a bit messy though.

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/e/2/e2c80f97a4769b0bd27358c10e42365ffeebaf16.png)

```auto
[{"id":"43cfd67a.6bce5","type":"inject","z":"bdd7be38.d3b55","name":"Press 0","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"0","payloadType":"num","x":90,"y":1840,"wires":[["ae7961b0.d87628"]]},{"id":"3121ec58.1c8b94","type":"inject","z":"bdd7be38.d3b55","name":"Release 1","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"1","payloadType":"num","x":100,"y":1880,"wires":[["ae7961b0.d87628"]]},{"id":"30b3f9ca.009afe","type":"debug","z":"bdd7be38.d3b55","name":"Short Press RELAY","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":910,"y":1980,"wires":[]},{"id":"32c4ac95.0e2774","type":"debug","z":"bdd7be38.d3b55","name":"Long Press RELAY","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":930,"y":1820,"wires":[]},{"id":"974d9aac.eeea18","type":"trigger","z":"bdd7be38.d3b55","name":"","op1":"","op2":"true","op1type":"nul","op2type":"bool","duration":"1","extend":false,"units":"s","reset":"","bytopic":"all","topic":"topic","outputs":1,"x":580,"y":1820,"wires":[["f349caf0.3891a","80b6ab31.a6c12"]]},{"id":"ae7961b0.d87628","type":"switch","z":"bdd7be38.d3b55","name":"Press?","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"0","vt":"num"},{"t":"else"}],"checkall":"true","repair":false,"outputs":2,"x":250,"y":1840,"wires":[["974d9aac.eeea18","6b81f375.1d19ac"],["92ada8bb.384bb","217c4822.1be01"]]},{"id":"68fb2663.33c2c","type":"comment","z":"bdd7be38.d3b55","name":"Press","info":"","x":350,"y":1800,"wires":[]},{"id":"92ada8bb.384bb","type":"change","z":"bdd7be38.d3b55","name":"","rules":[{"t":"set","p":"reset","pt":"msg","to":"","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":430,"y":1860,"wires":[["974d9aac.eeea18"]]},{"id":"80b6ab31.a6c12","type":"change","z":"bdd7be38.d3b55","name":"close","rules":[{"t":"set","p":"payload","pt":"msg","to":"close","tot":"str"},{"t":"set","p":"topic","pt":"msg","to":"control","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":730,"y":1880,"wires":[["217c4822.1be01"]]},{"id":"217c4822.1be01","type":"gate","z":"bdd7be38.d3b55","name":"","controlTopic":"control","defaultState":"closed","openCmd":"open","closeCmd":"close","toggleCmd":"toggle","defaultCmd":"default","persist":false,"x":530,"y":1980,"wires":[["c51688d7.e998b","80b6ab31.a6c12"]]},{"id":"c51688d7.e998b","type":"change","z":"bdd7be38.d3b55","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"true","tot":"bool"},{"t":"set","p":"topic","pt":"msg","to":"short-press","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":700,"y":1980,"wires":[["30b3f9ca.009afe"]]},{"id":"f349caf0.3891a","type":"change","z":"bdd7be38.d3b55","name":"","rules":[{"t":"set","p":"topic","pt":"msg","to":"long-press","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":730,"y":1820,"wires":[["32c4ac95.0e2774"]]},{"id":"b9b74edf.41abf8","type":"comment","z":"bdd7be38.d3b55","name":"Release","info":"","x":290,"y":1900,"wires":[]},{"id":"3cd5e5ab.1532aa","type":"comment","z":"bdd7be38.d3b55","name":"Simulated gpio press","info":"","x":110,"y":1800,"wires":[]},{"id":"6b81f375.1d19ac","type":"change","z":"bdd7be38.d3b55","name":"open","rules":[{"t":"set","p":"payload","pt":"msg","to":"open","tot":"str"},{"t":"set","p":"topic","pt":"msg","to":"control","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":490,"y":1900,"wires":[["217c4822.1be01"]]}]

```

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [12 August 2020 15:41 UTC](https://discourse.nodered.org/t/long-press-before-release/31459/18 "2020-08-12T15:41:16Z")

</div>

> [@Colin](#):
>
> I still have a feeling there aught to be a neater way though.

I find Dave (annoyingly) does that often 😉

> [@Colin](#):
>
> My alternative flow was not intended in any way to challenge yours.

Bad choice of words Colin - all in good faith

---

<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:** [12 August 2020 15:41 UTC](https://discourse.nodered.org/t/long-press-before-release/31459/19 "2020-08-12T15:41:57Z")

</div>

> [@matisok](#):
>
> Now the next logic step presents itself for me:

Are you using MQTT to keep the state of the relays?

---

<div class="post-metadata">

**Author:** ![matisok](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/matisok/32/3445_2.png) [@matisok](https://discourse.nodered.org/u/matisok)\
**Post date:** [12 August 2020 17:09 UTC](https://discourse.nodered.org/t/long-press-before-release/31459/20 "2020-08-12T17:09:54Z")

</div>

I am not - would that be a good idea? Right now the output of your flow goes directly to my other code which is a bit of timer countdown and a grinder motor on/off (simulated). I included the code here, but the timer bit is really messy. But it is this timer flow that I need to be able to get out off (reset) with a single short press.

```auto
[{"id":"5611aceb.191cc4","type":"rpi-gpio out","z":"f50f27f5.e841b8","name":"","pin":"35","set":true,"level":"0","freq":"100","out":"out","x":300,"y":240,"wires":[]},{"id":"aa7429d7.50f5e8","type":"inject","z":"f50f27f5.e841b8","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"1","payloadType":"num","x":130,"y":220,"wires":[["5611aceb.191cc4"]]},{"id":"a1406b66.73a3a8","type":"inject","z":"f50f27f5.e841b8","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"0","payloadType":"num","x":130,"y":260,"wires":[["5611aceb.191cc4"]]},{"id":"6952c2ca.d7f98c","type":"rpi-gpio out","z":"f50f27f5.e841b8","name":"","pin":"37","set":true,"level":"0","freq":"100","out":"out","x":1280,"y":140,"wires":[]},{"id":"5084f185.5092a","type":"inject","z":"f50f27f5.e841b8","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"0","payloadType":"num","x":1170,"y":240,"wires":[["6952c2ca.d7f98c"]]},{"id":"53e9e007.d6208","type":"change","z":"f50f27f5.e841b8","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"true","tot":"bool"},{"t":"set","p":"topic","pt":"msg","to":"short-press","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":740,"y":220,"wires":[["de178924.06e208"]]},{"id":"3cc2b3c6.b2b5fc","type":"gate","z":"f50f27f5.e841b8","name":"","controlTopic":"control","defaultState":"closed","openCmd":"open","closeCmd":"close","toggleCmd":"toggle","defaultCmd":"default","persist":false,"x":530,"y":220,"wires":[["53e9e007.d6208"]]},{"id":"f71f3524.8f0598","type":"switch","z":"f50f27f5.e841b8","name":"Press?","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"0","vt":"num"},{"t":"else"}],"checkall":"true","repair":false,"outputs":2,"x":270,"y":100,"wires":[["cdc7e7d9.ed1cf8"],["3cc2b3c6.b2b5fc","bfb5c4d4.f33ca8"]]},{"id":"6cbe7ec3.94877","type":"change","z":"f50f27f5.e841b8","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"status.fill ? 'open' : 'close'","tot":"jsonata"},{"t":"set","p":"topic","pt":"msg","to":"control","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":560,"y":180,"wires":[["3cc2b3c6.b2b5fc"]]},{"id":"2c288744.30d318","type":"inject","z":"f50f27f5.e841b8","name":"Press 0","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"0","payloadType":"num","x":110,"y":100,"wires":[["f71f3524.8f0598"]]},{"id":"255b9cb5.6513e4","type":"inject","z":"f50f27f5.e841b8","name":"Release 1","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"1","payloadType":"num","x":120,"y":160,"wires":[["f71f3524.8f0598"]]},{"id":"cdc7e7d9.ed1cf8","type":"trigger","z":"f50f27f5.e841b8","name":"","op1":"","op2":"true","op1type":"nul","op2type":"bool","duration":"1","extend":false,"units":"s","reset":"","bytopic":"all","topic":"topic","outputs":1,"x":600,"y":80,"wires":[["8b1771c8.ed13a"]]},{"id":"bfb5c4d4.f33ca8","type":"change","z":"f50f27f5.e841b8","name":"","rules":[{"t":"set","p":"reset","pt":"msg","to":"","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":450,"y":120,"wires":[["cdc7e7d9.ed1cf8"]]},{"id":"9bb69aab.7d6398","type":"rpi-gpio in","z":"f50f27f5.e841b8","name":"","pin":"36","intype":"up","debounce":"25","read":false,"x":100,"y":40,"wires":[["f71f3524.8f0598"]]},{"id":"b28b1e5f.e8614","type":"status","z":"f50f27f5.e841b8","name":"","scope":["cdc7e7d9.ed1cf8"],"x":620,"y":120,"wires":[["6cbe7ec3.94877"]]},{"id":"8b1771c8.ed13a","type":"change","z":"f50f27f5.e841b8","name":"","rules":[{"t":"set","p":"topic","pt":"msg","to":"long-press","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":780,"y":80,"wires":[["fb4151e9.0dfe8"]]},{"id":"cdb28524.f35bd8","type":"rpi-gpio out","z":"f50f27f5.e841b8","name":"","pin":"33","set":true,"level":"0","freq":"100","out":"out","x":1060,"y":940,"wires":[]},{"id":"7a12ca40.1ce0f4","type":"inject","z":"f50f27f5.e841b8","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"0","payloadType":"num","x":870,"y":940,"wires":[["cdb28524.f35bd8"]]},{"id":"2a878da6.5bc182","type":"countdown","z":"f50f27f5.e841b8","name":"red 4","topic":"Timer1","payloadTimerStart":"true","payloadTimerStartType":"bool","payloadTimerStop":"false","payloadTimerStopType":"bool","timer":"10","resetWhileRunning":false,"setTimeToNewWhileRunning":false,"startCountdownOnControlMessage":false,"x":510,"y":540,"wires":[["721a4d2f.265804","1a798e86.ae4b21","cdb28524.f35bd8"],[]]},{"id":"721a4d2f.265804","type":"trigger","z":"f50f27f5.e841b8","name":"","op1":"1","op2":"0","op1type":"str","op2type":"str","duration":"400","extend":false,"units":"ms","reset":"","bytopic":"all","topic":"topic","outputs":1,"x":1300,"y":900,"wires":[["e98fa94.3f87a58"]]},{"id":"1a798e86.ae4b21","type":"switch","z":"f50f27f5.e841b8","name":"","property":"payload","propertyType":"msg","rules":[{"t":"true"},{"t":"false"}],"checkall":"true","repair":false,"outputs":2,"x":450,"y":400,"wires":[["ee1f961e.33d1c8"],["ef97e484.696558","a323366b.a64d38","ee1f961e.33d1c8"]]},{"id":"a323366b.a64d38","type":"InterruptFlowUltimate","z":"f50f27f5.e841b8","name":"Gok","triggertopic":"Timer1","x":470,"y":660,"wires":[["2a878da6.5bc182"]]},{"id":"e98fa94.3f87a58","type":"rpi-gpio out","z":"f50f27f5.e841b8","name":"","pin":"13","set":true,"level":"0","freq":"","out":"out","x":1560,"y":680,"wires":[]},{"id":"ef97e484.696558","type":"InvertUltimate","z":"f50f27f5.e841b8","name":"Invert","x":870,"y":400,"wires":[["495854.a1d847ac","e1da5fc0.c280e"]]},{"id":"ee1f961e.33d1c8","type":"change","z":"f50f27f5.e841b8","name":"","rules":[{"t":"set","p":"topic","pt":"msg","to":"trigger","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":800,"y":520,"wires":[["ca21a163.17be"]]},{"id":"e1da5fc0.c280e","type":"BlinkerUltimate","z":"f50f27f5.e841b8","name":"Blinker","blinkfrequency":"250","x":1020,"y":500,"wires":[["cdb28524.f35bd8"]]},{"id":"fb4151e9.0dfe8","type":"switch","z":"f50f27f5.e841b8","name":"","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"1","vt":"num"},{"t":"eq","v":"0","vt":"str"}],"checkall":"true","repair":false,"outputs":2,"x":230,"y":800,"wires":[["a323366b.a64d38","2766c3e3.e4fa8c"],[]]},{"id":"d2d90d90.b1448","type":"delay","z":"f50f27f5.e841b8","name":"","pauseType":"delay","timeout":"500","timeoutUnits":"milliseconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":630,"y":880,"wires":[["a323366b.a64d38"]]},{"id":"495854.a1d847ac","type":"trigger","z":"f50f27f5.e841b8","name":"","op1":"1","op2":"0","op1type":"str","op2type":"str","duration":"1","extend":false,"units":"s","reset":"","bytopic":"all","topic":"topic","outputs":1,"x":1360,"y":540,"wires":[["e98fa94.3f87a58"]]},{"id":"ca21a163.17be","type":"InvertUltimate","z":"f50f27f5.e841b8","name":"Invert","x":830,"y":640,"wires":[["95c6dccb.47cc5"]]},{"id":"1cc1395c.d51647","type":"change","z":"f50f27f5.e841b8","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"false","tot":"bool"}],"action":"","property":"","from":"","to":"","reg":false,"x":1060,"y":700,"wires":[["e1da5fc0.c280e","669fca85.5cdeb4"]]},{"id":"2766c3e3.e4fa8c","type":"delay","z":"f50f27f5.e841b8","name":"","pauseType":"delay","timeout":"50","timeoutUnits":"milliseconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":470,"y":780,"wires":[["95c6dccb.47cc5"]]},{"id":"b138bb54.bf0038","type":"change","z":"f50f27f5.e841b8","name":"","rules":[{"t":"set","p":"topic","pt":"msg","to":"Timer1","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":1280,"y":800,"wires":[["d2d90d90.b1448"]]},{"id":"95c6dccb.47cc5","type":"InterruptFlowUltimate","z":"f50f27f5.e841b8","name":"Interrupt Flow","triggertopic":"trigger","x":800,"y":760,"wires":[["1cc1395c.d51647"]]},{"id":"669fca85.5cdeb4","type":"InvertUltimate","z":"f50f27f5.e841b8","name":"Invert","x":1070,"y":760,"wires":[["b138bb54.bf0038"]]},{"id":"de178924.06e208","type":"trigger","z":"f50f27f5.e841b8","name":"","op1":"1","op2":"0","op1type":"str","op2type":"str","duration":"4","extend":false,"units":"s","reset":"","bytopic":"all","topic":"topic","outputs":1,"x":940,"y":220,"wires":[["6952c2ca.d7f98c"]]},{"id":"cfd18b02.490998","type":"ui_button","z":"f50f27f5.e841b8","name":"","group":"50ee66b2.914c18","order":0,"width":0,"height":0,"passthru":false,"label":"Reset now","tooltip":"","color":"","bgcolor":"","icon":"","payload":"false","payloadType":"bool","topic":"","x":290,"y":540,"wires":[["2a878da6.5bc182"]]},{"id":"7018b3b1.d2eeec","type":"comment","z":"f50f27f5.e841b8","name":"Buzzer","info":"","x":1550,"y":640,"wires":[]},{"id":"5c5f4658.b6a7e8","type":"comment","z":"f50f27f5.e841b8","name":"GREEN LED ","info":"","x":1070,"y":900,"wires":[]},{"id":"1d2e3eeb.435d11","type":"comment","z":"f50f27f5.e841b8","name":"BLUE LED ","info":"","x":1280,"y":100,"wires":[]},{"id":"51dae61b.b85af8","type":"comment","z":"f50f27f5.e841b8","name":"RED LED ","info":"","x":300,"y":200,"wires":[]},{"id":"f068e672.c59168","type":"comment","z":"f50f27f5.e841b8","name":"Simulate grinder motor (4s)","info":"","x":990,"y":180,"wires":[]},{"id":"50ee66b2.914c18","type":"ui_group","z":"","name":"Reset all timers","tab":"37955e3d.2fd1b2","order":3,"disp":true,"width":"6","collapse":false},{"id":"37955e3d.2fd1b2","type":"ui_tab","z":"","name":"QuickTimer","icon":"dashboard","disabled":false,"hidden":false}]

```

[Next page](https://discourse.nodered.org/t/long-press-before-release/31459.md?page=2)
