Template using multiple entity_ids

I’ve been working on developing a template to handle turning off groups of lights and switches when no motion has been detected for a certain amount of time. I’ve discovered that when using data_template and entity_id, you can only specify one entity.

My current code results in ERROR (MainThread) [homeassistant.components.homeassistant] homeassistant/turn_off cannot be called without entity_id when more than one entity is specified in the If/Elif logic

  action:
    - service: homeassistant.turn_off
      data_template: 
        entity_id: >
          {% if trigger.entity_id == 'binary_sensor.bedroom_motion' %}
            group.bedroom_lights
          {% elif trigger.entity_id == 'binary_sensor.livingroom_motion' %}
            group.living_room_lights, group.kitchen_lights, group.dining_room_lights
          {% endif %}

Adding quotes or forming a list results in homeassistant.exceptions.ServiceNotFound: Unable to find service - group/turn_off

  action:
    - service: homeassistant.turn_off
      data_template: 
        entity_id: >
          {% if trigger.entity_id == 'binary_sensor.bedroom_motion' %}
            - group.bedroom_lights
          {% elif trigger.entity_id == 'binary_sensor.livingroom_motion' %}
            - group.living_room_lights
            - group.kitchen_lights
            - group.dining_room_lights
          {% endif %}

Searching the forums, I found the following two threads attempting something similar but finding no solution:

One thread seemed to solve the issue in two ways (the first being similar to my current code above), but these methods seem to no longer work and result in homeassistant/turn_off cannot be called without entity_id:

Is this a bug or no longer feasible? All code checks out in the Jinja editor in Home Assistant, just not when triggering an automation.

As I mentioned in Issue #29240:

Most services, though, that take an entity_id parameter allow a comma separated list of entity_id 's.

Notice how I said “most services”, and that my example used the light.turn_off service. Unfortunately the homeassistant.turn_off service is not one of the services that accepts a comma separated list of entity_id’s.

EDIT: Is there a reason you don’t want to use the light.turn_off service instead which would work in your first automation?

EDIT2: Or maybe you could try creating a group of groups. E.g., if you created group.lr_kit_dr_lights that contained group.living_room_lights, group.kitchen_lights, group.dining_room_lights, then you could do:

  action:
    - service: homeassistant.turn_off
      data_template: 
        entity_id: >
          {% if trigger.entity_id == 'binary_sensor.bedroom_motion' %}
            group.bedroom_lights
          {% else %}
            group.lr_kit_dr_lights
          {% endif %}

I’m pretty sure groups are expanded recursively, so this should work.

You are correct, a group of groups would solve this as it creates one entity. The reason I was avoiding light.turn_off is because the groups contain a combination of lights (dimmable and color changeable) and switches (on/off only) and light.turn_off or switch.turn_off only turn off that domain.

In my config, the groups look as follows:

bedroom_table_lights:
   name: Bedroom Table Lights
   entities:
      - switch.bedroom_light1
      - switch.bedroom_light2
      
bedroom_lights:
   name: Bedroom Lights
   entities:
      - group.bedroom_table_lights
      - light.bedroom_overhead_light
      - switch.bedroom_closet_lights

dining_room_lights:
   name: Dining Room Lights
   entities:
      - switch.dining_room_light
      - light.storage_light

kitchen_lights:
   name: Kitchen Lights
   entities:
      - switch.kitchen_cabinet_lights
      - light.kitchen_overhead_lights
      - switch.bar_lights

So from code, group.bedroom works without issue as a group of groups.

1 Like