A brief tutorial demonstrating how to create a single automation to control a light that is able to handle missed events (i.e. Home Assistant is off when the primary trigger(s) occur).
Implemented as two automations:
Turn on a light just before sunset
- alias: 'Scheduled Exterior On'
trigger:
- platform: numeric_state
entity_id: sun.sun
attribute: elevation
below: 1.8
action:
- service: light.turn_on
target:
entity_id: light.exterior
Turn off a light just before sunrise
- alias: 'Scheduled Exterior Off'
trigger:
- platform: numeric_state
entity_id: sun.sun
attribute: elevation
above: -3.1
action:
- service: light.turn_off
target:
entity_id: light.exterior
Consolidated into a single automation:
Turn light on/off just before sunset/sunrise
- alias: 'Scheduled Exterior'
trigger:
- platform: numeric_state
entity_id: sun.sun
attribute: elevation
below: 1.8
- platform: numeric_state
entity_id: sun.sun
attribute: elevation
above: -3.1
action:
- service: "light.turn_{{ 'on' if trigger.below is defined and trigger.below != None else 'off' }}"
target:
entity_id: light.exterior
Same automation but implemented with choose
:
Single automation using 'choose'
- alias: 'Scheduled Exterior'
mode: single
trigger:
- platform: numeric_state
entity_id: sun.sun
attribute: elevation
below: 1.8
- platform: numeric_state
entity_id: sun.sun
attribute: elevation
above: -3.1
action:
- variables:
e: "{{ state_attr('sun.sun', 'elevation') | float }}"
- choose:
- conditions: "{{ e <= 1.8 }}"
sequence:
- service: light.turn_on
target:
entity_id: light.exterior
- conditions: "{{ e >= -3.1 }}"
sequence:
- service: light.turn_off
target:
entity_id: light.exterior
Avoid needlessly turning the light on
(or off
) if it’s already on
(or off
).
Smarter version
- alias: 'Scheduled Evening Exterior'
trigger:
- platform: numeric_state
entity_id: sun.sun
attribute: elevation
below: 1.8
- platform: numeric_state
entity_id: sun.sun
attribute: elevation
above: -3.1
action:
- variables:
e: "{{ state_attr('sun.sun', 'elevation') | float }}"
- choose:
- conditions:
- "{{ e <= 1.8 }}"
- "{{ is_state('light.exterior', 'off') }}"
sequence:
- service: light.turn_on
target:
entity_id: light.exterior
- conditions:
- "{{ e >= -3.1 }}"
- "{{ is_state('light.exterior', 'on') }}"
sequence:
- service: light.turn_off
target:
entity_id: light.exterior
Make it handle missed events; evaluate the sun’s elevation, and the light’s state, on startup.
Smartest version
- alias: 'Scheduled Exterior'
trigger:
- platform: numeric_state
entity_id: sun.sun
attribute: elevation
below: 1.8
- platform: numeric_state
entity_id: sun.sun
attribute: elevation
above: -3.1
- platform: homeassistant
event: start
action:
- variables:
e: "{{ state_attr('sun.sun', 'elevation') | float }}"
- choose:
- conditions:
- "{{ e <= 1.8 }}"
- "{{ is_state('light.exterior', 'off') }}"
sequence:
- service: light.turn_on
target:
entity_id: light.exterior
- conditions:
- "{{ e >= -3.1 }}"
- "{{ is_state('light.exterior', 'on') }}"
sequence:
- service: light.turn_off
target:
entity_id: light.exterior