It’s worse than that, the template will not be loaded if it happened during start up. Define your defaults and use an availability template if you want to catch unknown child sensor states.
It does not matter (well don’t use division by 0). As long as the default value exists the template will still load if one of the entities is missing.
The availability template will catch this and set the state to unknown.
One way to do this:
availability: "{{ states('sensor.temperature')|float(0) != 0 and states('sensor.humidity)|float(0) != 0 }}"
The problem with this method is that the sensor states could actually be 0. So put something else as the default, like this:
availability: "{{ states('sensor.temperature')|float('foo') != 'foo' and states('sensor.humidity)|float('foo') != 'foo' }}"
or this:
availability: "{{ states('sensor.temperature')|float('bar') != 'bar' and states('sensor.humidity)|float('bar') != 'bar' }}"
or this:
availability: "{{ states('sensor.temperature')|float(none) != none and states('sensor.humidity)|float(none) != none }}"
Another option is checking the states directly:
availability: "{{ states('sensor.temperature') not in [unavailable, unknown, none] and states('sensor.humidity) not in [unavailable, unknown, none] }}"
You only need to supply an availability template if you want to protect against these templates returning incorrect values when the sensors in the templates are unknown or unavailable.
Yes. I think there is work being done on a PR that prevents the state template being evaluated if the availability template returns false but I’m not sure if it has been merged yet.
And if I just copy the value to a template it will also copy unknown and therefore not fail.
state: "{{ state_attr('sun.sun', 'elevation') }}"
I think now I have it.
But thinking about condition templates. These will just return false if a sensor is not available, right? Because I do make calculations in these templates as well.