I have sensors, templated as below, to take openweathermap data from the internet and to do some calculations with that data:
template:
- sensor:
- name: Outside Dew Point now
unit_of_measurement: "°C"
state: >
{% set Toutside = states('sensor.openweathermap_temperature') | float %}
{% set Houtside = states('sensor.openweathermap_humidity') | float %}
{{ (((Houtside / 100)**(1 / 8)) * (112 + (0.9 * Toutside)) + 0.1 * Toutside - 112) | round(1) }}
I can fix these with float(0) before 2021.12 is released. But that might cause problems if:
- the openweathermap sensors are unavailable,
- the default 0 gets used in the calculation and
- the result of that then triggers an automation.
I’ve tried to update my templates to prepare for undefined variables as follows:
template:
- sensor:
- name: Outside Dew Point now
unit_of_measurement: "°C"
state: >
{% set Toutside = states('sensor.openweathermap_temperature') | float %}
{% set Houtside = states('sensor.openweathermap_humidity') | float %}
{ if is_number(Toutside) ~ if is_number(Houtside) }
{{ (((Houtside / 100)**(1 / 8)) * (112 + (0.9 * Toutside)) + 0.1 * Toutside - 112) | round(1) }}
{ else }
--
{ endif }
But I’m new to Home Assistant and to writing configuration, so please can anyone tell me if this is the correct way to fix my templates?
Supplementary questions are:
- will – be ok after the else statement, what will that look like if the openweathermap sensors are unavailable?
- why does the Template Editor in Developer Tools give me errors when I put % at the start and end of the if, else and endif statements?
Many thanks for any help.