Multiple entity_ids in automation based on state

Hi,

I have some problems with my automation. I want so dim only lights that are on. So I try to check if the entity has the state on. But the automation didn’t work. Maybe someone can help me with this? I didn get an error, it simply doesnt work:(

- alias: 'Media Player Start'
  trigger:
   - platform: state
     entity_id: media_player.wohnzimmer
     to: 'playing'
   - platform: state
     entity_id: media_player.ps4
     to: 'playing'
  action:
    - service: homeassistant.turn_on
      data_template:
        transition: "2"
        entity_id: >
          "{% if is_state('light.wohnzimmer_tv_rack', 'on') %}, light.wohnzimmer_tv_rack{% endif %}
          {% if is_state('light.wohnzimmer_hifi', 'on') %}, light.wohnzimmer_hifi{% endif %}
          {% if is_state('light.wohnzimmer_stehlampe_unten', 'on') %}, light.wohnzimmer_stehlampe_unten{% endif %}
          {% if is_state('light.wohnzimmer_stehlampe_oben', 'on') %}, light.wohnzimmer_stehlampe_oben{% endif %}
          {% if is_state('light.wohnzimmer_decke', 'on') %}, light.wohnzimmer_decke{% endif %}"
        brightness_pct: '{{ states.input_number.helligkeit_wohnzimmer_media_pause.state | round() }}'

try this

- alias: 'Media Player Start'
  trigger:
   - platform: state
     entity_id: media_player.wohnzimmer
     to: 'playing'
   - platform: state
     entity_id: media_player.ps4
     to: 'playing'
  action:
    - service: homeassistant.turn_on
      data_template:
        transition: "2"
        entity_id: >
          {% set entities = 'light.wohnzimmer_tv_rack', 
                            'light.wohnzimmer_hifi', 
                            'light.wohnzimmer_stehlampe_unten', 
                            'light.wohnzimmer_stehlampe_oben', 
                            'light.wohnzimmer_decke' %}
          {{ states | selectattr('entity_id','in',entities) | selectattr('state','eq','on') | map(attribute='entity_id') | list | join(', ') }}
        brightness_pct: >
          {{ states('input_number.helligkeit_wohnzimmer_media_pause') | float | round() }}

Hi @petro
thanks for your help but this didnt work:(
Did you have any clue what the problem is?

What didn’t work… What errors are in the logs?

The automation didnt dimm the lights:(
Here is the log:

2019-08-13 20:43:21 ERROR (MainThread) [homeassistant.core] Error doing job: Task exception was never retrieved
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/core.py", line 1213, in async_call
    processed_data = handler.schema(service_data)
  File "/usr/local/lib/python3.7/site-packages/voluptuous/schema_builder.py", line 267, in __call__
    return self._compiled([], data)
  File "/usr/local/lib/python3.7/site-packages/voluptuous/schema_builder.py", line 589, in validate_dict
    return base_validate(path, iteritems(data), out)
  File "/usr/local/lib/python3.7/site-packages/voluptuous/schema_builder.py", line 427, in validate_mapping
    raise er.MultipleInvalid(errors)
voluptuous.error.MultipleInvalid: not a valid value for dictionary value @ data['entity_id']

When only one Light is on, the automation works. Maybe the entities have to appear like
this:

- entity 1
- entity 2
- entity 3

instead of entity 1, entity 2, entity 3.
Can you give me a hint how to do that @petro ?

That’s not possible. You may not be able to do this. I was always under the impression it was possible to have a comma separated list.

Me too so I was perplexed by the negative result.

I tried the following (turn on a few lights that are normally off in my house) :

- alias: 'list test'
  trigger:
    platform: state
    entity_id: input_boolean.toggler
    to: 'on'
  action:
    service: homeassistant.turn_on
    data_template:
      entity_id: >
        {% set entities = 'light.family', 
                          'light.patio', 
                          'light.kitchen', 
                          'light.foyer' %}
        {{ states | selectattr('entity_id','in', entities) | selectattr('state','eq','off') | map(attribute='entity_id') | join(', ') }}

I got the same failure ( … not a valid value for dictionary value @ data[‘entity_id’]).

I was doubly perplexed because I am certain I’ve used this elsewhere successfully. Then I changed:

    service: homeassistant.turn_on

to:

    service: light.turn_on

and the automation worked.

So there must be a difference in how the two services validate their entity_id. I haven’t looked at the source code but I’m guessing homeassistant.turn_on has a more constrained interpretation of what is a valid entity_id whereas light.turn_on is more accommodating.


Another interpretation is that the validation code for homeassistant.turn_on has a bug …

1 Like

Thank you! \o/
With light.turn_on everything works \o/

That’s interesting. I’ll look into the code later for this. I’m wondering why that doesn’t work. I’d lean towards bug. But homeassistant.turn_on is more general so it’s up in the air.

Have you find the way to pass multiple entities to homeassistant.turn_on service? The only way I finded is:

test:
  sequence:
    - service: homeassistant.turn_on
      entity_id: ['switch.one', 'light.two']

But how to pass entities from data_template?

you can’t, it doesn’t work.

Try it:

- alias: 'Media Player Start'
  trigger:
   - platform: state
     entity_id: media_player.wohnzimmer
     to: 'playing'
   - platform: state
     entity_id: media_player.ps4
     to: 'playing'
  action:
    - service: homeassistant.turn_on
      data_template:
        transition: "2"
        entity_id: >
          {% for item in states['light'] if item.object_id.startswith('wohnzimmer_') and item.entity_id.state == 'on' -%}
            {% if not loop.first %},{% endif %}{{ item.entity_id }}
          {%- endfor %}
        brightness_pct: '{{ states.input_number.helligkeit_wohnzimmer_media_pause.state | round() }}'

This was fixed in https://github.com/home-assistant/core/pull/31509, which dates in February. After this entire post, making the information outdated.