Hi! Since one of the latest updates templates can now render lists. However the list workflow seems to be pretty limited. Here’s the problem I faced:
I’m trying to create a sensor with a list of links, parsed via IMTP integration. What I’ve got:
{% set body = state_attr('sensor.avito_clothes_mail', 'body') %}
{% set links = [] %}
{% for i in range(10) %}
{{ links.append(body | regex_findall_index('https://www.avito.ru.+?(?=")', index = i)) }}
{% endfor %}
{{ links }}
But instead of the wanted list I receive an error: SecurityError: access to attribute 'append' of 'list' object is unsafe.
So here’s my question — how do I create lists using templates, when I’m not allowed to append items to it?
You can append items to a list by adding two lists, like this:
{% set data = namespace(numbers=[]) %}
{% for i in range(10) %}
{% set data.numbers = data.numbers + [i] %}
{% endfor %}
{{ data.numbers }}
For your case, this should work (I cant test it and I am not sure about the “regex_findall_index” stuff):
{% set body = state_attr('sensor.avito_clothes_mail', 'body') %}
{% set data = namespace(links=[]) %}
{% for i in range(10) %}
{% set data.links = data.links + [(body | regex_findall_index('https://www.avito.ru.+?(?=")', index = i))] %}
{% endfor %}
{{ data.links }}
I’ve been trying to manipulate a list using that approach, but cannot get it to work. It must be a scoping issue, but apart from the condition, I cannot see my code being any different from the example above.
{% set list = ['A', 'B', 'C', 'D', 'E'] %}
{% set prefixed_list = [] %}
{% for item in list %}
{% if item == "C" %}
{{ item }}
{% set prefixed_list = prefixed_list + [item] %}
{{ prefixed_list }}
{% else %}
{{ item }}
{% set prefixed_list = prefixed_list + ['the ' + item] %}
{{ prefixed_list }}
{% endif %}
{% endfor %}
{% set list = prefixed_list %}
{{ list }}
This returns:
A
['the A']
B
['the B']
C
['C']
D
['the D']
E
['the E']
[]
I’m struggling to understand, perhaps because I am so new to templating…
What I’m trying to do is average a list of numbers together, but there are times when they are “unavailable”.
How do I create a list and add the resulting value only if it is a number and then ultimately average those? Sometimes it will be 3 values getting averaged, sometimes 2 and rarely 1.
Is this possible?
This is the base of what I have come up with thus far that works when all three are available, but as soon as one is unavailable it breaks for obvious reasons.
{% if is_number(states('sensor.first_oil')) %}
{% set first = states('sensor.first_oil')|float %}
{% elif set %}
{% endif %}
{% if is_number(states('sensor.second_oil')) %}
{% set second = states('sensor.second_oil')|float %}
{% endif %}
{% if is_number(states('sensor.third_oil')) %}
{% set third = states('sensor.third_oil')|float %}
{% endif %}
{{average(first,second,third)}}
That’s so simple too, I wish I came here before spending like 6 hours trying to make this work LOL.
I love to learn so I want to fool around with this to see how it works as it’s so much simpler.
Thank you!!