Amalgamate options

Hi,
I’d like to know if there’s a better way (using a template) to amalgamate the following code into a single automation (using a data template?). Apologies if this is really easy - i’ve read through the templating docs but im struggling to understand because im thick

- alias: 'Downstairs lights on when motion detected (Day)'
  initial_state: true
  trigger:
    - platform: state
      entity_id: binary_sensor.downstairs_motion
      to: 'on'
  condition:
    - condition: time
      after: '06:00:00'
      before: '17:00:00'
  action:
    - service: scene.turn_on
      entity_id: scene.day__downstairs

- alias: 'Downstairs lights on when motion detected (Evening)'
  initial_state: true
  trigger:
    - platform: state
      entity_id: binary_sensor.downstairs_motion
      to: 'on'
  condition:
    - condition: time
      after: '17:00:00'
      before: '22:00:00'
  action:
    - service: scene.turn_on
      entity_id: scene.evening__downstairs

- alias: 'Downstairs lights on when motion detected (Night)'
  initial_state: true
  trigger:
    - platform: state
      entity_id: binary_sensor.downstairs_motion
      to: 'on'
  condition:
    - condition: time
      after: '22:00:00'
      before: '06:00:00'
  action:
    - service: scene.turn_on
      entity_id: scene.night__downstairs

I think this should work:

- alias: 'Downstairs lights on when motion detected'
  initial_state: 'on'
  trigger:
    - platform: state
      entity_id: binary_sensor.downstairs_motion
      to: 'on'
  action:
    - service: scene.turn_on
      data_template: 
        entity_id: >
          {% if now().hour > 6 and now().hour < 17 %}
            scene.day__downstairs
          {% elif now().hour > 17 and now().hour < 22 %}
            scene.evening__downstairs
          {% else %}
            scene.night__downstairs
          {% endif %}
1 Like

thanks mate, just what I was after

You might see the scene.night__downstairs being run between 17:00h and 17:59h, though, try

{% elif now().hour > 16 and now().hour < 22 %}
1 Like

yup, i’d actually already picked up on that but i was real lazy in replying earlier!

1 Like

Good catch! I didn’t think about the lack of overlap. Seeing that I also noticed that the condition for time = 06:00 wouldn’t work either.

I should have made the ‘>’ into ‘>=’:

{% if now().hour >= 6 and now().hour < 17 %}
  scene.day__downstairs
{% elif now().hour >= 17 and now().hour < 22 %}
  scene.evening__downstairs
{% else %}
  scene.night__downstairs
{% endif %}

It will now start at 0600 (instead of 0700) for the “day” scene and it should do the same thing as your template for the 1700-1759 time frame but it makes the logic easier to read in my opinion.

it now better matches the original automations conditions.

:+1:
I like especially how you avoided the ‘after 22h AND before 06h’ vs. ‘after 22:00h OR before 06h’ issue :wink:

yeah, I was trying to work that out in a clear way with it hard coded but it was just less confusing and easier to make that time range the default.

my final code here for reference…

- alias: 'Downstairs lights on when motion detected'
  initial_state: 'on'
  trigger:
    - platform: state
      entity_id: binary_sensor.downstairs_motion
      to: 'on'
  action:
    - service: scene.turn_on
      data_template: 
        entity_id: >
          {% if now().hour >= 6 and now().hour < 17 %}
            scene.day__downstairs
          {% elif now().hour >= 17 and now().hour < 22 %}
            scene.evening__downstairs
          {% else %}
            scene.night__downstairs
          {% endif %}