Concatener room number to clean and send it to vacuum

Hello,
i have a list of input_boolean, 1 for each room.
I want to conctened all the room number that as to be cleaned and send it to the vacuum with:

service: vacuum.send_command
data:
  command: app_segment_clean
  params:
    - segments:

i made a template sensor that check the input boolean and concatened the value and then the script send it to the vacuum.

SENSOR:

  - platform: template
    sensors:
      room_to_clean:
          friendly_name: pièce a nettoyer
          value_template: >
             {% set segm = "[" %} 
             
             {% if is_state('input_boolean.aspi_cuisine','on') %}              
                  {% set segm = segm ~  16 %}   
             {% endif %}
    
             {% if is_state('input_boolean.aspi_salleamanger','on') %} 
                  {% if segm == "[" %}  
                    {% set segm = segm ~ 17 %}  
                  {% else %}                 
                    {% set segm = segm ~ "," ~ 17 %}  
                 {% endif %}
             {% endif %}
             
             {% if is_state('input_boolean.aspi_salon','on') %} 
                  {% if segm == "[" %}  
                    {% set segm = segm ~ 18 %}  
                  {% else %}                 
                    {% set segm = segm ~ "," ~ 18 %}  
                 {% endif %}
             {% endif %}
    
             {% if is_state('input_boolean.aspi_couloir','on') %} 
                  {% if segm == "[" %}  
                    {% set segm = segm ~ 19 %}  
                  {% else %}                 
                    {% set segm = segm ~ "," ~ 19 %}  
                 {% endif %} 
             {% endif %}
             
              {% set segm = segm ~ "]" %}  
             {{ segm }}

SCRIPT:

  - service: vacuum.send_command
    data:
      command: app_segment_clean
      params:
        - segments: "{{states('sensor.room_to_clean') }}"
          repeat: 1
    target:
      entity_id:
        - vacuum.roborock_s7

It work fine but i would like to make the concatened work in a script instead as use a sensor.
I tried with variable but i can’t manage to make the variable concatening.

Can anyone please help me?

How far did you get with the script? How did you try to implement it?

By the way:
concatenate verb; concatenated participle; concatenation noun

  - variables:
      rooms:
        input_boolean.aspi_cuisine: 16
        input_boolean.aspi_salleamanger: 17
        input_boolean.aspi_salon: 18
        input_boolean.aspi_couloir: 19         
  - service: vacuum.send_command
    data:
      command: app_segment_clean
      params:
        - segments: |
            {% set ns = namespace(segs=[]) %}
            {%  for bool in rooms if is_state(bool, 'on') %}
              {% set ns.segs = ns.segs + [rooms.get(bool)] %}
            {% endfor %}
            {{ ns.segs }}
          repeat: 1
    target:
      entity_id:
        - vacuum.roborock_s7
1 Like

Many thanks for the help