Problem on rendering jinjia template

i have some jinjia template to calculate the average temperature in my house or the total of current consumed but … f**k i don’t remember the version of ha to start the problem…

By the way…

In the latest version (2023.3.5) i can’t get a working template…

this is my template:

{% set temperature_sensors = states.sensor
    | selectattr('entity_id', 'match', 'sensor.ptu_.*_temperature')
    | rejectattr('state', 'equalto', 'undefined')
    | rejectattr('state', 'equalto', 'unavailable')
    | list %}
{% set temperatures = [] %}
{% for sensor_temp in temperature_sensors %}
  {% set temperatures = temperatures + [sensor_temp.state | float] %}
{% endfor %}
{% if temperatures %}
  {{ (temperatures | sum / temperatures | length) | round(1) }}
{% else %}
  unavailable
{% endif %}

but the output is “unavailable”…

if i make some debug string:

{% set temperature_sensors = states.sensor
    | selectattr('entity_id', 'match', 'sensor.ptu_.*_temperature')
    | rejectattr('state', 'equalto', 'undefined')
    | rejectattr('state', 'equalto', 'unavailable')
    | list %}
{% set temperatures = [] %}
{% for sensor_temp in temperature_sensors %}
  {% set temperatures = temperatures + [sensor_temp.state | float] %}
{{ sensor_temp.entity_id }}
  sensor_temp.state: {{ sensor_temp.state }}
  temperatures: {{ temperatures }}
  temperatures length: {{ temperatures | length }}
{% endfor %}
{% if temperatures %}
  {{ (temperatures | sum / temperatures | length) | round(1) }}
{% else %}
  unavailable
{% endif %}

i’ve got this output:

sensor.ptu_camera_da_letto_temperature
sensor_temp.state: 19.6
temperatures: [19.6]
temperatures length: 1

sensor.ptu_cameretta_temperature
sensor_temp.state: 18.9
temperatures: [18.9]
temperatures length: 1

sensor.ptu_cucina_temperature
sensor_temp.state: 17.7
temperatures: [17.7]
temperatures length: 1

sensor.ptu_lavanderia_temperature
sensor_temp.state: 17.2
temperatures: [17.2]
temperatures length: 1

sensor.ptu_soggiorno_temperature
sensor_temp.state: 20.4
temperatures: [20.4]
temperatures length: 1

unavailable

it seems that the “temperatures” variable is initialized on every “for” loop

I also tried with chatGpt but I couldn’t solve it, has anyone had (or has) the same problem?

thanks in advice

Antonio

{% set temperature_sensors = states.sensor
    | selectattr('entity_id', 'match', 'sensor.ptu_.*_temperature')
    | rejectattr('state', 'equalto', 'undefined')
    | rejectattr('state', 'equalto', 'unavailable')
    | list %}
{% set ns = namespace(temperatures = []) %}
{% for sensor_temp in temperature_sensors %}
  {% set ns.temperatures = ns.temperatures + [sensor_temp.state | float(0)] %}
{% endfor %}
{% if ns.temperatures %}
  {{ ns.temperatures | average | round(1) }}
{% else %}
  unavailable
{% endif %}
1 Like

my fault, i have not saw the change on the template…

now for all the entities we need to work with {% set ns = namespace(varName = []) %}?

thanks

The scope of a variable inside a for-loop is limited to the for-loop.

In other words, if you change a variable’s value inside a for-loop, that new value only exists inside the for-loop. The use of namespace allows you to expand a variable’s scope beyond the borders of the for-loop.

aaaaaa ok ok thanks!!!