Questions re Handling Arrays (List of Lists) in YAML

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:

  1. How do I get the number of rows in my table? row_count: “{{ table | count }}” returns 85, not 3.
  2. Is there a better way (in HA YAML) to represent an array of data? (I mean method, not my code layout.)
  3. Is “repeat count” the best way to use for the looping in this scenario? If not, what?
  4. 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.

The simplest way to make it work is to just put your “table” in curly braces so that it is seen as a Jinja template:

    table: >-
      {{ [ [ '1a', '1b', '1c', '1d']
      , [ '2a', '2b', '2c', '2d']
      , [ '3a', '3b', '3c', '3d']
      ] }}

or remove the multi-line string indicator so it is seen as a json list:

    table: [ [ '1a', '1b', '1c', '1d'],[ '2a', '2b', '2c', '2d'], [ '3a', '3b', '3c', '3d'] ]

Here’s the results:

1 Like

Hi @Didgeridrew,
Can you please explain what that does and why it works?
Looks like templating the value somehow… Does that somehow change the “datatype”?
Would the ‘count’ function then return the number of rows correctly?

EDIT: Ahh, just seen your Edit on above post. That makes sense. So it’s the ‘>-’ that’s throwing things off? Must admit I’ve not got my head fully around how those modifiers work and their effects…

There’s a very helpful blog post from a long-time HA contributor that you may want to take a look at:

http://thomasloven.com/blog/2018/08/YAML-For-Nonprogrammers/

1 Like

Many thanks @Didgeridrew
As per your above post, I removed the ‘>-’ modifier on my declaration of the table and used:

    table:
      [
        ["1a", "1b", "1c", "1d"],
        ["2a", "2b", "2c", "2d"],
        ["3a", "3b", "3c", "3d"],
      ]

This seems to have resolved all the issues I was having.

2 Likes