Loop through group fails

Hello everyone,

i have a question because my automation which loops through a group fails.
I create a script that checks the current position of a roller shutter and set the new position only if current position > new position

Then i create a single automation with a cover entity and a position entity and it works. My script is:

alias: Neues Skript
sequence:
  - service: cover.set_cover_position
    target:
      entity_id: "{{cover_entity}}"
    data:
      position: >
        {% if int(state_attr(cover_entity, 'current_position')) >
        int(states(position)) %}
          {{states(position) | int}}        
        {% endif %}       
mode: single
fields:
  cover_entity:
    selector:
      entity: {}
    name: Entität
    required: true
  position:
    selector:
      entity: {}
    name: Position
    required: true

Then i create an automation to loop througt a cover group to call my script above but this fails with the error code:

Fehler: Template rendered invalid entity IDs:

the code of my automation is:

alias: "Rollo: Steuerung Gruppe runter"
description: ""
trigger: []
condition: []
action:
  - repeat:
      sequence:
        - service: script.cover_set_position
          metadata: {}
          data:
            entity: "{{repeat.item}}"
            position: |
              input_number.{{repeat.item.replace('cover.','')}}_sollwert
      for_each: "{{ state_attr('cover.automatisch_herunter', 'entity_id') }}"
mode: single

Does anyone know where is my error in the automation?

I think the reason might be that you pass the current cover into the script declared as: entity rather than: cover_entity like your script’s field variable expects.
So i think it should look like this: cover_entity: "{{repeat.item}}"

If that isn’t the cause then maybe you could provide some trace logs.

position needs to be an integer, not the entity ID of an Input number.

yaml</s> <s>action:</s> <s> - repeat:</s> <s> for_each: "{{ state_attr('cover.automatisch_herunter', 'entity_id') }}"</s> <s> sequence:</s> <s> - service: script.cover_set_position</s> <s> data:</s> <s> cover_entity: "{{repeat.item}}"</s> <s> position: "{{ states(repeat.item | replace('cover.', 'input_number.') ~ '_sollwert') | int(0) }}"</s> <s>mode: single</s> <s>

EDIT: See below

Isn’t the target only used for script.turn_on to reference the target script?

As stated in the docs: “The other way is calling the script as a service directly. In this case, all service data will be made available as variables”

If the Script is called as a service directly, only the service data should be published, right?

In my case, I forgot to add the entity_id before “{{repeat.item}}”
So with the variable called cover_entity, I believe you’d need to change the target to service data:

action:
  - repeat:
      for_each: "{{ state_attr('cover.automatisch_herunter', 'entity_id') }}"
      sequence:
        - service: script.cover_set_position
          data:
            position: "{{ states(repeat.item | replace('cover.', 'input_number.') ~ '_sollwert') | int(0) }}"
            cover_entity:
              entity_id: "{{repeat.item}}"
mode: single

Please correct me if this is wrong. I have a similar script with variables that works this way.

You’re right… apparently while I was looking over the automation I forgot about the script :man_facepalming:

@JC00P3R You probably want to move the value comparison to a condition instead of having a template with if but no else. It doesn’t always cause problems, but there have been other threads where user report issues with similar setups.

alias: Neues Skript
sequence:
  - condition: template
    value_template: |
      {{ state_attr(cover_entity, 'current_position') | int(0) 
      > states(position) | int(0) }}
  - service: cover.set_cover_position
    target:
      entity_id: "{{ cover_entity }}"
    data:
      position: "{{ states(position) | int(0) }}"        
mode: single
fields: ...

thx for your help. the solution was the condition in my script and the data names of my script in my loop. now it works like a charm and i’m a bit further on my detach from node-red