How do I use multiple entity_IDs in a data_template in a script?

Hello

I’m trying to make a script that checks if a light and a switch is turned on. if nether is, the light shall turn on, if the light is turned on, the switch shall turn on. If the switch is turned on the light shall turn on, if both is turned on both shall turn off. I’m gonna trigger the script with a trådfri remote.

I use a data_template where I set the entity with if statements. But I can’s set two entity_ids, what do i do wrong? And any ideas on how to solve it?

tradfri_remote:
  alias: Tradfri remote
  sequence:
    - service: homeassistant.toggle
      data_template:
        entity_id: >
          {% if is_state('light.moodlights', 'off') and is_state('switch.tradfri_livingroomtvlight', 'off') %}
            light.moodlights
          {% else %}
            {% if is_state('light.moodlights', 'on') and is_state('switch.tradfri_livingroomtvlight', 'off') %}
              switch.tradfri_livingroomtvlight
            {% else %}
              {% if is_state('light.moodlights', 'off') and is_state('switch.tradfri_livingroomtvlight', 'on') %}
                light.moodlights
              {% else %}
                - light.moodlights
                - switch.tradfri_livingroomtvlight
              {% endif %}
            {% endif %}
          {% endif %}
tradfri_remote:
  alias: Tradfri remote
  sequence:
    - service: homeassistant.toggle
      data_template:
        entity_id: >
          {% if is_state('light.moodlights', 'off') and is_state('switch.tradfri_livingroomtvlight', 'off') %}
            light.moodlights
          {% elif is_state('light.moodlights', 'on') and is_state('switch.tradfri_livingroomtvlight', 'off') %}
            switch.tradfri_livingroomtvlight
          {% elif is_state('light.moodlights', 'off') and is_state('switch.tradfri_livingroomtvlight', 'on') %}
            light.moodlights
          {% else %}
            light.moodlights, switch.tradfri_livingroomtvlight
          {% endif %}

Hello!

Thank you! That was a neater way to do it, but it didn’t work.

It’s the else statement that doesn’t work, the lights doesn’t turn off. I get this error message.

Invalid service data for light.toggle: Entity ID light.moodlights, switch.tradfri_livingroomtvlight is an invalid entity id for dictionary value @ data[‘entity_id’]. Got [‘light.moodlights, switch.tradfri_livingroomtvlight’]

@pnbruckner

I ended up doing like this, and that works, now I can toggle through the scenarios.

Thank you for your help!

tradfri_remote:
  alias: Tradfri remote
  sequence:
    - service: homeassistant.toggle
      data_template:
        entity_id: >
          {% if is_state('light.moodlights', 'off') and is_state('switch.tradfri_livingroomtvlight', 'off') %}
            light.moodlights
          {% elif is_state('light.moodlights', 'on') and is_state('switch.tradfri_livingroomtvlight', 'off') %}
            switch.tradfri_livingroomtvlight
          {% elif is_state('light.moodlights', 'on') and is_state('switch.tradfri_livingroomtvlight', 'on') %}
            light.moodlights
          {% elif is_state('light.moodlights', 'off') and is_state('switch.tradfri_livingroomtvlight', 'on') %}
            switch.tradfri_livingroomtvlight
          {% endif %}

Probably gonna make an automation that turns everything off if I long press the button.

Hmm, it’s been a while since I read how the homeassistant services work. I assumed it would do each entity in turn (calling the appropriate domain service), but apparently it looks at the first entity and calls a domain service appropriate for that entity and passes all the entities to it. Obviously, as you found, that doesn’t work.

FWIW, it’s valid to have a value for the entity_id parameter that is a comma separated “list” of individual entity_id’s. It’s just that the homeassistant.toggle service can’t handle that if the entities are from different domains.

Glad you got it working!