Room Occupancy Using Motion and Contact Sensors

Really struggling with what I feel should be a simple flow but for some reason no matter what I try I cannot seem to get the flow to run the way I want.

Attempting to build a presence detection for rooms based on Motion and Door Contact Sensors - essentially just triggering an input boolean.

Criteria 1: When the door opens the the input boolean should be triggered to ON

Criteria 2: When motion is detected the input boolean should be triggered to ON

Criteria 2a: If the door is in the closed (off) position the input boolean should be left ON until the door has been opened (on) and no motion has been detected for at least 30 seconds

Criteria 2b: If the door is in the open (on) position the input boolean should be left ON until there has been no motion detected for 30 seconds.

Essentially what I am wanting is it to turn on the input boolean with the door opening or motion being detected and only want to turn it off if the door is opened and there is no motion detected for atleast 30 seconds. I am trying to avoid the scenario where one person is in the room someone else comes in and it turns the input boolean off because the door was triggered again.

I've been messing with this for far too long and hoping someone has some ideas.

1 Like

Welcome to the forum!
I use one of these in the kitchen, to automatically turn on the strip lights as long as someone is in the room. Expensive for more than one room, but it is 100% reliable.

Not really looking to add more hardware to the mix when i already have equipment that should work. Just not sure on the proper flow to create.

Door sensors will not really work, imagine someone just looks into a room to say hello.
I have tried radar and PIR sensors, they are too unreliable and if someone stands still for a while, reading a book, for example, or even doing small tasks in the kitchen, they will report no movement. I spent a LOT of time on this.

Right BUT: Door sensors combined with Motion sensor will work i have no doubt. And if you really think about it if motion has been detected at all in a room that the door is closed then occupancy cannot change until the door is open and no motion is detected.... unless they fly out windows that is.

The motion detection is the problem!

And people will leave the room and leave the door open.

Not in this use case they wont. But OK....

Not sure i have the door sensor in the correct orientation, but this should do what you require.

