For loop & templating

Does jinja2 & HA allow looping, and if so, what would be the proper way to do this within an automation?

Essentially, I have this automation for one light:

alias: Morning Office
  trigger:
    - platform: state
      entity_id: light.office
      from: 'off'
      to: 'on'
  condition:
    - condition: time
      after: '06:00'
      before: '19:00'
  action:
    - service: light.turn_on
      entity_id: light.office
      data:
        brightness: 255

But I have like 10 lights I want to do this for, i.e. if light.kitchen is turned on, then set light.kitchen to 255 brightness.

I’ve used jinja2 templating with Python/Flask before… just not sure how to incorporate this into YML

When posting YAML code, please follow the instructions at the top of the page to properly format it. If you don’t, then it’s really difficult to read the code (as you can see from your post.)

Assuming you want to do this for all lights, then one way to do this is to use a state_changed event trigger for the automation with a condition that runs the action only if the domain is light.

automation:
  - alias: Full brightness for morning light turn on
    trigger:
      platform: event
      event_type: state_changed
    condition:
      - condition: template
        value_template: "{{ trigger.event.data.new_state.domain == 'light' }}"
      - condition: time
        after: '06:00'
        before: '19:00'
    action:
      service: light.turn_on
      data_template:
        entity_id: "{{ trigger.event.data.entity_id }}"
        brightness: 255

If you only want to do this for a subset of your lights, then you could create a group (e.g., group.morning_lights) with the lights you care about, and then change the template condition to check that the entity whose state just changed is in that group.

      - condition: template
        value_template: >
          {{ trigger.event.data.entity_id in
               state_attr('group.morning_lights', 'entity_id') }}

Or, a more “brute force” (and slightly simpler) way to do it is to simply list all the lights you care about in the trigger.

automation:
  - alias: Full brightness for morning light turn on
    trigger:
      platform: state
      entity_id:
        - light.light1
        - light.light2
        ...
      to: 'on'
    condition:
      condition: time
      after: '06:00'
      before: '19:00'
    action:
      service: light.turn_on
      data_template:
        entity_id: "{{ trigger.entity_id }}"
        brightness: 255
1 Like

Thank you!

I actually tried formatting the code… and usually do… but for some reason, it just wasn’t having it. Fixed it now, though :slight_smile:

Actually, turns out that that first suggestion turns a light back on even if you’re turning it off.

Yeah, good point. Didn’t think of that at first. I guess the condition should probably be something more like:

      - condition: template
        value_template: >
          {{ trigger.event.data.new_state.domain == 'light' and
             trigger.event.data.new_state.state == 'on' and
             trigger.event.data.new_state.attributes.brightness != 255 }}

Hopefully that will work better. :slight_smile:

Or even:

      - condition: template
        value_template: >
          {% set s = trigger.event.data.new_state %}
          {{ s.domain == 'light' and s.state == 'on' and
             'brightness' in s.attributes and s.attributes.brightness != 255 }}
1 Like