List of entity_id with data_template in script

Hello,

I would like to create a liste of entities with templating but i got an error when executing the script

    - service: cover.close_cover
      data_template: 
        entity_id: |
          {% if is_state('binary_sensor.chambre_contacteur_fenetre', 'on') and state_attr('cover.chambre', 'current_position') >= 22 %} - cover.chambre {% endif %}
          {% if is_state('binary_sensor.cuisine_contacteur_porte', 'on') and state_attr('cover.cuisine', 'current_position') >= 22 %} - cover.cuisine {% endif %}
          {% if (is_state('binary_sensor.sam_contacteur_porte_gauche', 'on') or is_state('binary_sensor.sam_contacteur_porte_droite', 'on')) and state_attr('cover.salle_a_manger', 'current_position') >= 22 %} - cover.salle_a_manger {% endif %}
          {% if is_state('binary_sensor.salon_contacteur_porte', 'on') and state_attr('cover.salon', 'current_position') >= 22 %} - cover.salon {% endif %}
          - none

The error : MultipleInvalid: not a valid value for dictionary value @ data[‘entity_id’]

How can i do it without create two scripts ?

Thanks you

You need to create the list then join the results. You can’t create listed items like - xxx.xxx for a single field. This is a limitation of yaml & jinja. The HA dev team created a work around where entity_id accepts a comma separated list. So if you want to dynamically add entity_ids, you have to conform to that rule:

    - service: cover.close_cover
      data_template: 
        entity_id: >
          {% set ns = namespace(entities=[]) %}
          {% if is_state('binary_sensor.chambre_contacteur_fenetre', 'on') and state_attr('cover.chambre', 'current_position') >= 22 %}
            {% set ns.entities = ns.entities + [ 'cover.chambre' ] %} 
          {% endif %}
          {% if is_state('binary_sensor.cuisine_contacteur_porte', 'on') and state_attr('cover.cuisine', 'current_position') >= 22 %}
            {% set ns.entities = ns.entities + [ 'cover.cuisine' ] %} 
          {% endif %}
          {% if (is_state('binary_sensor.sam_contacteur_porte_gauche', 'on') or is_state('binary_sensor.sam_contacteur_porte_droite', 'on')) and state_attr('cover.salle_a_manger', 'current_position') >= 22 %}
            {% set ns.entities = ns.entities + [ 'cover.salle_a_manger' ] %} 
          {% endif %}
          {% if is_state('binary_sensor.salon_contacteur_porte', 'on') and state_attr('cover.salon', 'current_position') >= 22 %}
            {% set ns.entities = ns.entities + [ 'cover.salon' ] %} 
          {% endif %}
          {% if ns.entities | count > 0 %}
            {{ ns.entities | join(', ') }}
          {% else %}
            none
          {% endif %}

Thanks you

This is my final code based on your’s

sequence:
    - service: cover.close_cover
      data_template: 
        entity_id: >-
          {% set entities = [] %}
          {% if is_state('binary_sensor.chambre_contacteur_fenetre', 'on') and state_attr('cover.chambre', 'current_position') >= 22 %}
            {% set entities = entities + [ 'cover.chambre' ] %} 
          {% endif %}
          {% if is_state('binary_sensor.cuisine_contacteur_porte', 'on') and state_attr('cover.cuisine', 'current_position') >= 22 %}
            {% set entities = entities + [ 'cover.cuisine' ] %} 
          {% endif %}
          {% if (is_state('binary_sensor.sam_contacteur_porte_gauche', 'on') or is_state('binary_sensor.sam_contacteur_porte_droite', 'on')) and state_attr('cover.salle_a_manger', 'current_position') >= 22 %}
            {% set entities = entities + [ 'cover.salle_a_manger' ] %} 
          {% endif %}
          {% if is_state('binary_sensor.salon_contacteur_porte', 'on') and state_attr('cover.salon', 'current_position') >= 22 %}
            {% set entities = entities + [ 'cover.salon' ] %} 
          {% endif %}
          {% if entities | count > 0 %}
            {{ entities | join(', ') }}
          {% else %} none {% endif %} ```