Home Assistant And Node-RED Lighting Automation With Presence Detection
In the video below, we show you how to configure Node-Red in Home Assistant to automate your lighting and make use of presence detection
We want lights to be turned on when it’s dark so first we set up a rule that allows us to know if it’s night time
For rooms where people don’t stay long i.e. transit areas, we’ll use a motion sensor to detect movement and turn the lights on, then use a timer to turn the lights off when movement is no longer detected
Because motion sensors are not good at detecting presence, we use ESPresence to track which rooms people are in. This then allows us to create room occupancy sensors to tell us which rooms are occupied
For rooms where people will spend a long period of time, we then adapt our lighting rules to only turn lights off if a room is empty
Because there are times during the year when it’s dark in the morning, we create an additional rule to turn lights off after sunrise
Steps Taken:
-
Requirements
ESPresence compatible hardware
Base Stations
Cables
Apple Watch
Xiaomi Mi Band 5
Outdoor Lux Sensor
Philips Hue Dusk-to-Dawn
NOTE: I am part of the Amazon affiliate program and may earn commission through Amazon website links if used. However, there is no additional cost to the buyer -
Assumptions
Home Assistant is already setup and you’ve added motions sensors and smart lights or switches for the rooms
You already have Node Red and ESPresence installed and configured
If not then I do have several videos that can help which you can find here
Home Assistant Playlist -
Night Time Rule
We only want lights turning on if it’s dark so we create a toggle switch that we’ll call Night_Time
To track night time we use a big timer node to tell us when it’s sunrise and sunset
TIP: You can get the latitude and longitude co-ordinates for this from Google Maps
With on set to sunrise and off set to sunset we can then turn on our Night_Time toggle switch at sunset and off at sunrise -
Lights On A Timer
For transit areas like hallways we set up a rule to turn the lights on if motion is detected
As useful as a LUX sensor is for checking light levels in a room, the problem with the ones I have is they are too slow to react to light level changes
In other words, if the light is turned off just as you enter the room, the light won’t come back on until the LUX sensor updates and the motion sensor clears
For that reason, the rule checks to see if it’s night time to decide if it’s dark before turning the lights on
If you do have a LUX sensor in the room though, you can still take advantage of it to have the rule turn the light on if it’s dark during the day
That slow reaction time isn’t as bad then and this is useful on days when it’s dull and overcast
When motion is no longer detected in the room, the rule starts a timer to turn off the lights, but this will be stopped if someone enters the room while the timer is running
That’s made possible by using the stoptimer node
Because it’s no longer included by default you will need to install this -
Room Occupancy Rules
A presence detection system like ESPresence tells us which rooms people are in, but for our lighting rules we want to know if a room is empty
So we create toggle switches that will be used to tell us which room a person is in
We than create a rule for each person so that when their Bluetooth device moves to another room, the relevent room switch for that person will be turned on and their other room switches turned off
Although these switches only tell us the same thing that their tracker does, i.e. which room they are in, we can combine the toggle switches of multiple people into a room occupancy sensor
To create the room sensors, you can either put the details into the original configuration.yaml file, or as I prefer, create a separate file and configure the configuration.yaml file to include it. In this case we need to tell it to include a template file
Each room sensor is defined by the state of the toggle switches for different people for that room
By using the OR operator it means that if one person is in the room, the room sensor will be on, which lets us know it’s occupied
Likewise, if nobody is in the room, the room sensor will be off, which lets us know it’s empty
To make use of these, all we need do is to re-use the rule we used for transit areas but add an extra check after the timer expires to check if the room sensor is off
This way, the lights remain on if ESPresence detects someone in that room
But they will still be turned off once everybody has left the room -
Additional Room Occupancy Rules
The original lighting rules I set up were split into two parts
A motion sensor rule would turn the lights on and a presence detection rule would turn them off
The motion sensor rule was the same as the transit area rule mentioned earlier, just without the timer to turn the lights off
The presence rule would check if a room had become empty and then start a timer to turn the lights off but only after a final check to see if it was still empty
If motion was then detected in the room, as the timer was counting down, the timer would be stopped
Now if someone comes back into the room and the motion sensor hasn’t cleared, we need another means to stop the timer
In addition, devices can sometimes jump to another room and then back
If you ever watch the distance for a device in Home Assistant, you’ll see it gets weaker and the distance to the base station increases
If there isn’t an update in time, the device might then be detected as being closer to another base station. As a result the device jumps to another room
But shortly afterwards it will return to the original room after an updated signal is received
Although you can try and fine tune the distance filter on these base stations, some devices are reported as being further away than others so this may not help when you have to accomodate different distances for different devices
In which case, the rule also checks when the room changes to occupied
When that happens the timer will be stopped, but it requires a changenode adding in to send that stop message
Although these two rules worked fine to begin with, there is a problem if someone leaves a room, triggers the lights to come on in that room, but then returns to the previous room too quickly
If presence detection hasn’t noticed a room change, the lights will stay on
So that’s why the motion sensor rule in part 5 includes a timer to turn the lights off
However, the presence rule to turn the lights off is still being used as a fallback rule
It is possible for the motion sensor rule to be told the room is occupied when it’s not, for example, a device just happens to falsely move to that room just as the timer expires
So by giving the presence rule a higher time to expire it provides another way to turn the lights off, just in case they had been left on in error -
Sunrise Light Rule
Lighting automation rules like these are best suited to night time
But there are times of the year that you get up and it will be dark outside
Then you have a situation where the lights were turned on because it was dark, then the sun comes up and it’s light outside, but the lights in the house are still on
In which case I have an outdoor LUX sensor to monitor the light levels outside
A rule then monitors that and once it gets bright enough outside the lights are turned off
Sharing is caring!