Lost with templating (again) with list of entity IDs

I’m trying to be frugal in creating an automation whereby I have one trigger with two possible entities and then use the trigger.entity_id to control the actions. Specifically, I have two powered windows and want to flash lights adjacent to the relevant one when it’s opened or closed. I have the automation working fine separately for testing, but now I’m trying to render the actions to build the entity_id lists for each light_on and off action.

What I have so far is below. I know it must look a mess to anyone who knows what they’re doing and I apologise for that.

service: light.turn_on
metadata: {}
data: {}
target:
  entity_id: >
    {% set trigger_entity = trigger.entity_id %}{% if 'left' in
    trigger_entity%}- light.kitchen_light_m_2{% else %}- light.kitchen_light_o_2
    {% endif %}  

    {% if 'left' in trigger_entity %}- light.kitchen_light_n_2 {% else %}-
    light.kitchen_light_p_2{% endif %}

I expect that I simply have the wrong approach and trying to replicate the standard layout with a template is completely wrong?

You would need to share the whole automation to get help with this. That references the trigger and it also requires a target as opposed to an entity,so there could be may things going on there.

If that’s as far as you got, that’s good to know as well.

Correct, you cannot “build” a YAML list in situ with a template like that. Instead of building a list, what you have built is a list-shaped string… but that string doesn’t make sense to the YAML interpreter. One way to make it work is to have the template return real lists:

service: light.turn_on
metadata: {}
data: {}
target:
  entity_id: >
    {% if 'left' in trigger.entity_id %}
      {{ ['light.kitchen_light_m_2', 'light.kitchen_light_n_2'] }}
    {% else %}
      {{ ['light.kitchen_light_o_2', 'light.kitchen_light_p_2'] }}
    {% endif %}

That’s great. Thank you very much. I’d spent well over an hour trying to Google the correct answer and it was very frustrating. I could see it was wrong and suspected why, but simply couldn’t see what the correct answer should be.

I shall add that to my list of “helpful solutions”!

1 Like