Running roborock command to clean areas based on turned on input booleans

I want to be able to run an app_segment_clean command based on the state of helper input_booleans, each represents a single room, this is what I currently have. However, the template rendering for the mapping between an entity and area in the roborock map fails:

---
input_boolean:
  clean_roi_room:
    name: Roi's Room
    initial: off
  clean_adi_room:
    name: Adi's Room
    initial: off
  clean_kitchen:
    name: Kitchen
    initial: off
  clean_study:
    name: Study
    initial: off
  clean_pantry:
    name: Pantry
    initial: off
  clean_corridor1:
    name: Corridor1
    initial: off
  clean_guest_bedroom:
    name: Guest Bedroom
    initial: off
  clean_dining_room:
    name: Dining Room
    initial: off
  clean_kids_bathroom:
    name: Kids Bathroom
    initial: off
  clean_living_room_corridor:
    name: Living Room Corridor
    initial: off
  clean_bathroom:
    name: Bathroom
    initial: off
  clean_living_room:
    name: Living Room
    initial: off
  clean_entrance:
    name: Entrance
    initial: off
  clean_hall:
    name: Hall
    initial: off

input_button:
  start_cleaning:
    name: Start Cleaning

automation:
  - alias: 'Clean selected areas with Roborock'
    description: >
      Gather all checked areas, send one segment‐clean command
      with a YAML list of segments, then reset the toggles.
    trigger:
      - platform: state
        entity_id: input_button.start_cleaning

    action:
      - action: vacuum.send_command
        target:
          entity_id: vacuum.yulanda
        data:
          command: app_segment_clean
          params: >-
            {%- set areas = [
              {'entity':'input_boolean.clean_roi_room',            'segment':16},
              {'entity':'input_boolean.clean_adi_room',            'segment':17},
              {'entity':'input_boolean.clean_kitchen',             'segment':18},
              {'entity':'input_boolean.clean_study',               'segment':19},
              {'entity':'input_boolean.clean_pantry',              'segment':20},
              {'entity':'input_boolean.clean_corridor1',           'segment':21},
              {'entity':'input_boolean.clean_guest_bedroom',       'segment':22},
              {'entity':'input_boolean.clean_dining_room',         'segment':23},
              {'entity':'input_boolean.clean_kids_bathroom',       'segment':24},
              {'entity':'input_boolean.clean_living_room_corridor','segment':25},
              {'entity':'input_boolean.clean_bathroom',            'segment':26},
              {'entity':'input_boolean.clean_living_room',         'segment':27},
              {'entity':'input_boolean.clean_entrance',            'segment':28},
              {'entity':'input_boolean.clean_hall',                'segment':29}
            ] -%}
            {{ [ area.segment for area in areas if is_state(area.entity, 'on') ] }}

      - condition: template
        value_template: >-
          {{
            [ area for area in [
              {'entity':'input_boolean.clean_roi_room',            'segment':16},
              {'entity':'input_boolean.clean_adi_room',            'segment':17},
              {'entity':'input_boolean.clean_kitchen',             'segment':18},
              {'entity':'input_boolean.clean_study',               'segment':19},
              {'entity':'input_boolean.clean_pantry',              'segment':20},
              {'entity':'input_boolean.clean_corridor1',           'segment':21},
              {'entity':'input_boolean.clean_guest_bedroom',       'segment':22},
              {'entity':'input_boolean.clean_dining_room',         'segment':23},
              {'entity':'input_boolean.clean_kids_bathroom',       'segment':24},
              {'entity':'input_boolean.clean_living_room_corridor','segment':25},
              {'entity':'input_boolean.clean_bathroom',            'segment':26},
              {'entity':'input_boolean.clean_living_room',         'segment':27},
              {'entity':'input_boolean.clean_entrance',            'segment':28},
              {'entity':'input_boolean.clean_hall',                'segment':29}
            ] if is_state(area.entity,'on') ] | length > 0
          }}

      # 3) Turn off every helper that was on
      - repeat:
          for_each:
            - input_boolean.clean_roi_room
            - input_boolean.clean_adi_room
            - input_boolean.clean_kitchen
            - input_boolean.clean_study
            - input_boolean.clean_pantry
            - input_boolean.clean_corridor1
            - input_boolean.clean_guest_bedroom
            - input_boolean.clean_dining_room
            - input_boolean.clean_kids_bathroom
            - input_boolean.clean_living_room_corridor
            - input_boolean.clean_bathroom
            - input_boolean.clean_living_room
            - input_boolean.clean_entrance
            - input_boolean.clean_hall
          sequence:
            - condition: state
              entity_id: "{{ repeat.item }}"
              state: 'on'
            - service: input_boolean.turn_off
              target:
                entity_id: "{{ repeat.item }}"

Any ideas?

in my setup, I have a script:

alias: Run Vacuum
sequence:
  - data_template:
      command: app_segment_clean
      params:
        - segments: |
            {% set vacrooms = namespace(roomid=[]) %}
            {% for rooms in state_attr('sensor.roborock_rooms','rooms') -%}
              {% if is_state(rooms.boolean,'on') %}
                {% set room = rooms.id | int %}
                {% set vacrooms.roomid = vacrooms.roomid + [room] %}
              {% endif %}
            {%- endfor %}
            {{ vacrooms.roomid }}
    target:
      entity_id: vacuum.roborock_s7
    action: vacuum.send_command
mode: single
icon: mdi:robot-vacuum

With that sensor populated from JSON:

{
    "rooms": [
        {
            "name": "kitchen",
            "boolean": "input_boolean.vac_kitchen",
            "id": "17"
            },
        {
            "name": "dining_room",
            "boolean": "input_boolean.vac_dining_room",
            "id": "16"
            },
        {
            "name": "foyer",
            "boolean": "input_boolean.vac_foyer",
            "id": "26"
            },
        {
            "name": "koi_room",
            "boolean": "input_boolean.vac_koi_room",
            "id": "24"
            },
        {
            "name": "living_room",
            "boolean": "input_boolean.vac_living_room",
            "id": "19"
            },
        {
            "name": "master_bedroom",
            "boolean": "input_boolean.vac_master_bedroom",
            "id": "21"
            },
        {
            "name": "guest_bathroom",
            "boolean": "input_boolean.vac_guest_bathroom",
            "id": "23"
            },
        {
            "name": "guest_bedroom",
            "boolean": "input_boolean.vac_guest_bedroom",
            "id": "18"
            }
    ],
    "points": [
        {
            "name": "sink",
            "x": 17989,
            "y": 25491
         }
    ]
}

The sensor created as:

##
## Roborock Rooms 
##
- platform: rest
  name: roborock_rooms
  resource: http://192.168.1.245:8123/local/Roborock/roborock.json
  value_template: "{{ now() }}"
  json_attributes:
    - rooms
    - points

1 Like

This sounds like a more robust and flexible solution, I’ll sure try it out.