How to correctly pass sensor state into action?

Hi all,

First up, I have had home assistant for years but have only just started diving into anything beyond some buttons for irrigation solenoids, my knowledge is limited but growing fast.

I have a roborock vacuum which can take multiple room segment id’s in the form of {x,xx,xxx].

In order to dynamically clean multple rooms I have several input_booleans for rooms to select and I have arduously put together a template sensor as follows:

  - platform: template
    sensors:
      rooms_to_clean:
        friendly_name: "Rooms to Clean"
        value_template: >
          {% set rooms = [
            {'name': 'input_boolean.clean_entrance', 'number': 19},
            {'name': 'input_boolean.clean_kitchen', 'number': 24},
          ] %}
          
          {% set cleaned = rooms | selectattr('name', 'in', states.input_boolean | selectattr('state', 'eq', 'on') | map(attribute='entity_id') | list) | map(attribute='number') | list %}
          
          {% if cleaned | length > 0 %}
          [{{ cleaned | join(',') }}]
          {% else %}
            "None"
          {% endif %}

This displays correctly as [19,24] when I show the values in dev tools or on the dashboard. My issues arise when I try and use this in the segments specification for the button to start vacuuming. See first a working dashboard button that cleans both rooms:

      - show_name: true
        show_icon: true
        type: button
        entity: vacuum.q7_max
        name: Testing (Entry, Kitchen)
        icon: mdi:alert
        tap_action:
          action: perform-action
          perform_action: vacuum.send_command
          data:
            command: app_segment_clean
            params:
              - segments: [19,24]
                repeat: 1
          target:
            entity_id: vacuum.q7_max
        icon_height: 30px

And now this, which calls the sensor, but does not work:


      - show_name: true
        show_icon: true
        type: button
        entity: vacuum.q7_max
        name: Testing (Entry, Kitchen)
        icon: mdi:alert
        tap_action:
          action: perform-action
          perform_action: vacuum.send_command
          data:
            command: app_segment_clean
            params:
              - segments: {{states(sensor.cleaned_rooms)}}
                repeat: 1
          target:
            entity_id: vacuum.q7_max
        icon_height: 30px

I have tried several variations of calling the sensor values. I assume, but cannot find, information to support the idea that integrations may either not accept template values, or perhaps the string is not accepted?

If anyone is able to enlighten me on where I am going wrong it would be a great learning opportunity appreciated, cheers.

With the exception of the Markdown Card, none of the other cards support Jinja2 templates.

That’s why the first example you posted works (segments is a hard-coded list value) and the second example doesn’t.

I suggest you create a script. The card’s tap_action calls the script. The script uses the vacuum.send_command action and references your sensor via a template.

NOTE

You’re likely to run into the following issue.

The state value for all entities is a string. Regardless if it looks like a list, number, or dict, it’s handled as a string.

That means this {{ states(sensor.cleaned_rooms) }} returns a string. The segments option expects a list.

If the following fails to work within the script’s vacuum.send_command

  segments: "{{ states('sensor.cleaned_rooms') }}"

then I suggest you use this

  segments: "{{ states('sensor.cleaned_rooms') | from_json }}"

It converts strings that look like a list/dict into a true list/dict.


EDIT

Corrected an oversight. Forgot to wrap entity_id in single-quotes.
See post below by reste_narquois

The string issue makes a lot of sense, I’ve implemented a script and I call the script with a dashboard button now.

Unfortunately the same scenario arises, hard coded values work, but not the sensor state. Is there a method to view the exact call that is being made to the vacuum, or at least see the type (dict, string, etc) at run time?

Post the script’s code.

Examine the script’s trace.

Shouldn’t the sensor’s entity name be in quotes at least?

segments: "{{ states('sensor.cleaned_rooms') | from_json }}"
1 Like

Yes. Unless one has a variable named sensor, which contains a dict with the desired value stored at key cleaned_rooms. But that seems unlikely.

As it turns out, the answer by @123 was the solution. I’m not sure what happened this morning, perhaps just tired. But with renewed gusto this evening I gave it another shot and it worked.

1 Like