Sending a state value as list

Hi there,

Im trying to get an automation working to send the state of an input_number as a list.
In my case im using a Xiaomi vacuum and trying to set the volume, which needs to be send as „params: [40]“ for example, the values need to be a list!

When I’m trying to send a fix value it works, but as soon as I insert a variable here it seems to be send as a string instead a list, which isn’t recognized by the vacuum.

Working fix values:

 - service: vacuum.send_command
    data_template: 
      entity_id: vacuum.roborock
      command: change_sound_volume
      params: [40]

Not working state values (Being send as string):

 - service: vacuum.send_command
    data_template: 
      entity_id: vacuum.roborock
      command: change_sound_volume
      params: "[{{ states('input_number.vacuum_volume') | int }}]"

If I don’t insert the quotes I get the attached error:

 - service: vacuum.send_command
    data_template: 
      entity_id: vacuum.roborock
      command: change_sound_volume
      params: "[{{ states('input_number.vacuum_volume') | int }}]"

Error:
Error loading /config/configuration.yaml: invalid key: “OrderedDict([(“states(‘input_number.vacuum_volume’) | int”, None)])”
in “/config/automation/automations.yaml”, line 308, column 0

For testing pruposes I inserted a push to my device to see what values are given, which looks fine (it’s still a string, so I can’t see the issue here):

- service: notify.ios_device
    data:
      message: "[{{ states('input_number.vacuum_volume') | int }}]"

Is there a hint why this isn’t working or does anybody has a solution?
Greets

Try:

 - service: vacuum.send_command
    data_template: 
      entity_id: vacuum.roborock
      command: change_sound_volume
      params: "{{ [ states('input_number.vacuum_volume') | int ] }}"

Thanks, tried that yesterday, already. But the robot doesn’t change the volume.

I guess there’s no way to check what data (and datatypes) are given to the send_command, right?

Alright, then this one should work:

 - service: vacuum.send_command
    data_template: 
      entity_id: vacuum.roborock
      command: change_sound_volume
      params: 
        - "{{ states('input_number.vacuum_volume') | int }}"

This keeps params as a list and you are templating the first item.

EDIT: This should also work:

 - service: vacuum.send_command
    data_template: 
      entity_id: vacuum.roborock
      command: change_sound_volume
      params: [ "{{ states('input_number.vacuum_volume') | int }}" ]

Jinja only outputs strings. So the data type is always a string, which I suspect is the underlying problem. So we need to provide a way to enforce that it is a list of items. Jinja will naturally convert the strings to numbers if they can but it will not convert a string to a complex type like a list.

1 Like

Thanks for these ideas, but unfortunately none of them works :confused:
Even this one isn’t Working:

- service: vacuum.send_command
    data_template: 
      entity_id: vacuum.roborock
      command: change_sound_volume
      params: ["{{ 1 + 1 }}"]

But it seems to calculate right, because if I insert this in my test notifications I get [2] as notification:

  - service: notify.ios_device
    data:
      message: "[{{1 + 1}}]"

The Params for the vacuum seems not to be a string, because if I insert

  - service: notify.ios_device
    data:
      message: [ "{{ states('input_number.vacuum_volume') | int }}" ]

I get this expected issue in my log:

voluptuous.error.MultipleInvalid: template value should be a string for dictionary value @ data['message']

Thats because message does not accept a list.

do not mix this up between templates. params may accept a list where message does not. Message will only accept a single string.

Did you try:

 - service: vacuum.send_command
    data_template: 
      entity_id: vacuum.roborock
      command: change_sound_volume
      params: 
        - "{{ states('input_number.vacuum_volume') | int }}"

Yes I only did this to check if the output is a list instead a string.

Yes that one didn’t work, too.

did you get an error with that last one?

No, it’s the same as with all other tries the robot just doesn’t change its volume.

Well i just noticed there’s an extra space on each line after the service line. Try it again with this.

 - service: vacuum.send_command
   data_template: 
     entity_id: vacuum.roborock
     command: change_sound_volume
     params: 
       - "{{ states('input_number.vacuum_volume') | int }}"

Thanks, I removed that space when pasting in Home assistant already, because I guessed I copied to much. So this error still exists.

To test if these could work I tried the following:

 - service: vacuum.send_command
    data_template: 
      entity_id: vacuum.roborock
      command: change_sound_volume
      params:
        - 10

Which isn’t working, too. (Doesn’t change the volume)

not sure if it’s a copy error, but it looks like service and data_template are not in the same line.