I am very new to scripting and templating. I would like to program a script that calls a service to activate the vacuum cleaner and clean a selection of rooms. For example, for 2 rooms in particular the script is as follows (thanks this post):
alias: Room Cleaning
sequence:
- service: xiaomi_miot.call_action
data:
entity_id: vacuum.dreame_mc1808_ef51_robot_cleaner
siid: 18
aiid: 1
params:
- piid: 1
value: 18
- piid: 21
value: "{\"selects\": [[7,1,3,3,1],[8,1,3,3,1]]}"
force_params: true
throw: true
mode: single
icon: mdi:robot-vacuum
where the first number of each vector [[7,1,3,3,1],[8,1,3,3,3,1]]
is the room to be cleaned (7 for kitchen, 8 for living room, e.g.). This script works without problems.
Then, using a series of input_boolean and a sensor template (thanks to this), I am able to autogenerate the complete string (e.g. [[7,1,3,3,3,1],[8,1,3,3,3,1]]
) with the necessary data depending on the chosen rooms:
template:
- sensor:
- name: rooms_to_clean
state: >
{% set x = expand('input_boolean.clean_kitchen', 'input_boolean.clean_living', 'input_boolean.clean_bedroom') | map(attribute='state') | list %}
{% set y = [[7,1,3,3,1], [8,1,3,3,1], [9,1,3,3,1]] %}
{% set rooms = namespace (z=[]) %}
{% for i in x %}
{% if i == 'on' %}
{% set rooms.z = rooms.z + [y[loop.index-1]] %}
{% endif %}
{% endfor %}
{{ rooms.z }}
The final script (and the one I have problems with) that I use is the following:
alias: Room Cleaning (var template)
sequence:
- service: xiaomi_miot.call_action
data:
entity_id: vacuum.dreame_mc1808_ef51_robot_cleaner
siid: 18
aiid: 1
params:
- piid: 1
value: 18
- piid: 21
value: "{\"selects\": {{ states('sensor.rooms_to_clean') }}}"
force_params: true
throw: true
mode: single
icon: mdi:robot-vacuum
Well, although through Developer tools/Template I check that this code is exactly the same as the first one without templates, I don’t know the reason why the service call returns error, I’m going quite crazy, what can I be doing wrong?
Thanks in advance!