When creating sensors it happens that a value is unavailable and the calculated sensor value is crap. I read that there is the availability_template but I looks like that this is only available in the legacy configuration format. What is the alternative for it in the recommended modern configuration format?
Right now I solve it the following way but I hope there is a smarter solution.
template:
- sensor:
- name: "COP heating last 24h"
unit_of_measurement:
state: >
{% if states('sensor.delta_heating_supplied_24h') in ['unavailable', 'unknown', 'none'] %}
{{ states('sensor.COP_heating_last_24h') }}
{% elif states('sensor.delta_heating_consumed_compressor_24h') in ['unavailable', 'unknown', 'none'] %}
{{ states('sensor.COP_heating_last_24h') }}
{% elif states('sensor.delta_heating_consumed_electrical_24h') in ['unavailable', 'unknown', 'none'] %}
{{ states('sensor.COP_heating_last_24h') }}
{% else %}
{% set delta_heating_supplied_24h = states('sensor.delta_heating_supplied_24h') | float %}
{% set delta_heating_consumed_compressor_24h = states('sensor.delta_heating_consumed_compressor_24h') | float %}
{% set delta_heating_consumed_electrical_24h = states('sensor.delta_heating_consumed_electrical_24h') | float %}
{{ (delta_heating_supplied_24h / (delta_heating_consumed_compressor_24h + delta_heating_consumed_electrical_24h)) | round(2, default=0) }}
{% endif %}