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: 1sensor.ptu_cameretta_temperature
sensor_temp.state: 18.9
temperatures: [18.9]
temperatures length: 1sensor.ptu_cucina_temperature
sensor_temp.state: 17.7
temperatures: [17.7]
temperatures length: 1sensor.ptu_lavanderia_temperature
sensor_temp.state: 17.2
temperatures: [17.2]
temperatures length: 1sensor.ptu_soggiorno_temperature
sensor_temp.state: 20.4
temperatures: [20.4]
temperatures length: 1unavailable
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