I feel like I should find a simple answer to this, but I am at a loss after several hours of trying to get this to work. What I want to accomplish is simply this: Calculate the average value over two or more entities while catching unavailable
states using a default value or similar. Below is a snippet with two approaches to the templating.
- platform: template
sensors:
temperature_average_upstairs:
friendly_name: "Average Temperature Upstairs"
unit_of_measurement: "°C"
value_template: >-
{{ ( ( ( states('sensor.temperature_average_bedroom') | default(20) | float ) + ( states('sensor.temperature_average_balcony') | default(20) | float ) ) / 2 ) | round(1) }}
The thing is the snippet works when no entities are unavailable
. But as a matter of fact some entities are unavailable
for quite some time. So HA warns me, that this template will fail in the future and also the resulting sensor entity stays unavailable
until all entities in the template become available
.
HA Warning Message
2021-10-26 21:40:26 WARNING (MainThread) [homeassistant.helpers.template] Template warning: 'float' got invalid input 'unknown' when rendering template '{{ ( ( ( states('sensor.temperature_average_bedroom') | default(20) | float ) + ( states('sensor.temperature_average_balcony') | default(20) | float ) ) / 2 ) | round(1) }}' but no default was specified. Currently 'float' will return '0', however this template will fail to render in Home Assistant core 2021.12
I hope I could make myself clear, cause it is late and my head is spinning. Will clarify if questions arise.
Thanks in advance for any input!