Automation entity_id fetch with loop

Hello,

i hade a question about action.
this is my yaml code:

`action:

  • service: cover.open_cover
    entity_id: >
    {% for device in states -%}
    {%- if Cover.lower() in device.entity_id -%}
    {{ device.entity_id }}
    {%- endif -%}
    {%- endfor -%}`

this raises an error.
does anyone how to handle this.

thx

What error are you getting? I’d start with that “if” code. What’s the value of Cover on each pass? It’s likely undefined for most passes.

i modified the code like this:

OpenCoverIntent:
  action:
    - service: cover.open_cover
      data_template:
        entity_id: >
          {% for device in states -%}
            {%- if Cover.lower() in device.entity_id -%}
              {{ device.entity_id }},
            {%- endif -%}
          {%- endfor -%}

i got his error:
ERROR (MainThread) [homeassistant.core] Invalid service data for cover.open_cover: Entity ID is an invalid entity id for dictionary value @ data[‘entity_id’]. Got ‘cover.bad_gross_io0203519077805265811,cover.bad_klein_io0203519077809104199,’

Seems like I saw someone run into this in another thread. Generating a list of entity_id’s wasn’t working. It’s like HA sees it as one string instead of a list. Not sure how to create a list in Jinja (templates), but casting as a list might take care of that.

1 Like

I fixed.
i wrote an python script which does this :wink: