I come from a software engineering background but for whatever reason am really struggling with basic concepts in HA. I want to script the cleaning of single or multiple areas. To do this, I have to convert from a HA area, to a pre-defined set of Roborock segment IDs.
My problems are for this script field:
area:
selector:
area:
multiple: true
How can I loop through chosen values and convert the area to a number based on a lookup object: {“rooms”: {“kitchen”: 20, “dining”: 24}}.
Secondly, how can I use the multiple values from performing the lookup as parameters for the segments argument in the app_segment_clean command?
action: vacuum.send_command
data:
command: app_segment_clean
params:
- segments:
- "{{ convertedArea1 }}"
- "{{ convertedArea2 }}"
- ...
repeat: 1
target:
entity_id: vacuum.roborock_qrevo_master
That was a bit of fun but I think I now have a better way forwards for future projects. I got sick of fighting with Jinja and list/string types in YAML so opted to do the conversion activities in Python.
This script allows selection of areas from HA and passes them off to the Python script. I call this with automations and with buttons:
alias: Roborock – clean selected areas
mode: restart
fields:
area:
name: Areas to clean
selector:
area:
multiple: true
sequence:
- data:
area: "{{ area }}"
action: python_script.roborock_segment_clean
The Python script itself takes the list of areas, looks up the relevant Roborock room segments from a static list, and then uses haas to call the app_segment_clean roborock command without all of the crazy YAML issues stepping in the way:
ROOMS = {
"living_room": 19,
"dining_room": 22,
}
area = data.get("area")
if isinstance(area, str):
areas = [area]
elif area is None:
areas = []
else:
areas = list(area)
segments = [ROOMS[a] for a in areas if a in ROOMS]
logger.warning("roborock_segment_clean: area=%r areas=%r segments=%r", area, areas, segments)
if segments:
hass.services.call(
"vacuum",
"send_command",
{
"entity_id": "vacuum.roborock_qrevo_master",
"command": "app_segment_clean",
"params": [
{
"segments": segments,
"repeat": 1,
}
],
},
False,
)
else:
logger.warning("roborock_segment_clean: no matching segments, nothing sent")
alias: Roborock – clean selected areas
mode: restart
fields:
area:
name: Areas to clean
selector:
area:
multiple: true
sequence:
- variables:
rooms:
living_room: 19
dining_room: 22
segments: |
{% set ns = namespace(segs=[]) %}
{% for a in v_areas if a in rooms %}
{% set ns.segs =ns.segs + [rooms[a]]%}
{% endfor %}
{{ns.segs}}
- action: vacuum.send_command
data:
command: app_segment_clean
params: |
{{ [{"segments": segments, "repeat":1}] }}
target:
entity_id: vacuum.roborock_qrevo_master