Float(0) and Automations in 2021.12

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:

  1. will – be ok after the else statement, what will that look like if the openweathermap sensors are unavailable?
  2. 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.

Add the availability option to the Template Sensor’s configuration containing a template that checks if both sensor values are not unavailable.

      availability: >
        {{ states('sensor.openweathermap_temperature') != 'unavailable' and
            states('sensor.openweathermap_humidity') != 'unavailable' }}

You still need to supply float with a default value but the availability template ensures the state template’s calculation is never performed when either sensor’s value is unavailable.

If you want it to be more thorough, use this:

      availability: >
        {{ states('sensor.openweathermap_temperature') not in ['unavailable', 'unknown'] and
            states('sensor.openweathermap_humidity') not in ['unavailable', 'unknown'] }}
2 Likes

Thanks very much for that.

I cannot find a reference to availability in the documentation, but have configured a test sensor as follows:

    - name: Outside Dew Point now test
      unit_of_measurement: "°C"
      availability: >
        {{ states('sensor.openweathermap_temperature') not in ['unavailable', 'unknown'] and
            states('sensor.openweathermap_humidity') not in ['unavailable', 'unknown'] }}      
      state: >
       {% set Toutside = states('sensor.openweathermap_temperature') | float(0) %}
       {% set Houtside = states('sensor.openweathermap_humidity') | float(0) %}
       {{ (((Houtside / 100)**(1 / 8)) * (112 + (0.9 * Toutside)) + 0.1 * Toutside - 112) | round(1) }}

It gives the correct result when the openweathermap sensors are available.

Did I add the availability option correctly please?

Thanks again.

It’s here: availability

Yes. All that was needed was to copy-paste the code I posted to the sensor’s configuration.

Please consider marking my post above with the Solution tag. It will automatically place a check-mark next to the topic’s title which signals to other users that this topic is resolved. This helps other users find answers to similar questions. For more information, refer to guideline 21 in the FAQ.

1 Like

I didn’t see your second question get answered, and it doesn’t look applicable with your new code, but figured an answer might still be appreciated. Also, for future reference, it helps if you specify the error that you are seeing when asking about why you’re getting an error.

Anyway, you’re getting an error because of incorrect formatting in the if statement; only one if is needed, even with multiple checks. Also, I’m not too familiar with the tilde operation, but it seemed like a weird use to me, so I replaced it with an and:

{% set Toutside = states('sensor.openweathermap_temperature') | float %}
{% set Houtside = states('sensor.openweathermap_humidity') | float %}
{% if is_number(Toutside) and is_number(Houtside) %}
  {{ (((Houtside / 100)**(1 / 8)) * (112 + (0.9 * Toutside)) + 0.1 * Toutside - 112) | round(1) }}
{% else %}
  --
{% endif %} 
1 Like

It’s used for concatenating strings and its usage in the original example is completely incorrect. It’s probably the root cause of the Template Editor’s complaint and not this:

The fact is, without the % symbol, the resulting Jinja2 brackets are definitely invalid and don’t form a proper statement.

2 Likes

Thanks for all the replies and for putting me straight on the various errors I’d made, your help is much appreciated.

2 Likes