# Detect double / long press with momentary switch

**URL:** <https://discourse.nodered.org/t/detect-double-long-press-with-momentary-switch/27356>\
**Category:** General\
**Created:** [27 May 2020 07:54 UTC](https://discourse.nodered.org/t/detect-double-long-press-with-momentary-switch/27356 "2020-05-27T07:54:05Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![sansan](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/sansan/32/22776_2.png) [@sansan](https://discourse.nodered.org/u/sansan)\
**Post date:** [27 May 2020 07:54 UTC](https://discourse.nodered.org/t/detect-double-long-press-with-momentary-switch/27356/1 "2020-05-27T07:54:05Z")

</div>

Hi everyone,

I have a device that publish the state of a momentary switch to my MQTT broker.

When I press the switch my device send a 1 into a topic and then a 0 when the button is released.

Is there a simple way to detect a single press / double press and long press ?

I'm trying to make it work with the trigger node but that doesn't seems right...

Thanks for help 🥰

---

<div class="post-metadata">

**Author:** ![ristomatti](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ristomatti/32/5857_2.png) [@ristomatti](https://discourse.nodered.org/u/ristomatti)\
**Post date:** [27 May 2020 08:42 UTC](https://discourse.nodered.org/t/detect-double-long-press-with-momentary-switch/27356/2 "2020-05-27T08:42:35Z")

</div>

Someone else here can probably help you better but does the device happen to be an Arduino device running your own code? If it is, then I would say it might be simpler and more responsive to detect it on the device and send a different value for each button press type. There are handy Arduino libraries for that. But nevermind if it isn't. 🙂

---

<div class="post-metadata">

**Author:** ![sansan](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/sansan/32/22776_2.png) [@sansan](https://discourse.nodered.org/u/sansan)\
**Post date:** [27 May 2020 08:48 UTC](https://discourse.nodered.org/t/detect-double-long-press-with-momentary-switch/27356/3 "2020-05-27T08:48:50Z")

</div>

Unfortunately it’s a Shelly4Pro and I don’t think I’m able to flash it she same way I flashed my Shelly Basics...

---

<div class="post-metadata">

**Author:** ![cymplecy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cymplecy/32/2773_2.png) [@cymplecy](https://discourse.nodered.org/u/cymplecy)\
**Post date:** [27 May 2020 09:04 UTC](https://discourse.nodered.org/t/detect-double-long-press-with-momentary-switch/27356/4 "2020-05-27T09:04:03Z")

</div>

I found an old thread on this sort of thing that might help  
[https://groups.google.com/forum/#!topic/node-red/WcHYquUvLjE](https://groups.google.com/forum/#!topic/node-red/WcHYquUvLjE)

---

<div class="post-metadata">

**Author:** ![sansan](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/sansan/32/22776_2.png) [@sansan](https://discourse.nodered.org/u/sansan)\
**Post date:** [27 May 2020 09:20 UTC](https://discourse.nodered.org/t/detect-double-long-press-with-momentary-switch/27356/5 "2020-05-27T09:20:20Z")

</div>

I’ll give it a try! Thanks

---

<div class="post-metadata">

**Author:** ![sansan](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/sansan/32/22776_2.png) [@sansan](https://discourse.nodered.org/u/sansan)\
**Post date:** [27 May 2020 11:03 UTC](https://discourse.nodered.org/t/detect-double-long-press-with-momentary-switch/27356/6 "2020-05-27T11:03:24Z")

</div>

I tried the flow on the google group but the double press doesn't work, I got two "single events" for some reason msg.count stay = 1 I'm not sure why I've never used msg.count... maybe this was written for an older version of node-red?

 ![ezgif-1-7baa6fe4193e](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/8/c/8c026ad48e62c75c026d1730de4db1a3733c0f76.gif)

Edit: Sorry I think I got it, for some reason when the flow is imported the timed-counter loose some settings

 ![Capture d'écran 2020-05-27 13.10.05](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/9/d/9d6205c67a518ed27a703073e66fab07014fbbd4.png)

Hope that will help someone else

I'm leaving this here in the case that can help someone else

```auto
[{"id":"6def0a28.c0dac4","type":"timed-counter","z":"7cc53c0e.cfd8cc","name":"","timelimit":"350","timeunit":"1","withhold":true,"fixedtimeout":false,"pertopic":false,"x":2300,"y":860,"wires":[["40d79de.c9ef9e4"]]},{"id":"4442ebe6.cbd40c","type":"switch","z":"7cc53c0e.cfd8cc","name":"","property":"payload.value","propertyType":"msg","rules":[{"t":"eq","v":"1","vt":"str"},{"t":"eq","v":"0","vt":"str"}],"checkall":"false","repair":false,"outputs":2,"x":2150,"y":880,"wires":[["6def0a28.c0dac4"],["a4fade63.72ccd"]]},{"id":"40d79de.c9ef9e4","type":"function","z":"7cc53c0e.cfd8cc","name":"Single/Double/Long","func":"var pressTime;\n\nif (msg.count >= 2 && msg.payload.value == '1')\n{\n msg.payload = \"double\";\n context.set('pressTime',null);\n context.set('releaseTime',null);\n return msg;\n}\nif (msg.count == 1){\n if (msg.payload.value == '1') { //Pressed\n pressTime = Date.now(); \n //node.send({payload:{pressTime}});\n context.set('pressTime',pressTime);\n }\n pressTime = context.get('pressTime');\n //node.send({payload:{pressTime}});\n if (msg.payload.value === '0' && pressTime !== null) { //Released\n releaseTime = Date.now(); \n \n var timeDiff = releaseTime-pressTime;\n //node.send({payload:{timeDiff}});\n if (releaseTime-pressTime>350) {msg.payload = \"long\";}\n else {msg.payload = \"single\";}\n context.set('pressTime',null);\n context.set('releaseTime',null);\n return msg;\n }\n}","outputs":1,"noerr":0,"x":2500,"y":880,"wires":[["93be91ac.a3d7e8"]]},{"id":"93be91ac.a3d7e8","type":"debug","z":"7cc53c0e.cfd8cc","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":2696.250015258789,"y":880.0000019073486,"wires":[]},{"id":"a4fade63.72ccd","type":"timed-counter","z":"7cc53c0e.cfd8cc","name":"","timelimit":"350","timeunit":"1","withhold":true,"fixedtimeout":false,"pertopic":false,"x":2300,"y":900,"wires":[["40d79de.c9ef9e4"]]},{"id":"ba9d9965.589358","type":"delay","z":"7cc53c0e.cfd8cc","name":"","pauseType":"rate","timeout":"5","timeoutUnits":"seconds","rate":"8","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":1990,"y":840,"wires":[["4442ebe6.cbd40c"]]},{"id":"47c4f184.73c2d8","type":"function","z":"7cc53c0e.cfd8cc","name":"Double","func":"node.send({ payload: {\"value\":\"1\"} });\nnode.send({ payload: {\"value\":\"0\"} });\nnode.send({ payload: {\"value\":\"1\"} });\nnode.send({ payload: {\"value\":\"0\"} });","outputs":1,"noerr":0,"x":1840,"y":840,"wires":[["ba9d9965.589358"]]},{"id":"3f51bc93.7b1ec4","type":"function","z":"7cc53c0e.cfd8cc","name":"Single","func":"node.send({ payload: {\"value\":\"1\"} });\nnode.send({ payload: {\"value\":\"0\"} });","outputs":1,"noerr":0,"x":1830,"y":880,"wires":[["39aba5b6.6d3bc2"]]},{"id":"b5ddd272.b5564","type":"function","z":"7cc53c0e.cfd8cc","name":"Long","func":"node.send({ payload: {\"value\":\"1\"} });\nnode.send({ payload: {\"value\":\"0\"} });","outputs":1,"noerr":0,"x":1830,"y":920,"wires":[["ab0da844.5ade48"]]},{"id":"ab0da844.5ade48","type":"delay","z":"7cc53c0e.cfd8cc","name":"","pauseType":"rate","timeout":"5","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":1990,"y":920,"wires":[["4442ebe6.cbd40c"]]},{"id":"bb481312.8baad","type":"inject","z":"7cc53c0e.cfd8cc","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":"","x":1700,"y":840,"wires":[["47c4f184.73c2d8"]]},{"id":"2e72cb09.7d773c","type":"inject","z":"7cc53c0e.cfd8cc","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"x":1700,"y":880,"wires":[["3f51bc93.7b1ec4"]]},{"id":"1b11f668.944d2a","type":"inject","z":"7cc53c0e.cfd8cc","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"x":1700,"y":920,"wires":[["b5ddd272.b5564"]]},{"id":"39aba5b6.6d3bc2","type":"delay","z":"7cc53c0e.cfd8cc","name":"","pauseType":"rate","timeout":"5","timeoutUnits":"seconds","rate":"4","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":1990,"y":880,"wires":[["4442ebe6.cbd40c"]]},{"id":"e1561be4.c4a218","type":"comment","z":"7cc53c0e.cfd8cc","name":"Emulation of press/release events","info":"","x":1920,"y":800,"wires":[]}]

```

The timed-counter node can be found [here](https://flows.nodered.org/node/node-red-contrib-timed-counter)

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [10 June 2020 11:03 UTC](https://discourse.nodered.org/t/detect-double-long-press-with-momentary-switch/27356/7 "2020-06-10T11:03:25Z")

</div>

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