Call service: how to pass a list as a service_data parameter

There is a vacuum robot which has a service accepting room numbers for cleaning:

      - service: vacuum.send_command
        data:
          entity_id: vacuum.xiaomi_roborock_s50
          command: app_segment_clean
          params: [1, 2, 3]

or

          ...
          params:
            - 1
            - 2
            - 3

Here is a code for a template sensor to generate that list:

          {% set ns = namespace(COMMAND = "") -%}
          {%- set ROOM_FLAGS = expand('group.vacuum_clean_rooms') -%}

          {%- for flag in ROOM_FLAGS -%}
            {%- if flag.entity_id | regex_match("input_boolean.vacuum_clean_room_", ignorecase=False) and
                   is_state(flag.entity_id,'on') -%}
              {%- set ROOM = flag.entity_id.split("vacuum_clean_room_")[1] -%}
              {%- set ROOM_NUMBER = states('input_number.vacuum_clean_room_number_' + ROOM)|int -%}
              {%- set CLEAN_COUNT = states('input_number.vacuum_clean_room_count_' + ROOM)|int -%}
              {%- set ROOM_NUMBER_STRING = (ROOM_NUMBER|string + ",") * CLEAN_COUNT -%}
              {%- set ns.COMMAND = ns.COMMAND + ROOM_NUMBER_STRING -%}
            {%- endif -%}
          {%- endfor -%}
          {%- set ns.COMMAND = (ns.COMMAND)[:-1] -%}
          {{ ns.COMMAND }}

Output in Developer Tools → Template:
image

Here is my script:

script:

  vacuum_clean_rooms:
    alias: 'vacuum: Clean rooms'
    sequence:
      - service: vacuum.send_command
        data:
          entity_id: vacuum.xiaomi_roborock_s50
          command: app_segment_clean
          params: "{{ states('sensor.vacuum_clean_command') }}"

And I am getting this error:
Unable to send command to the vacuum: {'code': -10000, 'message': 'data for segment is not a number'}

I think that the "{{ states('sensor.vacuum_clean_command') }}" expression produces not a list…

Although the state value of all entities is a string, native type conversion should interpret the state value of sensor.vacuum_clean_command to be a list if it looks like a list.

But in fact it does not…

Then it’s either incapable of interpreting the state value as a list or the params option is being picky. :slightly_smiling_face:

Either way, I can’t test it because I don’t have a vacuum entity.

Thank you very much anyway for efforts!

Finally my vacuum accepts this corrected script:

            {% set ns = namespace(COMMAND = "") -%}
            {%- set ROOM_FLAGS = expand('group.vacuum_clean_rooms') -%}
            {%- for flag in ROOM_FLAGS -%}
              {%- if flag.entity_id | regex_match("input_boolean.vacuum_clean_room_", ignorecase=False) and
                    is_state(flag.entity_id,'on') -%}
                {%- set ROOM = flag.entity_id.split("vacuum_clean_room_")[1] -%}
                {%- set ROOM_NUMBER = states('input_number.vacuum_clean_room_number_' + ROOM)|int -%}
                {%- set CLEAN_COUNT = states('input_number.vacuum_clean_room_count_' + ROOM)|int -%}
                {%- set ROOM_NUMBER_STRING = (ROOM_NUMBER|string +",") * CLEAN_COUNT -%}
                {%- set ns.COMMAND = ns.COMMAND + ROOM_NUMBER_STRING -%}
              {%- endif -%}
            {%- endfor -%}
            {%- set ns.COMMAND = (ns.COMMAND)[:-1] -%}
            [{{ ns.COMMAND }}]

The difference between this code and the previous one is in the last row.