Using template for-loop to generate entity_id list

I am trying to write an automation that will dim any lights which are turned on, but not act on any that are off:

- alias: Dim Lights in Evening
  trigger:
    platform: sun
    event: sunset
    offset: "01:00:00"
  condition:
    condition: state
    entity_id: input_boolean.automate_lighting
    state: 'on'
  action:
    - service: light.turn_on
      data_template:
        entity_id: >
          [{%- for light in states.light -%}
          {%- if light.state == 'on' -%}
          {{ light.entity_id }},
          {%- endif -%}
          {%- endfor -%}]
        color_temp: 443
        brightness: 144

If I evaluate the template in the Template Editor and paste the output in place of the entity_id template, the automation works as desired.

However if I use the template to generate the entity_id list, the following error is printed to the log:

Invalid data for call_service at pos 1: not a valid value for dictionary value @ data['entity_id']

I have tried every variation I can think of based on the templating docs and other posts here, but cannot get it to work.

If anybody can tell me the correct way to do this it would be greatly appreciated :slight_smile:

Try removing the square brackets []

- alias: Dim Lights in Evening
  trigger:
    platform: sun
    event: sunset
    offset: "01:00:00"
  condition:
    condition: state
    entity_id: input_boolean.automate_lighting
    state: 'on'
  action:
    - service: light.turn_on
      data_template:
        entity_id: >
          {%- for light in states.light -%}
          {%- if light.state == 'on' -%}
          {{ light.entity_id }},
          {%- endif -%}
          {%- endfor -%}
        color_temp: 443
        brightness: 144

Here is maybe an easier way (and also handles the case where none of the lights are on):

- alias: Dim Lights in Evening
  trigger:
    platform: sun
    event: sunset
    offset: "01:00:00"
  condition:
    - condition: state
      entity_id: input_boolean.automate_lighting
      state: 'on'
    - condition: template
      value_template: >
        {{ states.light|selectattr('state','eq','on')|list|count > 0 }}
  action:
    - service: light.turn_on
      data_template:
        entity_id: >
          {{ states.light|selectattr('state','eq','on')
             |map(attribute='entity_id')|join(',') }}
        color_temp: 443
        brightness: 144
2 Likes

coincidentally busy with a related issue, I thought to use:

    - condition: template
      value_template: >
        {{state_attr('group.lights','on')}}

If any of the lights is on (your count >0) the group would be on? Change the group name of course.

Yes, that could work, too. In this case I’d just use group.all_lights which is automatically maintained.

yes that would be easiest and default. I mostly use a group.all_lights_only, which is hand made and maintained, because the group.all_lights also contains Hue groups, which are imported as light…Other advantage of the group usage is one can leave out several lights, that should always be exempt of any automation. Think of outside lights in this case, which are on at night but needn’t be dimmed by the automation.

I don’t understand how that template is supposed to work. It’s looking for an attribute named on in group.lights?

If I replace it with group.all_lights it produces the following results:

As expected, is_state produces a valid result but not state_attr (when defined that way).

What am I missing here?

Nothing. It should be is_state; I just looked right past that. lol! I’m sure that’s what @Mariusthvdb meant.

Well OK then! So you Jinja-ninjas are mortal after all! :slight_smile:

1 Like

:blush: I am so sorry… should have been:

{{is_state('group.all_inside_lights','on')}} 

as you can see, no lights on inside, but the group all_lights (or states.light in this template) has a light.outdoors on. Which is a Hue group (room).

sorry for my mistake. glad you noticed @123

Thanks @pnbruckner, that is much tidier and works exactly as desired!

1 Like

Supposing you have the more rooms and in each room more lights that you want to control by the remote that each room has its own, how would you change the data template for each group?

Sorry, I’m not quite sure what you’re asking. Can you explain a bit more about what you want?