[{"id":"a0fd5d10.aede9","type":"inject","z":"bf9e1e33.030598","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"door","payload":"0","payloadType":"num","x":130,"y":1760,"wires":[["96dbe28f.e57638"]]},{"id":"96dbe28f.e57638","type":"join","z":"bf9e1e33.030598","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":310,"y":1680,"wires":[["783f1971.fb173","a0b15049.95e66"]]},{"id":"284d40a3.52899","type":"inject","z":"bf9e1e33.030598","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"motion","payload":"1","payloadType":"num","x":150,"y":1600,"wires":[["96dbe28f.e57638"]]},{"id":"618bc702.155b58","type":"inject","z":"bf9e1e33.030598","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"motion","payload":"0","payloadType":"num","x":140,"y":1640,"wires":[["96dbe28f.e57638"]]},{"id":"1a77085e.0cf158","type":"inject","z":"bf9e1e33.030598","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"door","payload":"1","payloadType":"num","x":130,"y":1720,"wires":[["96dbe28f.e57638"]]},{"id":"783f1971.fb173","type":"debug","z":"bf9e1e33.030598","name":"","active":false,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"payload","statusType":"auto","x":380,"y":1800,"wires":[]},{"id":"a0b15049.95e66","type":"switch","z":"bf9e1e33.030598","name":"","property":"$$.payload.motion & $$.payload.door","propertyType":"jsonata","rules":[{"t":"eq","v":"00","vt":"str"},{"t":"eq","v":"01","vt":"str"},{"t":"else"}],"checkall":"false","repair":false,"outputs":3,"x":470,"y":1680,"wires":[["d234b0e7.181938"],["4591017.a999c","f9046698.b809f8"],["f9046698.b809f8"]]},{"id":"d234b0e7.181938","type":"change","z":"bf9e1e33.030598","name":"set off","rules":[{"t":"set","p":"payload","pt":"msg","to":"0","tot":"num"}],"action":"","property":"","from":"","to":"","reg":false,"x":700,"y":1620,"wires":[["8927a77b.9ddd4"]]},{"id":"f9046698.b809f8","type":"change","z":"bf9e1e33.030598","name":"set on","rules":[{"t":"set","p":"payload","pt":"msg","to":"1","tot":"num"},{"t":"set","p":"reset","pt":"msg","to":"true","tot":"bool"}],"action":"","property":"","from":"","to":"","reg":false,"x":680,"y":1740,"wires":[["8927a77b.9ddd4","5745b932.2bcda"]]},{"id":"4591017.a999c","type":"delay","z":"bf9e1e33.030598","name":"","pauseType":"delay","timeout":"50","timeoutUnits":"milliseconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":680,"y":1680,"wires":[["d234b0e7.181938"]]},{"id":"8927a77b.9ddd4","type":"trigger","z":"bf9e1e33.030598","name":"","op1":"","op2":"","op1type":"nul","op2type":"payl","duration":"3","extend":false,"overrideDelay":false,"units":"s","reset":"","bytopic":"all","topic":"topic","outputs":1,"x":930,"y":1680,"wires":[["5745b932.2bcda"]]},{"id":"5745b932.2bcda","type":"change","z":"bf9e1e33.030598","name":"","rules":[{"t":"delete","p":"reset","pt":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":1140,"y":1680,"wires":[["a7a50df.5fbf0f"]]},{"id":"a7a50df.5fbf0f","type":"rbe","z":"bf9e1e33.030598","name":"","func":"rbe","gap":"","start":"","inout":"out","septopics":false,"property":"payload","x":1310,"y":1680,"wires":[["4007718e.44361"]]},{"id":"4007718e.44361","type":"debug","z":"bf9e1e33.030598","name":"on","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"payload","statusType":"auto","x":580,"y":1800,"wires":[]}]

[edit]
set to 3 seconds for testing, change to what ever delay you require in the trigger node.

Not sure i really understand that flow even in the slightest bit.
Assuming Motion 1 and Motion 0 are Motion On and Off? Also assuming same for the Door.

But how to turn this into something I can actually work with that might be a different story.

Working with someone on a reddit post I was able to come up with this which seems to do the trick perfectly.

[{"id":"c3f07a50346d189c","type":"group","z":"7b906bdd2be7a09c","name":"Bonus Room Occupancy","style":{"fill":"#d1d1d1","label":true,"color":"#ff0000","stroke":"#ff0000"},"nodes":["661e8487f5a6103f","142cd3cea156f49e","01982287983a03b1","b9c2cef79c0f5ae8","80a10a95d04c6fc0","214e2b4be4b78e42","b2f5ea734730d2c7","95c1f6dd5101f8a9","f36d5ca986cc9871","117e245e2c7cafd3","5bc26c2dd88d4831","b16edd49b625d05d","2d82ca7f758cb840","194f6264621b6f04","5d9dc49dab2bea14","4cf6a16c9d14fc8a","f70964ee06900d78","0ca807f1d2c4eed3","42e0a10b16017cc5"],"x":34,"y":79,"w":1372,"h":362},{"id":"661e8487f5a6103f","type":"trigger-state","z":"7b906bdd2be7a09c","g":"c3f07a50346d189c","name":"Door Opens","server":"c8d073b3.907a4","version":2,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityid":"binary_sensor.bonus_room_door_sensor_contact","entityidfiltertype":"exact","debugenabled":false,"constraints":[{"targetType":"this_entity","targetValue":"","propertyType":"current_state","propertyValue":"new_state.state","comparatorType":"is","comparatorValueDatatype":"str","comparatorValue":"on"},{"targetType":"entity_id","targetValue":"input_boolean.bonus_room_occupancy","propertyType":"current_state","propertyValue":"new_state.state","comparatorType":"is","comparatorValueDatatype":"str","comparatorValue":"off"}],"inputs":0,"outputs":2,"customoutputs":[],"outputinitially":false,"state_type":"str","enableInput":false,"x":150,"y":120,"wires":[["01982287983a03b1"],[]]},{"id":"142cd3cea156f49e","type":"trigger-state","z":"7b906bdd2be7a09c","g":"c3f07a50346d189c","name":"Motion Detection","server":"c8d073b3.907a4","version":2,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityid":"binary_sensor.bonus_room_motion_sensor_occupancy","entityidfiltertype":"exact","debugenabled":false,"constraints":[{"targetType":"this_entity","targetValue":"","propertyType":"current_state","propertyValue":"new_state.state","comparatorType":"is","comparatorValueDatatype":"str","comparatorValue":"on"},{"targetType":"entity_id","targetValue":"input_boolean.bonus_room_occupancy","propertyType":"current_state","propertyValue":"new_state.state","comparatorType":"is","comparatorValueDatatype":"str","comparatorValue":"off"}],"inputs":0,"outputs":2,"customoutputs":[],"outputinitially":false,"state_type":"str","enableInput":false,"x":160,"y":180,"wires":[["01982287983a03b1"],[]]},{"id":"01982287983a03b1","type":"api-call-service","z":"7b906bdd2be7a09c","g":"c3f07a50346d189c","name":"Bonus Room Occupancy - On","server":"c8d073b3.907a4","version":5,"debugenabled":false,"domain":"input_boolean","service":"turn_on","areaId":[],"deviceId":[],"entityId":["input_boolean.bonus_room_occupancy"],"data":"","dataType":"jsonata","mergeContext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":430,"y":140,"wires":[[]]},{"id":"b9c2cef79c0f5ae8","type":"trigger-state","z":"7b906bdd2be7a09c","g":"c3f07a50346d189c","name":"Door Sensor","server":"c8d073b3.907a4","version":2,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityid":"binary_sensor.bonus_room_door_sensor_contact","entityidfiltertype":"exact","debugenabled":false,"constraints":[{"targetType":"this_entity","targetValue":"","propertyType":"current_state","propertyValue":"new_state.state","comparatorType":"is","comparatorValueDatatype":"str","comparatorValue":"on"},{"targetType":"entity_id","targetValue":"input_boolean.bonus_room_occupancy","propertyType":"current_state","propertyValue":"new_state.state","comparatorType":"is","comparatorValueDatatype":"str","comparatorValue":"on"}],"inputs":0,"outputs":2,"customoutputs":[],"outputinitially":false,"state_type":"str","enableInput":false,"x":150,"y":240,"wires":[["80a10a95d04c6fc0"],[]]},{"id":"80a10a95d04c6fc0","type":"delay","z":"7b906bdd2be7a09c","g":"c3f07a50346d189c","name":"","pauseType":"delay","timeout":"30","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"allowrate":false,"outputs":1,"x":320,"y":240,"wires":[["214e2b4be4b78e42"]]},{"id":"214e2b4be4b78e42","type":"api-current-state","z":"7b906bdd2be7a09c","g":"c3f07a50346d189c","name":"Motion Detector Status","server":"c8d073b3.907a4","version":3,"outputs":1,"halt_if":"","halt_if_type":"str","halt_if_compare":"is","entity_id":"binary_sensor.bonus_room_motion_sensor_occupancy","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":530,"y":240,"wires":[["b2f5ea734730d2c7"]]},{"id":"b2f5ea734730d2c7","type":"switch","z":"7b906bdd2be7a09c","g":"c3f07a50346d189c","name":"Switch","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"on","vt":"str"},{"t":"eq","v":"off","vt":"str"}],"checkall":"true","repair":false,"outputs":2,"x":740,"y":240,"wires":[["0ca807f1d2c4eed3"],["95c1f6dd5101f8a9"]]},{"id":"95c1f6dd5101f8a9","type":"api-call-service","z":"7b906bdd2be7a09c","g":"c3f07a50346d189c","name":"Bonus Room Occupancy - Off","server":"c8d073b3.907a4","version":5,"debugenabled":false,"domain":"input_boolean","service":"turn_off","areaId":[],"deviceId":[],"entityId":["input_boolean.bonus_room_occupancy"],"data":"","dataType":"jsonata","mergeContext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":990,"y":240,"wires":[[]]},{"id":"f36d5ca986cc9871","type":"trigger-state","z":"7b906bdd2be7a09c","g":"c3f07a50346d189c","name":"Motion Detection Door Open","server":"c8d073b3.907a4","version":2,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityid":"binary_sensor.bonus_room_motion_sensor_occupancy","entityidfiltertype":"exact","debugenabled":false,"constraints":[{"targetType":"this_entity","targetValue":"","propertyType":"current_state","propertyValue":"new_state.state","comparatorType":"is","comparatorValueDatatype":"str","comparatorValue":"on"},{"targetType":"entity_id","targetValue":"binary_sensor.bonus_room_door_sensor_contact","propertyType":"current_state","propertyValue":"new_state.state","comparatorType":"is","comparatorValueDatatype":"str","comparatorValue":"on"}],"inputs":0,"outputs":2,"customoutputs":[],"outputinitially":false,"state_type":"str","enableInput":false,"x":180,"y":340,"wires":[["5bc26c2dd88d4831"],[]]},{"id":"117e245e2c7cafd3","type":"api-current-state","z":"7b906bdd2be7a09c","g":"c3f07a50346d189c","name":"Motion Off For 30 Seconds","server":"c8d073b3.907a4","version":3,"outputs":2,"halt_if":"off","halt_if_type":"str","halt_if_compare":"is","entity_id":"binary_sensor.bonus_room_motion_sensor_occupancy","state_type":"str","blockInputOverrides":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"entityState"},{"property":"data","propertyType":"msg","value":"","valueType":"entity"}],"for":"30","forType":"num","forUnits":"seconds","override_topic":false,"state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","x":680,"y":340,"wires":[["2d82ca7f758cb840"],["194f6264621b6f04"]]},{"id":"5bc26c2dd88d4831","type":"delay","z":"7b906bdd2be7a09c","g":"c3f07a50346d189c","name":"","pauseType":"delay","timeout":"5","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"allowrate":false,"outputs":1,"x":440,"y":340,"wires":[["117e245e2c7cafd3"]]},{"id":"b16edd49b625d05d","type":"api-call-service","z":"7b906bdd2be7a09c","g":"c3f07a50346d189c","name":"Bonus Room Occupancy - Off","server":"c8d073b3.907a4","version":5,"debugenabled":false,"domain":"input_boolean","service":"turn_off","areaId":[],"deviceId":[],"entityId":["input_boolean.bonus_room_occupancy"],"data":"","dataType":"jsonata","mergeContext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":1250,"y":340,"wires":[[]]},{"id":"2d82ca7f758cb840","type":"api-current-state","z":"7b906bdd2be7a09c","g":"c3f07a50346d189c","name":"Bonus Room Door Sensor","server":"c8d073b3.907a4","version":3,"outputs":2,"halt_if":"on","halt_if_type":"str","halt_if_compare":"is","entity_id":"binary_sensor.bonus_room_door_sensor_contact","state_type":"str","blockInputOverrides":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"entityState"},{"property":"data","propertyType":"msg","value":"","valueType":"entity"}],"for":"30","forType":"num","forUnits":"seconds","override_topic":false,"state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","x":960,"y":340,"wires":[["b16edd49b625d05d"],["4cf6a16c9d14fc8a"]]},{"id":"194f6264621b6f04","type":"link out","z":"7b906bdd2be7a09c","g":"c3f07a50346d189c","name":"","mode":"link","links":["5d9dc49dab2bea14"],"x":795,"y":400,"wires":[]},{"id":"5d9dc49dab2bea14","type":"link in","z":"7b906bdd2be7a09c","g":"c3f07a50346d189c","name":"","links":["194f6264621b6f04"],"x":365,"y":400,"wires":[["5bc26c2dd88d4831"]]},{"id":"4cf6a16c9d14fc8a","type":"link out","z":"7b906bdd2be7a09c","g":"c3f07a50346d189c","name":"","mode":"link","links":["f70964ee06900d78"],"x":1075,"y":400,"wires":[]},{"id":"f70964ee06900d78","type":"link in","z":"7b906bdd2be7a09c","g":"c3f07a50346d189c","name":"","links":["4cf6a16c9d14fc8a"],"x":285,"y":400,"wires":[["5bc26c2dd88d4831"]]},{"id":"0ca807f1d2c4eed3","type":"link out","z":"7b906bdd2be7a09c","g":"c3f07a50346d189c","name":"","mode":"link","links":["42e0a10b16017cc5"],"x":805,"y":180,"wires":[]},{"id":"42e0a10b16017cc5","type":"link in","z":"7b906bdd2be7a09c","g":"c3f07a50346d189c","name":"","links":["0ca807f1d2c4eed3"],"x":215,"y":280,"wires":[["80a10a95d04c6fc0"]]},{"id":"c8d073b3.907a4","type":"server","name":"Home Assistant","addon":true}]
1 Like

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