#Roborock clean segments automation fails

1 messages · Page 1 of 1 (latest)

frail cloak
#

This automation checks if any input_boolean switches are turned on. Each switch corresponds to an integer (representing a vacuum segment). If a switch is on, its value is added to a list (integer_list), which is then used to send a vacuum command.

Problem:
The integer_list is always empty, even when the switches are on. This prevents the vacuum command from being sent.

Expected Behavior:
• When a switch is on, its integer value should be added to the list.
• If the list is not empty, the vacuum command should be sent with the segments.
• If the list is empty, the automation does nothing.

alias: Input Booleans to Integer List Automation
trigger:
  - platform: state
    entity_id:
      - input_boolean.switch_1
      - input_boolean.switch_2
condition: []
action:
  - variables:
      input_boolean_int_mapping:
        - entity: 'input_boolean.switch_1'
          value: 29
        - entity: 'input_boolean.switch_2'
          value: 22
      integer_list: >
        {% set list = [] %}
        {% for item in input_boolean_int_mapping %}
          {% if is_state(item.entity, 'on') %}
            {% set list = list + [item.value] %}
          {% endif %}
        {% endfor %}
        {{ list }}

  - choose:
      - conditions:
          - condition: template
            value_template: "{{ integer_list | length > 0 }}"
        sequence:
          - service: vacuum.send_command
            data:
              command: app_segment_clean
              params:
                - segments: "{{ integer_list }}"
                  repeat: 1
            target:
              entity_id: vacuum.sample_vacuum
    default:
      - service: logbook.log
        data:
          name: Roborock
          message: "No segments were selected, so no cleaning was started."```
opal thorn
#

You have to use a namespace for changes to a variable in a for loop to be available outside it

#

it's also called out in the template documentation

frail cloak
#

Thank you, that did the trick!

opal thorn
#

you could also use this, which is simpler:
{{ input_boolean_int_mapping|selectattr('entity', 'is_state', 'on')|map(attribute='value')|list }}