This is a useful pattern I use in a few rooms around the house for turning lights on and off.
I have motion sensors in a few places. When I first got them, I naively just hooked them up so that when they detect motion in a particular room, the light turns on. Then when the sensor deactivates, the light turns off again.
Unsurprisingly this wasn't great and led to a number of farcical situations where we were desperately waving around trying to get lights to turn back on. I could tell by my wife's raised eyebrows that this wasn't really in keeping with my goal of avoiding automation for automation's sake.
Looking around, I saw a couple of mentions of using multiple motion sensors to deal with this problem.
The key thing here is differentiating between the motion detection and room occupancy. We want the lights to activate and deactive based on whether the room is occupied which may or may not be related to whether motion is detected.
Adding occupancy state to Home Assistant
So starting with my kitchen, the first thing I did was to add a new sensor to Home Assistant that would indicate whether the kitchen was occupied. I also added a new MQTT topic for this sensor to use. Based on my MQTT topic naming strategy I decided to call this
home/s/kitchen/occupied which would have a boolean value of either true or false. Here's what the sensor config looks like in Home Assistant
# configuration.yaml binary_sensor: - platform: mqtt device_class: occupancy name: Kitchen occupied state_topic: home/s/kitchen/occupied payload_on: 'true' payload_off: 'false'
You can then use the MQTT test client in Home Assistant to verify that this turns on and off correctly when messages are sent to the MQTT topic. Note how the home icon changes based on whether true or false is posted to that topic.
Interpreting the motion sensor states
Now in my house, my hall connects both my living room and kitchen. There is only one way out of both rooms, via the hall. Therefore if someone leaves the kitchen, regardless of where they are going they must pass through the hall. So it is reasonable to assume that once someone is in the kitchen, if we don't detect motion in the hall, they must still be there.
Since I use the same motion sensors everywhere (SmartThings), the delay between the last motion being detected and sending the "inactive" message is the same for both sensors. So if someone leaves the kitchen and passes through the hall, when the kitchen motion sensor sends its inactive state update, the hall sensor should still be in the active state. Therefore we can check the state of the hall sensor to verify this. If so we assume someone left the kitchen. If not, we assume the person is still in the kitchen.
Obviously whenever we detect any motion in the kitchen, that means someone is in there regardless of the hall state. This is particularly relevant if there is more than one person in the house since one person could leave the room and the other stay in the kitchen.
That sounds more complicated than it actually is so here is a table summarising the different possibilities.
|Kitchen motion sensor||Hall motion sensor||Kitchen occupied?||Reasoning|
|Activates||Any state||Yes||Someone moved in the kitchen|
|Deactivates||Inactive||Yes||The hall sensor wasn't triggered therefore the person is still in the kitchen but not moving|
|Deactivates||Active||No||The person left the kitchen and walked via the hall to another room|
So with all that figured out, the final step is to set up the Node-RED flow to implement this logic. Here's what that looks like
There is an additional simple connection from the MQTT occupancy topic to the topic to turn the kitchen lights on. Since these are both boolean topics all that is required is to pass the message from the
home/s/kitchen/occupied topic to the
So far this has worked a treat and makes all the difference. Now our days of waving things in the air to keep the lights on are over.
I also implemented the same flow in our living room however in this case I was also able to factor in whether or not the TV is on. This means that so long as the TV is on, the lights won't turn off automatically.
If you found this interesting you might also like this twist on a contact sensor automation.