where aaaa, bbbb, cccc, dddd - some strings (attributes of some objects) which are repeated several times.
Number of repetitions - depends on these objects (varies 1…3).
First I decided to create that [...] string without commas:
[ aaaa aaaa bbbb cccc cccc cccc dddd dddd ]
Here is a simplified code:
{% set COMMAND = "[" %}
{% for state in states.switch -%}
{%- set PARAM = "aaaa" -%}
{%- set COUNT = 2 -%}
{%- set PARAM_STRING = (PARAM + " ") * COUNT -%}
{%- set COMMAND = COMMAND + PARAM_STRING -%}
COMMAND = {{ COMMAND + "\n"}}
{%- endfor %}
{% set COMMAND = COMMAND + "]" %}
COMMAND = {{ COMMAND + "\n"}}
What is supposed to happen here:
The COMMAND string is initialized by “[”.
For each object (let it be every switch entity):
the “aaaa” string is repeated COUNT times (with a whitespace);
then this “aaaa aaaa ” string is added to the COMMAND.
Finally, the assembled COMMAND string is concatenated with “]”.
I am trying to generate a command for the vacuum.
Example: [2,2,3,3,3,16,16,17]
Each number is a “room number”. Number of repetitions = number of passes.
It’s the for-loop that goofs it up. Each iteration creates a list but then handles it like a string and concatenates it to the result of the previous iteration.
You have to handle each iteration’s result as a list and append it to the previous list. Initialize COMMAND to [] which is an empty list. Replace the ~ operator with + in the line that combines the current result to COMMAND.
{% set ns = namespace(COMMAND = []) -%}
{%- set ROOM_FLAGS = expand('group.vacuum_clean_rooms') -%}
{%- for flag in ROOM_FLAGS -%}
{%- set ROOM_NUMBER_STRING = ("2" * 3) | map('int', 0) | list -%}
{%- set ns.COMMAND = ns.COMMAND + ROOM_NUMBER_STRING -%}
{%- endfor -%}
{{ ns.COMMAND }}
Another option is:
{% set ns = namespace(COMMAND = "") -%}
{%- set ROOM_FLAGS = expand('group.vacuum_clean_rooms') -%}
{%- for flag in ROOM_FLAGS -%}
{%- set ROOM_NUMBER_STRING = ("2" * 3) -%}
{%- set ns.COMMAND = ns.COMMAND + ROOM_NUMBER_STRING -%}
{%- endfor -%}
{%- set LIST = ns.COMMAND | map('int', 0) | list -%}
{{ LIST }}
But both the cases give a wrong list if some number is of two digits:
The list contains three consecutive instances of 2for each iteration of the for-loop.
I don’t know what your goal is but that result is correct as far as what the code is designed to do.
As for the issue with 21, a double-digit value wasn’t specified in your original requirement. In fact, it didn’t even use numeric values but an alphabetic string. I can help you fix the double-digit problem but I suspect you will reveal yet another requirement and the template will have to be re-engineered again.
The original trick I used doesn’t work with a multi-digit value. However, this does work for either single or multi-digit values. It requires a trailing space so that it serves as a delimiter for split.
FWIW, I seem to recall I have successfully passed a list as a script variable. I’ll have to review my notes to confirm that. Anyway, whether it did or not might have no bearing on how it behaves with the params option.
BTW, every time I have replied in this thread, the forum software kept reminding me that it was already marked as Solved so was I sure I want to keep posting in it?