Automation - call scripts with multiple actions for entity list - sequentially?

Hi,

I’m sure I’m using search in a bad way - can’t find a sample that matches my desire to clean up the following:

  - alias: sunProtectService
  ##########################################
    trigger:
      - platform: state
        entity_id: 'input_boolean.sunprotect'
    action:
       - service: cover.set_cover_position
         data:
           entity_id: cover.fenster_bad
           position: 30
       - delay: 0:00:01
       - service: cover.set_cover_position
         data:
           entity_id: cover.fenster_wz
           position: 35
       - delay: 0:00:01
       - service: cover.set_cover_position
         data:
           entity_id: cover.fenster_ez
           position: 30
     [..]

I have that in multiple areas (up, down, down_street_earlier, down_sunprotection, …) - and find that it becomes very hard to maintain it this way.

The following is more in line with what looks ‘proper and maintainable’ to me - however I don’t find a way where to start a loop that would run two actions per entity (go to position + delay for a second so not all start at covers at once).

automation:
   [..]
    action:
      - data_template:
          input_entity: "[ cover.fenster_bad, cover.fenster_wz, ... ]"
          input_pos: 50
        service: script.rolladen2pos
script:
  rolladen2pos:
############################################
    alias: "Rolladen to Position"
    sequence:
      - service: notify.telegram_edelfull
        data_template:
          title: 'rolladen2pos'
          message: >
            Rolladen {{ input_pos }} to Position {{ input_entity.replace("_", "\\_") }}
      - delay: 0:00:01

This one kind of works - but calls the script once with all entities - where I’d like to get multiple, independent, sequential calls.

A little nudge to the right direction is appreciated.

ItsMee

It’s not 100% clear what you want (your examples are not very consistent), but maybe something like this, starting with the automation:

  - alias: sunProtectService
  ##########################################
    trigger:
      - platform: state
        entity_id: 'input_boolean.sunprotect'
    action:
      - service: script.set_cover_pos
        data:
          position: 30
          entity_id:
            - cover.fenster_bad
            - cover.fenster_wz
            - cover.fenster_ez
            - ...

… and the script:

set_cover_pos:
  sequence:
    - repeat:
        count: "{{ entity_id|length }}"
        sequence:
          - service: cover.set_cover_position
            data_template:
              entity_id: "{{ entity_id[repeat.index - 1] }}"
              position: "{{ position }}"
          - delay: 1
2 Likes

It’s not 100% clear what you want (your examples are not very consistent)

Fair - but I think you got it right, that looks very good. Will test & feedback - thanks!

FWIW, there is a feature request that I’ve added to my “to do” list that would make this even easier. See:

If/when this is done the suggested implementation above could be simplified to:

  - alias: sunProtectService
  ##########################################
    trigger:
      - platform: state
        entity_id: 'input_boolean.sunprotect'
    action:
      - repeat:
          for_each:
            - cover.fenster_bad
            - cover.fenster_wz
            - cover.fenster_ez
            - ...
          sequence:
            - service: cover.set_cover_position
              data_template:
                entity_id: "{{ repeat.value }}"
                position: 30
            - delay: 1

Feel free to vote for the feature request. :smile:

2 Likes

A draft PR has been created.