Detect Shelly switch firing multiple times in NodeRED

Hi everyone.

I’ve read a number of threads trying to get my head around this, but struggling to find the right examples.

I have several Shelly switches around the house, integrated with Home Assistant using the Shelly integration (I haven’t ventured into MQTT yet but thinking I might have to in order to achieve what I’m aiming for…).

I’m using NodeRED for my automations and so far things are working fine for a single switch. Using an events:state node on each of my Shelly entities I have different devices and scenes firing. What I’d like to do now is if a switch is clicked twice quickly (that could be either on/off or off/on in quick succession) then a different action will be performed.

So far using a debug node I’m able to detect if the switch is on or off but not whether it’s a multi-click. I don’t know if this is something I can get out of the payload coming from the Shelly or if I need to build in some additional logic like:

  • Fire with event:state
  • Store the new state of the switch
  • Wait 20 ms
  • Check if switch is still in the new state
  • if yes, then trigger the ‘single switch’ flow
  • if no, trigger the ‘double switch’ flow on the assumption that the switch was flicked on and then off very quickly

This feels a bit clunky so I think I’m missing something and would appreciate any guidance anyone can provide.

Thanks in advance for your help!

https://www.home-assistant.io/integrations/shelly/#listening-for-events

You can subscribe to the shelly.click

https://www.home-assistant.io/integrations/shelly/#possible-values-for-click_type

image

[{"id":"381924c6b56fa056","type":"debug","z":"c89d915bdff0f798","name":"single","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1022,"y":480,"wires":[]},{"id":"11ad6c490b9c4c75","type":"server-events","z":"c89d915bdff0f798","name":"","server":"","version":1,"event_type":"shelly.click","exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"waitForRunning":true,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"eventData"}],"x":530,"y":528,"wires":[["ef70bda4d1108be5"]]},{"id":"ef70bda4d1108be5","type":"switch","z":"c89d915bdff0f798","name":"device id","property":"payload.data.device_id","propertyType":"msg","rules":[{"t":"eq","v":"e09c64a22553484d804353ef97f6fcd6","vt":"str"}],"checkall":"true","repair":false,"outputs":1,"x":716,"y":528,"wires":[["849e243966187237"]]},{"id":"849e243966187237","type":"switch","z":"c89d915bdff0f798","name":"click type","property":"payload.data.click_type","propertyType":"msg","rules":[{"t":"eq","v":"single","vt":"str"},{"t":"eq","v":"double","vt":"str"},{"t":"eq","v":"long","vt":"str"}],"checkall":"true","repair":false,"outputs":3,"x":872,"y":528,"wires":[["381924c6b56fa056"],["fc9365d8d155f37b"],["aa33513ff84c8fec"]]},{"id":"fc9365d8d155f37b","type":"debug","z":"c89d915bdff0f798","name":"double","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1022,"y":528,"wires":[]},{"id":"aa33513ff84c8fec","type":"debug","z":"c89d915bdff0f798","name":"long","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1022,"y":576,"wires":[]}]

Thanks Kermit. That is working for me and I can see the difference between click_type = single and click_type = long.

The challenge I’m facing here is that all my switches are set to detached and sometimes when a person approaches the switch it might be in the on position. When they switch it, it changes to the off position and doesn’t fire an event because the event fired when the switch was previously switched on.

So in all my flows I’m using the events:state node to fire whenever the switch changes. My idea of a ‘double’ switch would be when a person switches it quickly on/off OR off/on. I don’t think there’s a straightforward way to detect that from the device?

The switch should output a separate on and off command when switched. Call the service switch.toggle instead of switch.turn_on/off

Yep got it, I’m using toggle which is working quite well. I think I need to do some more exploring on how to achieve double, triple etc click with some counters and logic in my flow. I’ll play around throughout the week and if I achieve what I’m after all share the results here.

Just wanted to update this, after almost giving up: It seems the nesting of click_type and device_id have moved from .data to .event in newer versions of HA and/or NR.

This is the flow that worked for me:

[{"id":"11ad6c490b9c4c75","type":"server-events","z":"5cb890a7cb20ca72","name":"","server":"908560c4.ace64","version":3,"exposeAsEntityConfig":"","eventType":"shelly.click","eventData":"","waitForRunning":true,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"eventData"}],"x":150,"y":800,"wires":[["b7a033647c81733c"]]},{"id":"849e243966187237","type":"switch","z":"5cb890a7cb20ca72","name":"click type","property":"payload.event.click_type","propertyType":"msg","rules":[{"t":"eq","v":"single_push","vt":"str"},{"t":"eq","v":"double_push","vt":"str"},{"t":"eq","v":"long_push","vt":"str"}],"checkall":"true","repair":false,"outputs":3,"x":560,"y":800,"wires":[["afbbaae55fbf2575","5f55c5c5af1d64e1"],["828c6129f87b6c28","5f55c5c5af1d64e1"],["64b7425a5f5c4ce8"]]},{"id":"b7a033647c81733c","type":"switch","z":"5cb890a7cb20ca72","name":"device id","property":"payload.event.device_id","propertyType":"msg","rules":[{"t":"eq","v":"d40f8aa01fa9374f46764668c28176ca","vt":"str"}],"checkall":"true","repair":false,"outputs":1,"x":360,"y":800,"wires":[["849e243966187237"]]},{"id":"908560c4.ace64","type":"server","name":"Vignacce HA","addon":true,"rejectUnauthorizedCerts":true,"ha_boolean":"","connectionDelay":false,"cacheJson":false,"heartbeat":false,"heartbeatInterval":"","statusSeparator":"","enableGlobalContextStore":false}]