Motion sensor and light

Hello, I’m just starting to learn Node-Red, I have a zigbee motion sensor and a zigbee light switch, I want to do this:

  1. when motion was detected, the light turned on, the motion disappeared, the light turned off after a certain time, but if motion was detected again at this time, the countdown was reset until the motion completely disappeared and then only turned off after a specified period of time
  2. but if the light is turned on manually using a switch, then it should not turn off according to the diagram in point 1.
    I managed to do the first option, but I can’t figure out how to implement the second, all attempts end up in some kind of wrong loop, please help.

[{"id":"05965022c18821fb","type":"tab","label":"Площадка","disabled":false,"info":"","env":[]},{"id":"2edd61d83a05ca74","type":"server-state-changed","z":"05965022c18821fb","name":"motion_plochadka","server":"10cea322.6322ad","version":5,"outputs":2,"exposeAsEntityConfig":"","entityId":"binary_sensor.ploshadka","entityIdType":"exact","outputInitially":false,"stateType":"str","ifState":"on","ifStateType":"str","ifStateOperator":"is","outputOnlyOnStateChange":false,"for":"0","forType":"num","forUnits":"minutes","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":90,"y":120,"wires":[["944e86233048e911"],["944e86233048e911"]]},{"id":"944e86233048e911","type":"switch","z":"05965022c18821fb","name":"on and off","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"on","vt":"str"},{"t":"eq","v":"off","vt":"str"}],"checkall":"true","repair":false,"outputs":2,"x":280,"y":120,"wires":[["884b8102ccd0346e","2a45a3bc07f63482"],["86c21caeb5621e4d"]]},{"id":"884b8102ccd0346e","type":"api-current-state","z":"05965022c18821fb","name":"light_ploshadka","server":"10cea322.6322ad","version":3,"outputs":2,"halt_if":"off","halt_if_type":"str","halt_if_compare":"is","entity_id":"light.ploshchadka","state_type":"str","blockInputOverrides":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"entityState"},{"property":"data","propertyType":"msg","value":"","valueType":"entity"}],"for":"0","forType":"num","forUnits":"minutes","override_topic":false,"state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","x":460,"y":80,"wires":[["b92a794b9e2bb7e7"],[]]},{"id":"b92a794b9e2bb7e7","type":"api-call-service","z":"05965022c18821fb","name":"On","server":"10cea322.6322ad","version":5,"debugenabled":false,"domain":"light","service":"turn_on","areaId":[],"deviceId":[],"entityId":["light.ploshchadka"],"data":"","dataType":"jsonata","mergeContext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":650,"y":80,"wires":[[]]},{"id":"cffc96e58c6f5e9b","type":"trigger","z":"05965022c18821fb","name":"","op1":"","op2":"0","op1type":"nul","op2type":"str","duration":"10","extend":true,"overrideDelay":false,"units":"s","reset":"","bytopic":"all","topic":"topic","outputs":1,"x":630,"y":200,"wires":[["d499594ac25118f7"]]},{"id":"d499594ac25118f7","type":"api-call-service","z":"05965022c18821fb","name":"Off","server":"10cea322.6322ad","version":5,"debugenabled":false,"domain":"light","service":"turn_off","areaId":[],"deviceId":[],"entityId":["light.ploshchadka"],"data":"","dataType":"jsonata","mergeContext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":490,"y":280,"wires":[[]]},{"id":"86c21caeb5621e4d","type":"time-range-switch","z":"05965022c18821fb","name":"","lat":"","lon":"","startTime":"07:00","endTime":"17:00","startOffset":"0","endOffset":0,"x":390,"y":200,"wires":[["cffc96e58c6f5e9b"],[]]},{"id":"2a45a3bc07f63482","type":"change","z":"05965022c18821fb","name":"Stop","rules":[{"t":"set","p":"reset","pt":"msg","to":"true","tot":"bool"}],"action":"","property":"","from":"","to":"","reg":false,"x":470,"y":140,"wires":[["cffc96e58c6f5e9b"]]},{"id":"10cea322.6322ad","type":"server","name":"Home Assistant","addon":true}]

Something like this?

yes, judging by your description, this is what you need and I have already met this and tried it, but as I wrote above, I just started studying and for some reason I can’t do it according to you, for some reason when the light turns on, it doesn’t matter what from the switch or from the sensor itself for the first time, then the sensor goes into blocked mode and then nothing happens, neither turning off nor turning on, apparently I’m doing something wrong.
I get the feeling that this is impossible to implement, it’s just that even if you follow the logic, everything will get looped in one of the nodes and, as a result, will not work. But I really believe that I’m wrong.

when the motion sensor is triggered, the light turns on and the trigger state goes into disable mode

after the motion sensor, not seeing the presence, goes into off mode due to the fact that the trigger state goes into disable, then nothing happens further and the light still remains on

and as you can see in the picture, even if you turn off the light with your hands, for some reason the trigger state still goes into disable, that’s where I stopped and I can’t understand what I’m doing wrong and for this reason I wrote to the forum.

Try this

[{"id":"ec0c2230626a4e60","type":"trigger-state","z":"60f2d2277843c698","name":"","server":"","version":4,"inputs":0,"outputs":2,"exposeAsEntityConfig":"","entityId":["binary_sensor.g3_instant_motion_2"],"entityIdType":"list","debugEnabled":false,"constraints":[{"targetType":"this_entity","targetValue":"","propertyType":"current_state","propertyValue":"new_state.state","comparatorType":"is","comparatorValueDatatype":"str","comparatorValue":"on"}],"customOutputs":[],"outputInitially":false,"stateType":"str","enableInput":false,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"x":200,"y":1680,"wires":[["2b1e14d658514562"],[]]},{"id":"132371cb6c6eb611","type":"api-call-service","z":"60f2d2277843c698","name":"","server":"","version":5,"debugenabled":false,"domain":"light","service":"turn_{{payload}}","areaId":[],"deviceId":[],"entityId":["light.dj"],"data":"","dataType":"jsonata","mergeContext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":1060,"y":1620,"wires":[[]]},{"id":"2b1e14d658514562","type":"trigger","z":"60f2d2277843c698","name":"","op1":"on","op2":"off","op1type":"str","op2type":"str","duration":"5","extend":true,"overrideDelay":true,"units":"s","reset":"","bytopic":"all","topic":"topic","outputs":1,"x":500,"y":1680,"wires":[["8de7dadf8b83a874"]]},{"id":"5909964afbf2b3ed","type":"server-state-changed","z":"60f2d2277843c698","name":"","server":"","version":5,"outputs":1,"exposeAsEntityConfig":"","entityId":"switch.kitchen_counter","entityIdType":"exact","outputInitially":false,"stateType":"str","ifState":"","ifStateType":"str","ifStateOperator":"is","outputOnlyOnStateChange":true,"for":"0","forType":"num","forUnits":"minutes","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":170,"y":1620,"wires":[["132371cb6c6eb611"]]},{"id":"8de7dadf8b83a874","type":"api-current-state","z":"60f2d2277843c698","name":"","server":"","version":3,"outputs":2,"halt_if":"off","halt_if_type":"str","halt_if_compare":"is","entity_id":"switch.kitchen_counter","state_type":"str","blockInputOverrides":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"msg.payload","valueType":"jsonata"},{"property":"data","propertyType":"msg","value":"","valueType":"entity"}],"for":"0","forType":"num","forUnits":"minutes","override_topic":false,"state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","x":750,"y":1680,"wires":[["132371cb6c6eb611"],[]]}]

