Node Red - motion sensor switching my lights off despite not being supposed to

Hey guys,

first post here, so please forgive me if the are ettiquette and formatting errors.
I have a motion sensor that switches on my ceiling lamp. After two minutes of no motion, it is switched off again.

I want the light to stay on, when I use a switch to turn it on. So the motion sensor automation should be ignored.
I tried to achieve this by including the timer in the motion sensor flow and was hoping it wouldn’t activate, when I use another automation to operate my wall switch.

However, when I use the wall switch, the light still turns off and I do not know how to code the condition for it to only time itself, when being activated by the motion sensor.
Can anyone help me out here?

Appreciated!

This is my Node Red code:

[{"id":"2f0f9659afab76de","type":"server-state-changed","z":"119d1199edbe0381","name":"Motion Sensor","server":"3e4d51f1.af843e","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":"","forType":"num","forUnits":"minutes","ignorePrevStateNull":false,"ignorePrevStateUnknown":false,"ignorePrevStateUnavailable":false,"ignoreCurrentStateUnknown":false,"ignoreCurrentStateUnavailable":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"true","valueType":"bool"},{"property":"topic","propertyType":"msg","value":"motion","valueType":"str"},{"property":"object","propertyType":"msg","value":"","valueType":"triggerId"}],"x":200,"y":1100,"wires":[["dac9ce93158f043d"],[]]},{"id":"5904a5ddb293049f","type":"api-current-state","z":"119d1199edbe0381","name":"lux reading <15","server":"3e4d51f1.af843e","version":3,"outputs":2,"halt_if":"15","halt_if_type":"num","halt_if_compare":"lt","entity_id":"sensor.hue_motion_sensor_1_illuminance","state_type":"num","blockInputOverrides":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"entityState"},{"property":"topic","propertyType":"msg","value":"dark","valueType":"str"}],"for":"0","forType":"num","forUnits":"minutes","override_topic":false,"state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","x":560,"y":1060,"wires":[["d2e80b95b0e98f99"],[]]},{"id":"d2e80b95b0e98f99","type":"api-current-state","z":"119d1199edbe0381","name":"SZ_D","server":"3e4d51f1.af843e","version":3,"outputs":2,"halt_if":"on","halt_if_type":"str","halt_if_compare":"is","entity_id":"light.decke_schlafzimmer","state_type":"str","blockInputOverrides":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"entityState"},{"property":"topic","propertyType":"msg","value":"off","valueType":"str"}],"for":"0","forType":"num","forUnits":"minutes","override_topic":false,"state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","x":690,"y":1120,"wires":[[],["96e3f9dd073e44e1"]]},{"id":"dac9ce93158f043d","type":"time-range-switch","z":"119d1199edbe0381","name":"Time restriction","lat":"","lon":"","startTime":"14:00","endTime":"23:00","startOffset":0,"endOffset":0,"x":380,"y":1100,"wires":[["5904a5ddb293049f"],[]]},{"id":"96e3f9dd073e44e1","type":"api-call-service","z":"119d1199edbe0381","name":"SZ_D on","server":"3e4d51f1.af843e","version":5,"debugenabled":false,"domain":"light","service":"turn_on","areaId":[],"deviceId":["3593fb9d97b08be78ce0b15fd83cfc84"],"entityId":[],"data":"","dataType":"jsonata","mergeContext":"","mustacheAltTags":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"reset","valueType":"str"}],"queue":"none","x":840,"y":1100,"wires":[["680de4c218fe4743"]]},{"id":"278f38bf5dd5666e","type":"api-call-service","z":"119d1199edbe0381","name":"SZ_D off","server":"3e4d51f1.af843e","version":5,"debugenabled":false,"domain":"light","service":"turn_off","areaId":[],"deviceId":[],"entityId":["light.decke_schlafzimmer"],"data":"","dataType":"jsonata","mergeContext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":1320,"y":1080,"wires":[[]]},{"id":"409d17384b78180d","type":"controltimer","z":"119d1199edbe0381","name":"delay","timerType":"delay","timerDurationUnit":"second","timerDurationType":"num","timerDuration":120,"timerLoopTimeoutUnit":"second","timerLoopTimeoutType":"num","timerLoopTimeout":0,"loopTimeoutMessageType":"str","loopTimeoutMessage":"LOOP_TIMEOUT","timerMaxLoopIterationsType":"num","timerMaxLoopIterations":0,"loopMaxIterationsMessageType":"str","loopMaxIterationsMessage":"MAX_LOOP_ITERATIONS","isConsecutiveStartActionTimerResetAllowed":false,"isRunningTimerProgressVisible":true,"outputReceivedMessageOnTimerTrigger":true,"outputReceivedMessageOnTimerHalt":false,"startTimerOnReceivalOfUnknownMessage":true,"resetTimerOnReceivalOfUnknownMessage":true,"isDebugModeEnabled":false,"timerTriggeredMessageType":"str","timerTriggeredMessage":"TIMER_TRIGGERED","timerHaltedMessageType":"str","timerHaltedMessage":"TIMER_HALTED","isStartActionEnabled":true,"isStopActionEnabled":true,"isResetActionEnabled":true,"isPauseActionEnabled":true,"isContinueActionEnabled":true,"actionPropertyNameType":"str","actionPropertyName":"payload","startActionNameType":"str","startActionName":"START","stopActionNameType":"str","stopActionName":"STOP","resetActionNameType":"str","resetActionName":"RESET","pauseActionNameType":"str","pauseActionName":"PAUSE","continueActionNameType":"str","continueActionName":"CONTINUE","x":1150,"y":1080,"wires":[["278f38bf5dd5666e"],[]]},{"id":"680de4c218fe4743","type":"join","z":"119d1199edbe0381","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":1010,"y":1080,"wires":[["409d17384b78180d"]]},{"id":"81f7b617f53943ca","type":"server-state-changed","z":"119d1199edbe0381","name":"Motion Sensor","server":"3e4d51f1.af843e","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":"","forType":"num","forUnits":"minutes","ignorePrevStateNull":false,"ignorePrevStateUnknown":false,"ignorePrevStateUnavailable":false,"ignoreCurrentStateUnknown":false,"ignoreCurrentStateUnavailable":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"true","valueType":"bool"},{"property":"topic","propertyType":"msg","value":"motion","valueType":"str"},{"property":"object","propertyType":"msg","value":"","valueType":"triggerId"}],"x":840,"y":1040,"wires":[["680de4c218fe4743"],[]]},{"id":"3e4d51f1.af843e","type":"server","name":"Home Assistant","addon":true}]

The way I do this is the motion sensor switches on the light and starts a timer (an HA timer under Helpers). When the timer finishes, it turns off the light using code that watches for state changes on the timer (although I watch for the timer-finished event because I have one piece of code for many timers).

I have “Current state equals previous state” turned off, so continued movement in the room will keep restarting the timer, effectively extending it.

If the switch is used, I cancel the timer. Cancelling the timer does not run the “on finish” code, so the light will stay on indefinitely.

There’s couple of things to note about my situation. Firstly, the switch is in the room, so by the time you use it, the light has already turned on due to movement, and the timer started. Secondly, I’m using a Hue v1 switch that differentiates between on and off, as opposed to the v2 one that wants you to “toggle”.

Also, I doubt the join node in your code is doing what you want. It’s going to wait for any 2 messages - not one message from each of the two connected nodes. If “time restriction”, “lux”, and “SZ_D” all pass the message along, then it’s like having two identical motion nodes connected to the join, which is pointless. Alternatively, if any of them fail, then eventually two messages from the second motion node will cause the join to pass them on. So if lux stays over 15, and the sensor goes on-off-on, a timer will start after which the light will turn off, even though it’s not on.