I’ve been experimenting in automations/scripts on using arrays (list of lists / etc.) and have some questions that I’ve not resolved / found documented or suitable community Topic answers on. Can people enlighten/advise me please??
Example script:
s_test_list:
alias: "Test Array handling"
mode: single
#
variables:
# Table as List of Lists
table: >-
[ [ '1a', '1b', '1c', '1d']
, [ '2a', '2b', '2c', '2d']
, [ '3a', '3b', '3c', '3d']
]
row_count: "{{ table | count }}"
#
sequence:
- action: input_text.set_value
target:
entity_id: input_text.test_message
data:
value: "row_count={{ row_count }}"
# Loop over the rows
- repeat:
count: "{{ row_count }}"
sequence:
- variables:
row: "{{ table[repeat.index - 1] }}"
- action: input_text.set_value
target:
entity_id: input_text.test_message
data:
value: "Row Num={{ repeat.index - 1 }}, Values={{ row }}"
My questions:
- How do I get the number of rows in my table? row_count: “{{ table | count }}” returns 85, not 3.
- Is there a better way (in HA YAML) to represent an array of data? (I mean method, not my code layout.)
- Is “repeat count” the best way to use for the looping in this scenario? If not, what?
- Why is my “Values={{ row }}” (the output at end of script) not giving me the Row’s values? The output is “Test message changed to Row Num=1, Values= triggered by action Script: Test Array handling”. Is this just truncation of the message? I’m sure I’ve seen table item output in my previous scripts. Is there a “truncate output” setting somewhere? The entity’s length is 255 chars.
Thank you.
EDIT: Astonishingly, the loop beyond the table’s range does NOT seem to result in an HA or runtime exception, which seems very dangerous.