Deconflicting Delay Node for PIR and Ikea Remote

Hello All,

Complete Newbee here (been running Node Red for about 3 days). Running NR 2.2.2, using Raspberry Pie, Zigbee Stick with Zigbee2MQTT. Managed to import the devices etc and make a basic flow allowing a hue PIR to switch on the bathroom light, and once the sensor stops capturing movement begin a delay node to turn off the light. Next step was to integrate an Ikea remote to act as the wall switch. Again been able to make that work both turning on and off the light.

What I want to do next is that if the "on" button is turned on, I want both the PIR and delay to cancel. Any help would be much appreciated!

[{"id":"72c40962d9a77f0e","type":"tab","label":"Shower Room","disabled":false,"info":"","env":[]},{"id":"7334fd33eca3aac8","type":"api-call-service","z":"72c40962d9a77f0e","name":"Shower Light On","server":"81081d78.f030b","version":5,"debugenabled":false,"domain":"light","service":"turn_on","areaId":[],"deviceId":["01b6b4dda37377e4eda068bad74df443"],"entityId":[],"data":"","dataType":"jsonata","mergeContext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":870,"y":120,"wires":[[]]},{"id":"2337786e5dbdecd4","type":"debug","z":"72c40962d9a77f0e","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":590,"y":80,"wires":[]},{"id":"820bab2a304f68b0","type":"server-state-changed","z":"72c40962d9a77f0e","name":"","server":"81081d78.f030b","version":4,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityidfilter":"binary_sensor.hue_motion_sensor_1_motion","entityidfiltertype":"exact","outputinitially":false,"state_type":"str","haltifstate":"on","halt_if_type":"str","halt_if_compare":"is","outputs":2,"output_only_on_state_change":true,"for":"0","forType":"num","forUnits":"seconds","ignorePrevStateNull":false,"ignorePrevStateUnknown":false,"ignorePrevStateUnavailable":false,"ignoreCurrentStateUnknown":false,"ignoreCurrentStateUnavailable":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"entityState"},{"property":"data","propertyType":"msg","value":"","valueType":"eventData"},{"property":"topic","propertyType":"msg","value":"","valueType":"triggerId"}],"x":200,"y":140,"wires":[["7334fd33eca3aac8","2337786e5dbdecd4"],["2337786e5dbdecd4","5921c2640321b050"]]},{"id":"3d978a6a7681ca58","type":"api-call-service","z":"72c40962d9a77f0e","name":"Shower Light Off","server":"81081d78.f030b","version":5,"debugenabled":false,"domain":"light","service":"turn_off","areaId":[],"deviceId":["01b6b4dda37377e4eda068bad74df443"],"entityId":[],"data":"","dataType":"jsonata","mergeContext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":890,"y":220,"wires":[[]]},{"id":"d0db074002a5180b","type":"mqtt in","z":"72c40962d9a77f0e","name":"","topic":"zigbee2mqtt/Shower_Switch","qos":"2","datatype":"json","broker":"3b8ec6af64b801c6","nl":false,"rap":true,"rh":0,"inputs":0,"x":220,"y":380,"wires":[["c293501579f10d88","a8dc7ce1ab7b6e0e"]]},{"id":"0b45bd5abf379e4d","type":"inject","z":"72c40962d9a77f0e","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":560,"y":240,"wires":[["3d978a6a7681ca58"]]},{"id":"6c3224654a4af61d","type":"inject","z":"72c40962d9a77f0e","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":280,"y":40,"wires":[["7334fd33eca3aac8"]]},{"id":"a8dc7ce1ab7b6e0e","type":"debug","z":"72c40962d9a77f0e","name":"Output","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":370,"y":580,"wires":[]},{"id":"c293501579f10d88","type":"switch","z":"72c40962d9a77f0e","name":"","property":"payload.action","propertyType":"msg","rules":[{"t":"eq","v":"on","vt":"str"},{"t":"eq","v":"off","vt":"str"},{"t":"eq","v":"brightness_move_up","vt":"str"},{"t":"eq","v":"brightness_move_down","vt":"str"},{"t":"eq","v":"arrow_left_click","vt":"str"},{"t":"eq","v":"arrow_right_click","vt":"str"}],"checkall":"false","repair":false,"outputs":6,"x":550,"y":480,"wires":[["71dd2a4e67ae651d","d101ee4093b854db","7334fd33eca3aac8"],["3d978a6a7681ca58","71dd2a4e67ae651d"],["71dd2a4e67ae651d"],["71dd2a4e67ae651d"],[],[]]},{"id":"71dd2a4e67ae651d","type":"debug","z":"72c40962d9a77f0e","name":"post switch","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":770,"y":680,"wires":[]},{"id":"5921c2640321b050","type":"delay","z":"72c40962d9a77f0e","name":"","pauseType":"delayv","timeout":"180","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"allowrate":false,"outputs":1,"x":580,"y":180,"wires":[["3d978a6a7681ca58"]]},{"id":"d101ee4093b854db","type":"change","z":"72c40962d9a77f0e","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"delay","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":440,"y":320,"wires":[["eaed9c963b4158d1"]]},{"id":"388936ac1a6280fd","type":"function","z":"72c40962d9a77f0e","name":"Store the delay value.","func":"if (msg.delay > 0)\n{\n    //   Delete the line below (or put // at the start)\n    node.warn(\"Delay received\");        //  this is just to show you\n    context.set('DELAY',msg.delay);\n    return;\n}\n    //   Delete the line below (or put // at the start)\n    node.warn(\"Payload received\");        //  this is just to show you\n    let delay = context.get('DELAY') || 1000;\n    msg = {payload:msg.payload,delay:delay};\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":340,"y":200,"wires":[["5921c2640321b050"]]},{"id":"eaed9c963b4158d1","type":"change","z":"72c40962d9a77f0e","name":"","rules":[{"t":"set","p":"delay","pt":"msg","to":"900000","tot":"num"}],"action":"","property":"","from":"","to":"","reg":false,"x":240,"y":280,"wires":[["388936ac1a6280fd","4a28d7c9914cedf8"]]},{"id":"4a28d7c9914cedf8","type":"debug","z":"72c40962d9a77f0e","name":"delay","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":430,"y":280,"wires":[]},{"id":"81081d78.f030b","type":"server","name":"Home Assistant","version":2,"addon":true,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true,"cacheJson":true,"heartbeat":false,"heartbeatInterval":30},{"id":"3b8ec6af64b801c6","type":"mqtt-broker","name":"core-mosquitto","broker":"localhost","port":"1883","clientid":"","autoConnect":true,"usetls":false,"protocolVersion":"4","keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthRetain":"false","birthPayload":"","birthMsg":{},"closeTopic":"","closeQos":"0","closeRetain":"false","closePayload":"","closeMsg":{},"willTopic":"","willQos":"0","willRetain":"false","willPayload":"","willMsg":{},"sessionExpiry":""}]

Welcome to the forum @Blacke1991

In order to make code readable and usable it is necessary to surround your code with three backticks (also known as a left quote or backquote ```)

``` 
   code goes here 
```

You can edit and correct your post by clicking the pencil :pencil2: icon.

See this post for more details - How to share code or flow json

I (obviously) haven't looked at the flow yet, but assuming you are using a Trigger node for the delay (which is amost certainly the easiest way) then you can configure the trigger node to cancel the delay when it receives a specific payload. You can use that to cancel it when the On button is pressed.

Thanks Colin,

Hopefully the code is now in an easier format for people to access. Im not using a trigger node but I'll have a play with that and see how I get on!

It is certainly simpler than using a delay node. You can set the trigger to Send Nothing, then wait for the timeout, then Send the message to turn off the light. If you also select Extend Delay if new message arrives then it restarts the timer each time it gets another message. Does your motion sensor send Start motion and End motion signals? If so then you might need to put a Switch node in front of the trigger so it only receives End messages.

Thanks. The problem I seem to have is that the end motion signal invariably comes after the switch has been turned on, so the end motion signal is the last message through whatever node is controlling the delay/off sequence even if the on action from the remote has triggered a 30 min delay timer.

OK. In that case you could use a node-red-contrib-simple-gate to only allow motion messages through to the trigger if the switch is off. Also make a switch action reset the trigger timer in case there is one already going.

Epic. I'll give that a go and let you know how I get on!

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