Script to take multiple entities individually

Hello,

I am trying to get the a script done. I should:

  • take a cover/entity_id as parameter
  • check if it’s state is “closed”
  • send an open command
  • wait for some seconds
  • send a stop command

so far this is working fine.

Now - with the help of the forum search and the manual - I extended this script to handle multible entity_ids. But the problem is now, that I can only check one of the entity state and run the service only on all or on none of the entities. What I actually wanted is, that each cover/entity_id is checked and opened separately.

cover_open_checked:
  alias: cover opening if closed
  mode: single
  sequence:
    - condition: template
      value_template: "{% for e in entity_id %} {% if loop.first %} {{ is_state( e , 'closed') }} {% else %} {% endif %}{% endfor %}\n"
    - data_template:
        entity_id: "{{ entity_id | join(', ') }}"
      service: cover.open_cover
    - delay: 00:00:02
    - data_template:
        entity_id: "{{ entity_id | join(', ') }}"
      service: cover.stop_cover

cover_office_open:
  alias: shades open in office
  sequence:
    - data:
        entity_id:
          - cover.erdgeschoss_buro_1
          - cover.erdgeschoss_buro_2
          - cover.erdgeschoss_buro_3
      service: script.cover_open_checked

I was not able to concatenate multiple is_state results for the condition template - e.g. I could make the template result into true and true and true but I would no longer start the script. Even if this that would work, it would still not solve the overall problem.

The whole sequence needs to be somehow templated - something that seems not to be possible - at least its not an option listed here: https://www.home-assistant.io/docs/scripts/

At that point I am not even sure if this is going to work using just one script or if that I idea needs a completely different solution. Therefore I am out of possible search terms.

would be glad if someone could help here :smiley:

Create a group containing the three covers.

group:
  covers_buro:
    name: Covers Buro
    entities:
      - cover.erdgeschoss_buro_1
      - cover.erdgeschoss_buro_2
      - cover.erdgeschoss_buro_3

In the first script, use a template to expand the group and select the covers that are closed.

cover_office_open:
  alias: shades open in office
  sequence:
    - data_template:
        entity_id: "{{expand('group.covers_buro') | selectattr('state', 'eq', 'closed') | list | join(', ') }}"
      service: script.cover_open_checked

In the second script, simply use the entities that are passed to it:

cover_open_checked:
  alias: cover opening if closed
  mode: single
  sequence:
    - data_template:
        entity_id: "{{ entity_id }}"
      service: cover.open_cover
    - delay: 00:00:02
    - data_template:
        entity_id: "{{ entity_id }}"
      service: cover.stop_cover

wow. thank you - you opened up a whole new chapter of templating for me - reading through jinja…

I ended up doing this: instead cover_office_open and using a group directly, I just check all incoming entities if their state and only forward the closed ones:

cover_open_check:
  alias: only forwards closed covers
  sequence:
    - data_template:
        entity_id: "{{ expand(entity_id) | selectattr('state', 'eq', 'closed') | map(attribute='entity_id') | join(', ') }}"
      service: script.cover_open_checked

thank you!

2 Likes