Living-area lights that follow people, and dim as the night goes on
Radar presence sensors, a dusk-to-bedtime dimming curve and per-room holds, so the lights come on after dark and leave your own changes alone.
- Difficulty
- Moderate
- Parts cost
- Free software, plus a presence sensor per room
- Build time
- An evening for the basics, then tweaking as you live with it
- Skills
- Home Assistant, YAML, Jinja templates
The problem
I wanted the lounge, dining and kitchen lights to come on by themselves when someone’s in there after dark, get warmer and dimmer as the night goes on, and go off when everyone’s left.
Getting them to come on was the easy bit. Getting them to stop fighting the people living with them took the next few months. Most of this guide is about that part: the lights have to leave your own changes alone, and every safety net has to survive Home Assistant restarting.
How it works
Presence, not motion. Each room has an mmWave radar, a presence sensor that sees people even when they sit still. Someone watching a film without moving still counts as there. The three rooms feed one group sensor, “living area occupied”, which is on if any room sees someone.
The curve. When someone’s there after dark, each room is set to a brightness that depends on the time: 100% at 4000 K (cool white) until 8pm, then sliding down in a straight line to 30% at 2200 K (warm, candle-ish) by 10:30pm, and staying there overnight until the sun’s up. At 9:04pm that works out to 70%. The automation runs when someone arrives, when the sun drops below 12°, and every 10 minutes. The 10-minute runs (“ticks”) can only ever lower a room, and by no more than 15 points a time, so a room someone brightened eases down instead of snapping. If a room is already within 3 points of the target, the tick leaves it alone.
“After dark” is the sun’s elevation, not a clock, so it follows the seasons. My rule of thumb is that about 6° of elevation is about 30 minutes. 12° turns the lights on roughly an hour before sunset, which is what I asked for. If that feels too early, 6° or 9° is about 30 or 45 minutes.
Leaving people alone. Two latches stop the automation fighting you:
- A per-room hold. Brighten or dim one room by hand and that room is held: the curve skips it and keeps dimming the others. Holds work in both directions (a dim sticks too) and last until the area empties, or 45 minutes, whichever comes first.
- An override for the whole area. Turning a room on or off by hand while it’s dark and occupied switches the curve off everywhere until the area empties. You can also switch it on yourself. It resets itself at 4am every day.
How does Home Assistant know a person made the change? Every state change carries a context. If a
person did it from the app or a dashboard, context.user_id is filled in. When an automation did it,
it’s empty. So the curve can never latch a hold on itself.
Lights off. When nobody’s been seen for 3 minutes, the lights go off and every hold and the override clear, so the next evening starts fresh.
The welcome. A radar by the entry door turns the living area on as you walk in after dark: lounge, then 1.5 seconds later the dining room, then the kitchen. It uses the same curve, so a 2am arrival gets 30% warm white, not a blast. I first wanted it to light a different room near the door, then dropped that myself because it could wake the baby. Someone suggested only doing it when the living area is empty. I said no: it’s common for one of us to be in the lounge when the other comes in. It doesn’t matter anyway, because if someone’s in there the lights are already on and the welcome skips them.
What you need
- Home Assistant, with the lights and sensors below already in it.
- A presence sensor in each room. It needs to see still people, so an mmWave radar rather than a PIR motion sensor. Mine are a mix: 24 GHz ceiling radars on Zigbee that only report occupied or not, and some home-made units (an HLK-LD2412 radar and an ESP32-C3 running ESPHome).
- One more radar by the entry door, if you want the welcome.
- Dimmable lights with adjustable white in each room. Mine are Nanoleaf NL67 bulbs over Matter and Thread, two per room, grouped into one light per room.
Helpers and sensors
All the ids below are examples. Swap in your own, and keep them consistent across every block.
| What | Example id | Job |
|---|---|---|
| Radar, lounge | binary_sensor.example_lounge_presence |
Someone in the lounge |
| Radar, dining | binary_sensor.example_dining_presence |
Someone in the dining room |
| Radar, kitchen | binary_sensor.example_kitchen_presence |
Someone in the kitchen |
| Group | binary_sensor.example_living_occupied |
On if any of the three is on. The only presence sensor the curve looks at. |
| Radar, entry | binary_sensor.example_entry_presence |
Triggers the welcome. Not in the group. |
| Light groups | light.example_lounge, light.example_dining, light.example_kitchen |
One per room. Use the group, not the single bulbs. |
| Master switch | input_boolean.example_auto_lighting |
Turns the whole thing on or off. |
| Override | input_boolean.example_living_override |
Whole area: curve stops until the area empties. |
| Holds | input_boolean.example_hold_lounge, input_boolean.example_hold_dining, input_boolean.example_hold_kitchen |
One per room. |
| Welcome switch | input_boolean.example_welcome_enabled |
Turns the welcome on or off. |
| Curve | sensor.example_light_curve |
The brightness right now, with the colour temperature as an attribute. |
Keep the entry radar out of the group. It’s in a different room, so it would hold the living lights on and confuse the lights-off automation.
My master switch lives on the dashboard, in the living area section with the light controls.
The YAML
This YAML is rebuilt from my notes, not pasted from my live system, so treat it as a starting point and check it against your own setup.
Helpers, group and curve (configuration.yaml)
The curve lives in one template sensor so the curve automation and the welcome always agree.
input_boolean:
example_auto_lighting:
name: Living auto lighting
icon: mdi:lightbulb-auto
example_living_override:
name: Living auto-dim override
example_hold_lounge:
name: Hold lounge
example_hold_dining:
name: Hold dining
example_hold_kitchen:
name: Hold kitchen
example_welcome_enabled:
name: Entry welcome lights
binary_sensor:
- platform: group
name: Example living occupied
unique_id: example_living_occupied
device_class: occupancy
entities:
- binary_sensor.example_lounge_presence
- binary_sensor.example_dining_presence
- binary_sensor.example_kitchen_presence
template:
- sensor:
- name: Example light curve
unique_id: example_light_curve
unit_of_measurement: "%"
# 100% until 20:00, straight line down to 30% at 22:30,
# then 30% overnight until the sun's up.
state: >
{% set m = now().hour * 60 + now().minute %}
{% set start = 20 * 60 %}
{% set end = 22 * 60 + 30 %}
{% set night = m >= end or (now().hour < 12 and is_state('sun.sun', 'below_horizon')) %}
{% if night %}30
{% elif m <= start %}100
{% else %}{{ (100 - 70 * (m - start) / (end - start)) | round(0) | int }}
{% endif %}
attributes:
kelvin: >
{% set m = now().hour * 60 + now().minute %}
{% set start = 20 * 60 %}
{% set end = 22 * 60 + 30 %}
{% set night = m >= end or (now().hour < 12 and is_state('sun.sun', 'below_horizon')) %}
{% if night %}2200
{% elif m <= start %}4000
{% else %}{{ (4000 - 1800 * (m - start) / (end - start)) | round(0) | int }}
{% endif %}
The curve sits on its 30% floor overnight and hands back to full brightness once the sun’s up. The
now().hour < 12 part only tells the small hours apart from the early evening, when the sun is also
down but the lights should still be at full.
The curve (automations.yaml)
Two things in here are there because they bit me (see the lessons):
- Each room gets a bare
light.turn_onfirst, a one-second wait, then a secondlight.turn_onwith brightness and colour. Sending brightness and colour in one command left my Matter bulbs dark. - Every light step has
continue_on_error: true, so one slow bulb can’t stop the other rooms.
- id: example_living_curve
alias: "Living: presence lighting (curve)"
mode: queued
triggers:
- trigger: state
entity_id: binary_sensor.example_living_occupied
to: "on"
id: occupied
- trigger: numeric_state
entity_id: sun.sun
attribute: elevation
below: 12
id: dusk
- trigger: time_pattern
minutes: "/10"
id: tick
- trigger: state
entity_id: input_boolean.example_living_override
to: "off"
id: override_clear
conditions:
- condition: state
entity_id: input_boolean.example_auto_lighting
state: "on"
- condition: state
entity_id: input_boolean.example_living_override
state: "off"
- condition: state
entity_id: binary_sensor.example_living_occupied
state: "on"
- condition: numeric_state
entity_id: sun.sun
attribute: elevation
below: 12
actions:
- variables:
pct: "{{ states('sensor.example_light_curve') | int(100) }}"
kelvin: "{{ state_attr('sensor.example_light_curve', 'kelvin') | int(4000) }}"
full: "{{ trigger.id != 'tick' }}"
- repeat:
for_each:
- light: light.example_lounge
hold: input_boolean.example_hold_lounge
- light: light.example_dining
hold: input_boolean.example_hold_dining
- light: light.example_kitchen
hold: input_boolean.example_hold_kitchen
sequence:
- if:
- condition: template
value_template: "{{ is_state(repeat.item.hold, 'off') }}"
then:
- choose:
# Arrival, dusk, override cleared, or the room is off: set it to the curve.
- conditions:
- condition: template
value_template: "{{ full or is_state(repeat.item.light, 'off') }}"
sequence:
- action: light.turn_on
target:
entity_id: "{{ repeat.item.light }}"
continue_on_error: true
- delay: "00:00:01"
- action: light.turn_on
target:
entity_id: "{{ repeat.item.light }}"
data:
brightness_pct: "{{ pct }}"
color_temp_kelvin: "{{ kelvin }}"
continue_on_error: true
# A 10-minute tick on a lit room: only ever lower it, 15 points at most.
- conditions:
- condition: template
value_template: >
{% set cur = (state_attr(repeat.item.light, 'brightness') | int(0) / 2.55) | round(0) %}
{{ cur > pct + 3 }}
sequence:
- action: light.turn_on
target:
entity_id: "{{ repeat.item.light }}"
data:
brightness_pct: >
{% set cur = (state_attr(repeat.item.light, 'brightness') | int(0) / 2.55) | round(0) | int %}
{{ [pct, cur - 15] | max }}
color_temp_kelvin: "{{ kelvin }}"
continue_on_error: true
A room someone dimmed below the curve is simply left alone: a tick never raises anything.
One catch I’ve left in on purpose: clearing the override sets every room straight to the curve. That’s right when the area has emptied and the lights are off. If you clear the override by hand while you’re sitting there, the lights will jump to the curve.
Holds and the override
- id: example_living_room_hold
alias: "Living: hold a room changed by hand"
mode: queued
triggers:
- trigger: state
entity_id:
- light.example_lounge
- light.example_dining
- light.example_kitchen
variables:
holds:
light.example_lounge: input_boolean.example_hold_lounge
light.example_dining: input_boolean.example_hold_dining
light.example_kitchen: input_boolean.example_hold_kitchen
conditions:
# A person did it, not an automation.
- condition: template
value_template: "{{ trigger.to_state.context.user_id is not none }}"
actions:
- action: input_boolean.turn_on
target:
entity_id: "{{ holds[trigger.entity_id] }}"
- id: example_living_override_latch
alias: "Living: manual on/off sets the override"
mode: queued
triggers:
- trigger: state
entity_id:
- light.example_lounge
- light.example_dining
- light.example_kitchen
conditions:
- condition: template
value_template: "{{ trigger.to_state.context.user_id is not none }}"
# On/off only. Brightness and colour tweaks are handled by the holds.
- condition: template
value_template: "{{ trigger.from_state.state != trigger.to_state.state }}"
- condition: state
entity_id: binary_sensor.example_living_occupied
state: "on"
- condition: numeric_state
entity_id: sun.sun
attribute: elevation
below: 12
actions:
- action: input_boolean.turn_on
target:
entity_id: input_boolean.example_living_override
Lights off when the area empties
The first trigger is the normal one. The other two are there because the lights once got turned back on a few seconds after going off, with nobody walking in, so the “nobody here for 3 minutes” trigger never fired again (see the lessons). They only act if the area really has been empty for 3 minutes and a light is on.
- id: example_living_lights_off
alias: "Living: lights off when empty"
mode: single
triggers:
- trigger: state
entity_id: binary_sensor.example_living_occupied
to: "off"
for: "00:03:00"
id: empty_3min
- trigger: state
entity_id:
- light.example_lounge
- light.example_dining
- light.example_kitchen
to: "on"
for: "00:03:00"
id: lights_on_while_empty
- trigger: time_pattern
minutes: "/5"
id: recheck
conditions:
- condition: state
entity_id: input_boolean.example_auto_lighting
state: "on"
- condition: template
value_template: >
{{ is_state('binary_sensor.example_living_occupied', 'off') and
(now() - states.binary_sensor.example_living_occupied.last_changed).total_seconds() >= 180 }}
- condition: or
conditions:
- condition: trigger
id: empty_3min
- condition: state
entity_id:
- light.example_lounge
- light.example_dining
- light.example_kitchen
match: any
state: "on"
actions:
- action: light.turn_off
target:
entity_id:
- light.example_lounge
- light.example_dining
- light.example_kitchen
continue_on_error: true
- action: input_boolean.turn_off
target:
entity_id:
- input_boolean.example_living_override
- input_boolean.example_hold_lounge
- input_boolean.example_hold_dining
- input_boolean.example_hold_kitchen
The safety nets that clear stuck latches
Holds and the override should clear when the area empties. If the radars flicker, that can fail for a
whole day. These two clear them anyway. Note the extra triggers on each: a for: timer on its own
doesn’t survive a restart (the lessons explain).
- id: example_living_expire_holds
alias: "Living: expire stale per-room holds (45 min)"
mode: single
triggers:
- trigger: state
entity_id:
- input_boolean.example_hold_lounge
- input_boolean.example_hold_dining
- input_boolean.example_hold_kitchen
to: "on"
for: "00:45:00"
id: normal
# After a restart a hold's age starts again from zero, so this one
# rarely clears anything. The half-hourly sweep below is what catches it.
- trigger: homeassistant
event: start
id: sweep
- trigger: time_pattern
minutes: "/30"
id: sweep
actions:
- repeat:
for_each:
- input_boolean.example_hold_lounge
- input_boolean.example_hold_dining
- input_boolean.example_hold_kitchen
sequence:
- if:
# Check the AGE of the hold, not just the timer.
- condition: template
value_template: >
{{ is_state(repeat.item, 'on') and
(now() - states[repeat.item].last_changed).total_seconds() >= 2700 }}
then:
- action: input_boolean.turn_off
target:
entity_id: "{{ repeat.item }}"
- id: example_living_override_daily_reset
alias: "Living: reset auto-dim override daily"
mode: single
triggers:
- trigger: time
at: "04:00:00"
id: "4am"
- trigger: state
entity_id: input_boolean.example_living_override
to: "on"
for: "06:00:00"
id: "6 hours on"
- trigger: homeassistant
event: start
id: "restart"
conditions:
- condition: state
entity_id: input_boolean.example_living_override
state: "on"
# After a restart, only if it's already been on 6 hours. Its clock starts
# again at the restart too, so in practice the 4am reset is the backstop.
- condition: or
conditions:
- condition: not
conditions:
- condition: trigger
id: "restart"
- condition: template
value_template: >
{{ (now() - states.input_boolean.example_living_override.last_changed).total_seconds() >= 21600 }}
actions:
- action: input_boolean.turn_off
target:
entity_id: input_boolean.example_living_override
- action: logbook.log
data:
name: Living lights
message: "auto-dim override reset by the {{ trigger.id }} trigger"
entity_id: input_boolean.example_living_override
The logbook line matters. Without it, an override that vanished overnight looks like a bug. The cost: an override set at 11pm is gone by 4am. That’s on purpose. I’d rather switch it back on by hand than have it stuck on.
The welcome
Keep this as a flat list of steps (see the lessons about parallel). The 3-minute delay at the end is
the cooldown: with mode: single, any new trigger during it is ignored, so one walk past the door is
one welcome.
- id: example_entry_welcome
alias: "Entry: welcome lights"
mode: single
max_exceeded: silent
triggers:
- trigger: state
entity_id: binary_sensor.example_entry_presence
from: "off"
to: "on"
conditions:
- condition: state
entity_id: input_boolean.example_welcome_enabled
state: "on"
- condition: numeric_state
entity_id: sun.sun
attribute: elevation
below: 10
actions:
- if:
- condition: state
entity_id: input_boolean.example_living_override
state: "off"
then:
- variables:
pct: "{{ states('sensor.example_light_curve') | int(100) }}"
kelvin: "{{ state_attr('sensor.example_light_curve', 'kelvin') | int(4000) }}"
# Lounge
- if:
- condition: state
entity_id: light.example_lounge
state: "off"
- condition: state
entity_id: input_boolean.example_hold_lounge
state: "off"
then:
- action: light.turn_on
target:
entity_id: light.example_lounge
continue_on_error: true
- delay: "00:00:01"
- action: light.turn_on
target:
entity_id: light.example_lounge
data:
brightness_pct: "{{ pct }}"
color_temp_kelvin: "{{ kelvin }}"
continue_on_error: true
- delay:
milliseconds: 1500
# Dining
- if:
- condition: state
entity_id: light.example_dining
state: "off"
- condition: state
entity_id: input_boolean.example_hold_dining
state: "off"
then:
- action: light.turn_on
target:
entity_id: light.example_dining
continue_on_error: true
- delay: "00:00:01"
- action: light.turn_on
target:
entity_id: light.example_dining
data:
brightness_pct: "{{ pct }}"
color_temp_kelvin: "{{ kelvin }}"
continue_on_error: true
- delay:
milliseconds: 1500
# Kitchen
- if:
- condition: state
entity_id: light.example_kitchen
state: "off"
- condition: state
entity_id: input_boolean.example_hold_kitchen
state: "off"
then:
- action: light.turn_on
target:
entity_id: light.example_kitchen
continue_on_error: true
- delay: "00:00:01"
- action: light.turn_on
target:
entity_id: light.example_kitchen
data:
brightness_pct: "{{ pct }}"
color_temp_kelvin: "{{ kelvin }}"
continue_on_error: true
- delay: "00:03:00"
My welcome uses 10° for “after dark”, a little later than the curve’s 12°. They both used 10° once. When I asked for the living lights to come on earlier, I moved the curve and left the welcome where it was.
Step by step
- Get a presence sensor working in each room first. Radars can see through walls, so if yours has a range setting, cap it to the room.
- Make the room light groups, if each room has more than one bulb.
- Add the helpers, the group sensor and the curve sensor to
configuration.yamland restart. - Check the curve sensor shows a sensible number (Developer tools, States).
- Add the curve and the lights-off automation. Live with just those two for an evening.
- Add the hold and override automations, then the two safety nets.
- Add the entry radar and the welcome last.
- Put the master switch, override and welcome switch on your dashboard, so anyone in the house can turn it off without opening the settings.
Testing it
- Check the curve maths. At 9:04pm the curve sensor should read 70%. You can paste the template into Developer tools, Template, to see it.
- Test a hold end to end. Here’s the test I ran: after 10:30pm, with all three rooms at 30%, brighten the lounge to 90% from the app. Its hold should turn on. Let a tick run: the lounge should stay at 90% while dining and kitchen stay at 30%. Leave the area: after 3 minutes all holds clear and the lights go off.
- Test a dim. Dim a room below the curve (I dimmed one to 50% when the curve wanted 89%). The next tick should leave it there.
- Testing by hand skips the conditions. The
automation.triggeraction skips the conditions by default. To test them too, callautomation.triggerwithskip_condition: false. Otherwise you’re testing nothing, and a perfectly good automation can look broken. - When the lights “won’t come on”, read the trace and the logbook before you change anything. The first time it happened to me, it was the override doing its job: someone had tapped “all lights off” on the dashboard, which counted as a manual change. The logbook shows which user made each change.
- A clean trace isn’t proof. A
light.turn_offsent to a bulb that’s dropped off the network does nothing, and the trace still looks perfect. Check the bulb itself.
Lessons learnt
Dimming the lounge switched the whole thing off. The first override latched on any manual change. Dim the room a little and the curve stopped for the rest of the night. Now the override only latches on a manual on or off, the ticks can only lower a room, and dims below the curve are just left alone.
Brightening one room got the room dimmed back. The curve used to drive one group of all six bulbs. Brighten the lounge and the group’s average went up, so the next tick dimmed everything back, including the lounge. That was a design mistake, not a glitch. The fix was per-room: one hold per room, latched by a person’s change, and the curve skips held rooms.
Bulbs stayed dark while saying they were on. My Nanoleaf Matter bulbs, given brightness and colour in the same command, would randomly stay physically dark while reporting on at full brightness. Taps from the app always worked, and that difference was the clue: those were plain “on” commands. It wasn’t the mesh, the Wi-Fi or the radar. Every automation now sends a bare “on” first, waits a second, then sets brightness and colour. The side effect is a one-second blip at the old brightness.
The welcome lit one room and quietly stopped. The first version had an if for each room inside a
parallel block. It ran the first room and stopped, with no error. A flat list of steps works. Also: if
running it by hand seems to do nothing, it’s probably still sitting in its 3-minute cooldown.
A hold got stuck, so I added a 45-minute timer. Then the timer got disarmed. A hold once stuck for 9
hours, so I added the “expire stale holds” automation with a for: 45 minutes trigger. A month later all
three holds sat on for about 22 hours anyway, and the curve was dead in every room. I found out when one
of the kids was in the dining room, the sensor said “detected”, and nothing came on. The cause: a for:
trigger needs the entity to change to on and then stay there. Home Assistant had been restarted that
night for an unrelated change, and a restart throws away a for: timer that’s running. The holds were
already on when it came back, so they never changed again and the timer never restarted. This catches
any for:-based safety net, not just lighting. Now the automation also runs at every start-up and every
30 minutes, and checks how long each hold has been on, rather than waiting to see it turn on. There’s a
catch I only understood later: after a restart a helper keeps its on or off state, but its “how long
has it been like this” clock starts again. So the start-up check can’t clear a stuck hold by its age,
because to Home Assistant it’s only just turned on. It’s the half-hourly sweep that clears it, a bit
later, once 45 minutes have passed since the restart. The override has the same blind spot, which is why
it also gets the daily 4am reset.
The welcome ignored the override. I set the dining room to 100%, switched the override on, and less than a minute later someone walked in the entry door. The welcome set all three rooms to the curve (70% at that time), ignoring both the override and the hold. The welcome now does nothing if the override is on, and skips any room that’s held or already on. Anything else that sets living-area brightness has to obey the same two rules.
One slow bulb left two rooms dark. The curve does lounge, then dining, then kitchen in one run. One
lounge bulb answered 32 seconds late, Home Assistant gave up with “Operation aborted”, and that error
stopped the whole run. Dining and kitchen stayed dark until the next tick. It was rare (one run in three
days), but continue_on_error: true on every light step fixed it.
The lights came back on 4 seconds after going off, and stayed on. Another automation turned the living lights back on just after the area emptied. Nobody walked in, so occupancy never changed, and the “nobody here for 3 minutes” trigger never fired again. Only watching for the change to empty wasn’t enough. The lights-off automation now also checks every 5 minutes, and whenever a light has been on for 3 minutes, whether the area is empty.
One radar lit the living area at 3am. A single ceiling radar on its own decided someone was in the lounge at 3:19am. None of the other sensors saw anything. Now, between 10pm and 6am, the lights need two different radars to see someone within 60 seconds, at least one of them in the living area. In the day, one is enough. That check is its own template sensor, which the curve uses in place of the plain group (lights-off still uses the group). I’ve left it out of the YAML above to keep it readable.
Questions and comments
Loading comments…