In an automation that uses choose
can common conditions be placed separately instead of repeating them in each choose
?
For example, in my current automation each choose
rule has 3 conditions which are common across all of them, these being:
Common (repeated) conditions:
- condition: state
entity_id: input_boolean.holiday_mode
state: 'off'
- condition: time
after: '20:00:00'
before: '03:00:00'
- condition: template
value_template: '{{ is_state("person.m", "home") or is_state("person.r", "home") }}'
Below is my current automation, and a ‘proposed’ automation showing what I’m thinking.
Current automation
- alias: Bedtime
id: bedtime
mode: single
trigger:
- platform: state
entity_id:
- sensor.iphone_r_battery_state
- sensor.iphone_m_battery_state
to: Charging
from:
- Not Charging
- Full
action:
- choose:
# If R first
- conditions:
- condition: state
entity_id: input_boolean.holiday_mode
state: 'off'
- condition: template
value_template: '{{ is_state("sensor.iphone_m_battery_state", "Not Charging") }}'
- condition: template
value_template: '{{ is_state("person.m", "home") or is_state("person.r", "home") }}'
- condition: time
after: '20:00:00'
before: '03:00:00'
sequence:
- service: script.r_first_to_bed
# ElseIf M first
- conditions:
- condition: state
entity_id: input_boolean.holiday_mode
state: 'off'
- condition: template
value_template: '{{ is_state("sensor.iphone_r_battery_state", "Not Charging") }}'
- condition: time
after: '20:00:00'
before: '03:00:00'
- condition: template
value_template: '{{ is_state("person.m", "home") or is_state("person.r", "home") }}'
sequence:
- service: script.m_first_to_bed
# ElseIf Both to Bed
- conditions:
- condition: state
entity_id: input_boolean.holiday_mode
state: 'off'
- condition: template
value_template: '{{ is_state("sensor.iphone_m_battery_state", "Charging") and is_state("sensor.iphone_r_battery_state", "Charging")}}'
- condition: template
value_template: '{{ is_state("person.m", "home") or is_state("person.r", "home") }}'
- condition: time
after: '20:00:00'
before: '03:00:00'
- condition: state
entity_id: media_player.living_room_tv
state: 'off'
sequence:
- service: script.goodnight
Proposed automation
- alias: Bedtime
id: bedtime
mode: single
trigger:
- platform: state
entity_id:
- sensor.iphone_r_battery_state
- sensor.iphone_m_battery_state
to: Charging
from:
- Not Charging
- Full
condition: and
conditions:
- condition: state
entity_id: input_boolean.holiday_mode
state: 'off'
- condition: time
after: '20:00:00'
before: '03:00:00'
- condition: template
value_template: '{{ is_state("person.m", "home") or is_state("person.r", "home") }}'
action:
- choose:
# If R first
- conditions:
- condition: template
value_template: '{{ is_state("sensor.iphone_m_battery_state", "Not Charging") }}'
sequence:
- service: script.rachel_first_to_bed
# ElseIf M first
- conditions:
- condition: template
value_template: '{{ is_state("sensor.iphone_r_battery_state", "Not Charging") }}'
sequence:
- service: script.martin_first_to_bed
# ElseIf Both to Bed
- conditions:
- condition: template
value_template: '{{ is_state("sensor.iphone_m_battery_state", "Charging") and is_state("sensor.iphone_r_battery_state", "Charging")}}'
- condition: state
entity_id: media_player.living_room_tv
state: 'off'
sequence:
- service: script.goodnight