As an exercise, I created an automation that employs the new (in 0.113) choose
option. Although I don’t currently have the means of testing it, I believe it uses the correct syntax required by choose
. If anyone thinks it seems rather long and repetitive, you’re not wrong.
Click to reveal the automation
- alias: kitchen lights routine
initial_state: true
trigger:
- platform: sun
event: sunrise
offset: "-00:10:00"
- platform: time
at: "09:00:00"
- platform: time
at: "17:00:00"
- platform: sun
event: sunset
- platform: time
at: "21:00:00"
action:
- choose:
- conditions:
- condition: template
value_template: "{{ trigger.event is defined and trigger.event == 'sunrise' }}"
sequence:
service: light.turn_on
data_template:
entity_id: light.kitchen_cabinet_lightstrip
brightness: 255
rgb_color: [240,172,0]
transition: 600
- conditions:
- condition: template
value_template: "{{ trigger.now is defined and trigger.now.hour == 9 }}"
sequence:
service: light.turn_on
data_template:
entity_id: light.kitchen_cabinet_lightstrip
brightness: 255
rgb_color: [0,240,216]
transition: 600
- conditions:
- condition: template
value_template: "{{ trigger.now is defined and trigger.now.hour == 17 }}"
sequence:
service: light.turn_on
data_template:
entity_id: light.kitchen_cabinet_lightstrip
brightness: 255
rgb_color: [250,130,160]
transition: 600
- conditions:
- condition: template
value_template: "{{ trigger.event is defined and trigger.event == 'sunset' }}"
sequence:
service: light.turn_on
data_template:
entity_id: light.kitchen_cabinet_lightstrip
brightness: 175
rgb_color: [150,215,250]
transition: 600
- conditions:
- condition: template
value_template: "{{ trigger.now is defined and trigger.now.hour == 21 }}"
sequence:
service: light.turn_on
data_template:
entity_id: light.kitchen_cabinet_lightstrip
brightness: 75
rgb_color: [85,95,205]
transition: 600
A benefit of performing the exercise is that it made me see a pattern that I can replicate using the current version of Home Assistant. All I need is to make five scripts, one for each of the five triggers and then the automation is reduced to this:
- alias: kitchen lights routine
initial_state: true
trigger:
- platform: sun
event: sunrise
offset: "-00:10:00"
- platform: time
at: "09:00:00"
- platform: time
at: "17:00:00"
- platform: sun
event: sunset
- platform: time
at: "21:00:00"
action:
service_template: >
{% set name = trigger.event if trigger.event is defined else trigger.now.hour %}
script.kitchen_lightstrip_{{name}}
Here are the five scripts:
#script:
kitchen_lightstrip_sunrise:
sequence:
service: light.turn_on
data:
entity_id: light.kitchen_cabinet_lightstrip
brightness: 255
rgb_color: [240,172,0]
transition: 600
kitchen_lightstrip_sunset:
sequence:
service: light.turn_on
data:
entity_id: light.kitchen_cabinet_lightstrip
brightness: 175
rgb_color: [150,215,250]
transition: 600
kitchen_lightstrip_9:
sequence:
service: light.turn_on
data:
entity_id: light.kitchen_cabinet_lightstrip
brightness: 255
rgb_color: [0,240,216]
transition: 600
kitchen_lightstrip_17:
sequence:
service: light.turn_on
data:
entity_id: light.kitchen_cabinet_lightstrip
brightness: 255
rgb_color: [250,130,160]
transition: 600
kitchen_lightstrip_21:
sequence:
service: light.turn_on
data:
entity_id: light.kitchen_cabinet_lightstrip
brightness: 75
rgb_color: [85,95,205]
transition: 600
It fails to meet the original requirement of having “everything in one automation” but, given the automation’s triggering requirements (and need for setting rgb_color
), that’s currently impractical. It will become practical when choose
is available.