Issue with templated arrays and for_each

Hi everyone,

I cannot figure out what is wrong. I use similar structures in some automations but cannot get it to work in a script:

script_test:
  sequence:
    - repeat:
        for_each:
          - camera.blueiris_rox
          - camera.blueiris_rookie
          - camera.blueiris_tic
        sequence:
          - variables:
              test_array: >
                { 'camera.blueiris_rox':    ['South terrace', 'test1' ],
                  'camera.blueiris_rookie': ['Garage',        'test2' ],
                  'camera.blueiris_tic':    ['Back garden',   'test3' ] }
          - service: notify.info
            data_template:
              title: "{{ repeat.item }}"
              message: >
                {{ test_array[repeat.item][0] + ' ' + test_array[repeat.item][1] }}

I expect to get notifications with subjects being the cameras names and content being the area and then test1, test2, etc.

Instead, I get the error:

Error rendering data template: UndefinedError: 'str object' has no attribute 'camera.blueiris_rox'

I don’t get it. It seems test_array is considered a string?

@petro @123 any clue ?

You’re missing the {{ }} around your template for test_array.

1 Like

He can also remove the > because yaml knows json. Either way, I’d make it all yaml

          - variables:
              test_array:
                camera.blueiris_rox:
                - South terrace
                - test1
                camera.blueiris_rookie:
                - Garage
                - test2
                camera.blueiris_tic:
                - Back garden
                - test3

EDIT: I’d also move the declaration outside the repeat loop.

1 Like

Another way to do the same thing:

script_test:
  sequence:
    - repeat:
        for_each:
          - who: camera.blueiris_rox
            where: South terrace
            what: test1
          - who: camera.blueiris_rookie
            where: Garage
            what: test2
          - who: camera.blueiris_tic
            where: Back garden
            what: test3
        sequence:
          - service: notify.info
            data:
              title: "{{ repeat.item.who }}"
              message: "{{ repeat.item.where }} {{ repeat.item.what }}"
1 Like

Always invoke the gurus.
You made my day guys.
After testing, I can report all your 3 solutions work:

#-----------------------------------------------------------------------------------------------------------------------------------------
script_test:
  sequence:
    - variables:
        test_array:
          camera.blueiris_rox:
            - South terrace
            - test1
          camera.blueiris_rookie:
            - Garage
            - test2
          camera.blueiris_tic:
            - Back garden
            - test3
    - repeat:
        for_each:
          - camera.blueiris_rox
          - camera.blueiris_rookie
          - camera.blueiris_tic
        sequence:
          - service: notify.info
            data_template:
              title: "{{ repeat.item }}"
              message: "{{ test_array[repeat.item][0] + ' ' + test_array[repeat.item][1] }}"
#-----------------------------------------------------------------------------------------------------------------------------------------
script_test2:
  sequence:
    - repeat:
        for_each:
          - who: camera.blueiris_rox
            where: South terrace
            what: test1
          - who: camera.blueiris_rookie
            where: Garage
            what: test2
          - who: camera.blueiris_tic
            where: Back garden
            what: test3
        sequence:
          - service: notify.info
            data:
              title: "{{ repeat.item.who }}"
              message: "{{ repeat.item.where }} {{ repeat.item.what }}"
#-----------------------------------------------------------------------------------------------------------------------------------------
script_test3:
  sequence:
    - repeat:
        for_each:
          - camera.blueiris_rox
          - camera.blueiris_rookie
          - camera.blueiris_tic
        sequence:
          - variables:
              test_array: >
                {{  { 'camera.blueiris_rox':  ['South terrace', 'test1' ],
                    'camera.blueiris_rookie': ['Garage',        'test2' ],
                    'camera.blueiris_tic':    ['Back garden',   'test3' ] } }}
          - service: notify.info
            data_template:
              title: "{{ repeat.item }}"
              message: "{{ test_array[repeat.item][0] + ' ' + test_array[repeat.item][1] }}"