Home Assistant Wright to a Array element in a for loop

In a template sensor I want to write to a element of array in a for loop:

              {% set arrayname = [] %}
              {% for i in range ( 0 , 3) %}
                {% arrayname[i] = i %}
              {% endfor %}
              
              {{arrayname}}

What is wrong?

You have to use namespace(). Try this:

{% set data = namespace(arrayname=[]) %}
{% for i in range( 0, 3 ) %}
{% set data.arrayname = data.arrayname + [i] %}
{% endfor %}
{{ data.arrayname }}
1 Like