Data_template with multiple entity_id

Been trying to get an action to switch_on/off multiple entity_ids within a data_template for the “every light” case, no luck:

    data_template:
      entity_id: >
        {% if trigger.event.data["name"] == "living room light" %}
          switch.etekcity_0320_3
        {% elif trigger.event.data["name"] == "christmas light" %}
          switch.sonoff_s31_01   
        {% elif trigger.event.data["name"] == "every light" %} 
          switch.etekcity_0320_3, switch.sonoff_s31_01
        {% endif %}

I tried the two switches in separate lines, with and without a leading hyphen, straight after the %} and even with and without a hyphen as part of the line entity_id: > => entity_id: >- So far nothing does the trick.
Any ideas?

I know I could probably use a group as a workaround, but I’m trying to understand how it would work directly in the template - or at least why it doesn’t.

That should work. Maybe show the whole thing?

I know it should - but it does not :man_shrugging:
Thanks for looking at it :+1:

- alias: RHASSPY - turn on LR Light
  trigger:
    platform: event
    event_type: rhasspy_ChangeLightState
  action:
    service_template: >
      {% if trigger.event.data["state"] == "on" %}
        homeassistant.turn_on
      {% else %}
        homeassistant.turn_off    
      {% endif %}
    data_template:
      entity_id: >
        {% if trigger.event.data["name"] == "living room light" %}
          switch.etekcity_0320_3
        {% elif trigger.event.data["name"] == "christmas light" %}
          switch.sonoff_s31_01   
        {% elif trigger.event.data["name"] == "every light" %} 
          switch.etekcity_0320_3
          switch.sonoff_s31_01
        {% endif %}

If I take the 2nd switch out it works - but I actually have to remove it, commenting out results in an error.

Don’t you need a comma between multiple entity IDs for entity_id?

switch.etekcity_0320_3, switch.sonoff_s31_01

Never mind, the comma-way doesn’t work in data_template.
Maybe make a group of these 2 switches?

Put the comma back in, then try switch.turn_on and switch.turn_off instead of homeassistant.turn_…

Thanks @pnbruckner - it works, but it solves only one part of my problem.

In addition to the two switches shown, I also have three lights - that’s why I used the homeassistant.turn_ service instead of the switch.turn_ one :frowning_face:

Have you already considered the (brand new) Light Switch compopnent?

The idea would be to “convert” existing switches into virtual lights and then you would only use the light.turn_on command rather than homeassistant.turn_on.

Would that help?

Thanks for the hint @mastermarkush, due to a number of components not working properly in later versions my live install is a little behind - on V0.75.3 to be exact.
So I guess, a group it is.

I thought a template could only return a single line.

I didn’t think this would work:

{% elif trigger.event.data["name"] == "every light" %} 
  switch.etekcity_0320_3
  switch.sonoff_s31_01

Correct, it doesn’t. And many other options I tried don’t either.
That’s why I was trying to find a solution that does :wink:

1 Like