Detect double / long press with momentary switch

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

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. :slightly_smiling_face:

1 Like

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

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

1 Like

I’ll give it a try! Thanks

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?

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

Hope that will help someone else

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

[{"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

3 Likes

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