Cool. Yeah, I did something very similar! I just added a few extra things:
- A sensor helper that triggers if any light sensor labeled “illuminance_sensors” hits the threshold in the kitchen and the hallway to the kitchen (< 15 lux in the morning, < 25 lux from noon).
- Alarm system integration (the lights won’t turn on automatically if the night alarm is armed).
- A fixed timeframe when the lights should never turn on.
- A manual override so the lights stop switching automatically if the physical wall switch was used.
- I also set the mode to
restart. Since we use a delay before turning the lights off, this makes sure the countdown just resets if the sensor triggers again.
YAML for Home Assistant
Template binary sensor
{% set threshold = 15 if now().hour < 12 else 25 %}
{% set target_rooms = area_entities('kitchen') + area_entities('kitchen_hallway') %}
{% set is_dark = expand(label_entities('illuminance_sensors'))
| selectattr('entity_id', 'in', target_rooms)
| map(attribute='state') | map('float', 1000)
| select('lt', threshold)
| list | count > 0 %}
{{ is_dark }}
Automation
alias: Turn the kitchen light on/off automatically
description: ''
triggers:
- trigger: state
entity_id:
- binary_sensor.presence_sensor_fp2_ae68_presence_sensor_2
to:
- 'on'
- trigger: state
entity_id:
- binary_sensor.lux_threshold_met
to:
- 'on'
from:
- 'off'
conditions:
- condition: state
entity_id: binary_sensor.presence_sensor_fp2_ae68_presence_sensor_2
state:
- 'on'
- condition: state
entity_id: binary_sensor.lux_threshold_met
state:
- 'on'
- condition: state
entity_id: alarm_control_panel.camera_hub_g3_1765_security_system
state:
- disarmed
- unavailable
- unknown
enabled: true
- condition: time
after: '05:45:00'
before: '03:30:00'
- alias: Light switch not operated for 15 minutes
condition: template
value_template: >-
{{ ( as_timestamp(now()) -
as_timestamp(state_attr("automation.light_switch_on_off","last_triggered"))
|int(0) ) > 900 }}
actions:
- action: light.turn_on
metadata: {}
target:
area_id: kitchen
data: {}
- wait_for_trigger:
- trigger: state
entity_id:
- binary_sensor.presence_sensor_fp2_ae68_presence_sensor_2
to:
- 'off'
for:
hours: 0
minutes: 0
seconds: 20
- action: light.turn_off
metadata: {}
target:
area_id: kitchen
data: {}
mode: restart