The flow will need a few changes depending on how your light switch works.

Try this.

[{"id":"2144927790d1c79a","type":"api-call-service","z":"f25b7db6c0659c8b","name":"Kitchen Ceiling Lights ON","server":"","version":5,"debugenabled":false,"domain":"light","service":"turn_on","areaId":[],"deviceId":[],"entityId":["light.kitchen_ceiling_lights"],"data":"","dataType":"json","mergeContext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","output_location":"","output_location_type":"none","x":750,"y":510,"wires":[[]]},{"id":"e6849dbb0eed95b0","type":"trigger","z":"f25b7db6c0659c8b","name":"10min","op1":"","op2":"","op1type":"nul","op2type":"pay","duration":"1","extend":true,"units":"min","reset":"","bytopic":"all","outputs":1,"x":690,"y":590,"wires":[["17fcb3d81db1190e"]]},{"id":"17fcb3d81db1190e","type":"api-call-service","z":"f25b7db6c0659c8b","name":"Kitchen Ceiling Lights OFF","server":"","version":5,"debugenabled":false,"domain":"light","service":"turn_off","areaId":[],"deviceId":[],"entityId":["light.kitchen_ceiling_lights"],"data":"","dataType":"json","mergeContext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","output_location":"","output_location_type":"none","x":880,"y":590,"wires":[[]]},{"id":"b0911cfe3f7c354e","type":"change","z":"f25b7db6c0659c8b","name":"RESET","rules":[{"t":"set","p":"reset","pt":"msg","to":"true","tot":"bool"}],"action":"","property":"","from":"","to":"","reg":false,"x":655,"y":550,"wires":[["e6849dbb0eed95b0"]],"icon":"node-red/timer.svg","l":false},{"id":"5ba1a398291ef9e6","type":"trigger-state","z":"f25b7db6c0659c8b","name":"Kitchen Light Switch","server":"","version":4,"inputs":1,"outputs":2,"exposeAsEntityConfig":"","entityId":"binary_sensor.kitchen_light_switch","entityIdType":"exact","debugEnabled":false,"constraints":[{"targetType":"entity_id","targetValue":"light.kitchen_ceiling_lights","propertyType":"current_state","propertyValue":"new_state.state","comparatorType":"is","comparatorValueDatatype":"str","comparatorValue":"off"}],"customOutputs":[],"outputInitially":false,"stateType":"str","enableInput":true,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"x":240,"y":550,"wires":[["86fb2da68b39ab9e","2144927790d1c79a"],["c724ac3d6ae55dd8","17fcb3d81db1190e"]]},{"id":"94941d2eb7192fe8","type":"trigger-state","z":"f25b7db6c0659c8b","name":"Motion Sensor","server":"","version":4,"inputs":1,"outputs":2,"exposeAsEntityConfig":"","entityId":"binary_sensor.dining_room_motion_sensor","entityIdType":"exact","debugEnabled":false,"constraints":[{"targetType":"this_entity","targetValue":"","propertyType":"current_state","propertyValue":"new_state.state","comparatorType":"is","comparatorValueDatatype":"str","comparatorValue":"on"}],"customOutputs":[],"outputInitially":false,"stateType":"str","enableInput":true,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"x":490,"y":550,"wires":[["2144927790d1c79a","b0911cfe3f7c354e"],["e6849dbb0eed95b0"]]},{"id":"c724ac3d6ae55dd8","type":"change","z":"f25b7db6c0659c8b","name":"ENABLE","rules":[{"t":"set","p":"payload","pt":"msg","to":"enable","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":375,"y":580,"wires":[["94941d2eb7192fe8","b0911cfe3f7c354e"]],"icon":"font-awesome/fa-toggle-on","l":false},{"id":"86fb2da68b39ab9e","type":"change","z":"f25b7db6c0659c8b","name":"DISABLE","rules":[{"t":"set","p":"payload","pt":"msg","to":"disable","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":375,"y":520,"wires":[["94941d2eb7192fe8","b0911cfe3f7c354e"]],"icon":"font-awesome/fa-toggle-off","l":false}]

image

I’m saying that apparently I don’t fully understand something, why do you have two light devices in the Kitchen Light Switch: light.kitchen_ceiling_lights and binary_sensor.kitchen_light_switch?
Is this just a typo or is a second physical or software switch needed?
Somehow I have a feeling this can’t be done with two devices (zigbee switch and zigbee motion sensor)?
Perhaps I did not describe the situation quite correctly, I have only two devices in this room, a motion sensor (binary_sensor.motion_sensor_2) and a switch (switch.apendiks_switch_1) that turns on the light.
Is this enough or not enough to achieve what I want?

If it’s only the two entities, this is all you need. The current state node, use the switch, when on blocks all messages from the motion sensor.

[{"id":"ec0c2230626a4e60","type":"trigger-state","z":"60f2d2277843c698","name":"","server":"","version":4,"inputs":0,"outputs":2,"exposeAsEntityConfig":"","entityId":["binary_sensor.g3_instant_motion_2"],"entityIdType":"list","debugEnabled":false,"constraints":[{"targetType":"this_entity","targetValue":"","propertyType":"current_state","propertyValue":"new_state.state","comparatorType":"is","comparatorValueDatatype":"str","comparatorValue":"on"}],"customOutputs":[],"outputInitially":false,"stateType":"str","enableInput":false,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"x":1780,"y":580,"wires":[["2b1e14d658514562"],[]]},{"id":"132371cb6c6eb611","type":"api-call-service","z":"60f2d2277843c698","name":"","server":"","version":5,"debugenabled":false,"domain":"switch","service":"turn_{{payload}}","areaId":[],"deviceId":[],"entityId":["switch.kitchen_counter"],"data":"","dataType":"jsonata","mergeContext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":2670,"y":580,"wires":[[]]},{"id":"2b1e14d658514562","type":"trigger","z":"60f2d2277843c698","name":"","op1":"on","op2":"off","op1type":"str","op2type":"str","duration":"5","extend":true,"overrideDelay":true,"units":"s","reset":"","bytopic":"all","topic":"topic","outputs":1,"x":2080,"y":580,"wires":[["8de7dadf8b83a874"]]},{"id":"8de7dadf8b83a874","type":"api-current-state","z":"60f2d2277843c698","name":"","server":"","version":3,"outputs":2,"halt_if":"off","halt_if_type":"str","halt_if_compare":"is","entity_id":"switch.kitchen_counter","state_type":"str","blockInputOverrides":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"msg.payload","valueType":"jsonata"},{"property":"data","propertyType":"msg","value":"","valueType":"entity"}],"for":"0","forType":"num","forUnits":"minutes","override_topic":false,"state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","x":2330,"y":580,"wires":[["132371cb6c6eb611"],[]]}]

The trigger handles the on and off messages, you only need the motion to trigger when on. Set that timer for the length of time that you want the light to stay on after the last motion event.

According to your scheme, only the light turns on and nothing else happens, it doesn’t even turn it off, and if the light is already on, then nothing happens either. (((

That means you have “dumb” light bulbs, and the motion sensor triggers the relay in the light switch, right? Does your light switch integration expose different entities for the relay and the button?

If you turn off the switch physically it does noting? There is something missing because no matter what the automation does it wouldn’t effect the switch’s physical operation.

To add your diagram shows that the switch is in the on position, it won’t shut off unless you switch the switch to off.
Screenshot 2024-02-01 130015

That’s right, ordinary light bulbs are turned on by a smart relay, and there is also a separate smart motion sensor, when triggered, a command is sent to the smart relay to turn on the light.
What I was worried about was that the end result was a loop; apparently this cannot be achieved using these two devices?
And alas, apart from diagnostic and configuration functions, these devices can’t do anything else (((
Apparently something else needs to be done, either the light bulbs are also smart or, for example, a wireless switch, right?

image

Apparently, as I already answered above, these two devices are not enough for this task (((

Ok you can create a switch helper(input_boolean) in HA to simulate a physical switch. Use that in this flow where indicated (2 nodes).

[{"id":"ec0c2230626a4e60","type":"trigger-state","z":"60f2d2277843c698","name":"","server":"","version":4,"inputs":0,"outputs":2,"exposeAsEntityConfig":"","entityId":["binary_sensor.g3_instant_motion_2"],"entityIdType":"list","debugEnabled":false,"constraints":[{"targetType":"this_entity","targetValue":"","propertyType":"current_state","propertyValue":"new_state.state","comparatorType":"is","comparatorValueDatatype":"str","comparatorValue":"on"}],"customOutputs":[],"outputInitially":false,"stateType":"str","enableInput":false,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"x":1780,"y":580,"wires":[["2b1e14d658514562"],[]]},{"id":"132371cb6c6eb611","type":"api-call-service","z":"60f2d2277843c698","name":"","server":"","version":5,"debugenabled":false,"domain":"switch","service":"turn_{{payload}}","areaId":[],"deviceId":[],"entityId":["relay switch here"],"data":"","dataType":"jsonata","mergeContext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":2650,"y":520,"wires":[[]]},{"id":"2b1e14d658514562","type":"trigger","z":"60f2d2277843c698","name":"","op1":"on","op2":"off","op1type":"str","op2type":"str","duration":"5","extend":true,"overrideDelay":true,"units":"s","reset":"","bytopic":"all","topic":"topic","outputs":1,"x":2080,"y":580,"wires":[["8de7dadf8b83a874"]]},{"id":"5909964afbf2b3ed","type":"server-state-changed","z":"60f2d2277843c698","name":"","server":"","version":5,"outputs":1,"exposeAsEntityConfig":"","entityId":"input boolean here","entityIdType":"exact","outputInitially":false,"stateType":"str","ifState":"","ifStateType":"str","ifStateOperator":"is","outputOnlyOnStateChange":true,"for":"0","forType":"num","forUnits":"minutes","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":1730,"y":520,"wires":[["132371cb6c6eb611"]]},{"id":"8de7dadf8b83a874","type":"api-current-state","z":"60f2d2277843c698","name":"","server":"","version":3,"outputs":2,"halt_if":"off","halt_if_type":"str","halt_if_compare":"is","entity_id":"same input boolean here","state_type":"str","blockInputOverrides":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"msg.payload","valueType":"jsonata"},{"property":"data","propertyType":"msg","value":"","valueType":"entity"}],"for":"0","forType":"num","forUnits":"minutes","override_topic":false,"state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","x":2330,"y":580,"wires":[["132371cb6c6eb611"],[]]}]

I do exactly the same thing. To detect if someone used the wall switch, just use the event states node.

garage light status

[{"id":"be04c9b494079cf6","type":"server-state-changed","z":"427325c8e7ca8ea3","g":"5f19663345b0ba91","name":"Garage Light Status changed","server":"cc0d1734.6963a8","version":5,"outputs":2,"exposeAsEntityConfig":"","entityId":"switch.garage","entityIdType":"exact","outputInitially":false,"stateType":"str","ifState":"off","ifStateType":"str","ifStateOperator":"is_not","outputOnlyOnStateChange":true,"for":"0","forType":"num","forUnits":"minutes","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":180,"y":2040,"wires":[["85d3b89c15776571"],["85d3b89c15776571"]]},{"id":"9ca61ac724d67a16","type":"switch","z":"427325c8e7ca8ea3","g":"5f19663345b0ba91","name":"on or off?","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"on","vt":"str"},{"t":"eq","v":"off","vt":"str"}],"checkall":"true","repair":false,"outputs":2,"x":580,"y":2040,"wires":[["444841af2bb4c777"],[]]},{"id":"5c4c0fee50e277bf","type":"comment","z":"427325c8e7ca8ea3","g":"5f19663345b0ba91","name":"Checks for manual operation of the light","info":"","x":250,"y":2000,"wires":[]},{"id":"85d3b89c15776571","type":"ui_switch","z":"427325c8e7ca8ea3","g":"5f19663345b0ba91","name":"Garage Light","label":"Garage","tooltip":"","group":"c4a8276b.d60508","order":8,"width":0,"height":0,"passthru":false,"decouple":"true","topic":"","topicType":"str","style":"","onvalue":"on","onvalueType":"str","onicon":"fa-2x fa-lightbulb-o","oncolor":"#e8f146","offvalue":"off","offvalueType":"str","officon":"fa-2x fa-lightbulb-o","offcolor":"gray","animate":true,"className":"","x":410,"y":2040,"wires":[["9ca61ac724d67a16"]]},{"id":"cc0d1734.6963a8","type":"server","name":"Home Assistant- Raspberry","version":5,"addon":true,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true,"cacheJson":true,"heartbeat":false,"heartbeatInterval":30,"areaSelector":"friendlyName","deviceSelector":"friendlyName","entitySelector":"friendlyName","statusSeparator":"at: ","statusYear":"hidden","statusMonth":"short","statusDay":"numeric","statusHourCycle":"h23","statusTimeFormat":"h:m","enableGlobalContextStore":true},{"id":"c4a8276b.d60508","type":"ui_group","name":"Downstairs Lights","tab":"9ca4e0d8.f15fb","order":1,"disp":true,"width":4,"collapse":false},{"id":"9ca4e0d8.f15fb","type":"ui_tab","name":"Lights","icon":"dashboard","order":3}]

I don’t quite understand what this will give me? I can take the status from the light node itself

The input boolean represents the switch you asked for here.

Yes, your option gave results, now I can virtually turn on the light and the traffic signal does not turn it off, thank you very much for the tip

Thank you all so much for your help, I learned a lot of new things, which is all the more interesting, I will continue to study Node-Red