Testing Float Variable Value For Unavailable, Unknown, Blank, etc

Hi all! I’m guessing this is my lack of programming skills, but I haven’t found a solution through reading yet, so am asking.

I have a template sensor which checks two temperature sensors, and reports “Cooler”, “Warmer”, and “Nearly Equal”. Because the sensors are sometimes unavailable, I’d like to also include a “Unavailable” choice as well. I think I’ve prevented the float filter from trying to convert an unavailable value, but I’m having trouble figuring out how to determine how to test that variable for a Non-Number. Perhaps I should be making this test in a different way, but I’m not sure how to proceed. My code is below, and hopefully it is formatted in a readable way, as I haven’t posted code before.
Hopefully the comments will show what I’ve done, and what I’ve unsuccessfully tried.

  - sensor:
      - name: "Outdoor Temp Difference"
        # unit_of_measurement: "°F" #created problems, maybe not needed?
        # Set outdoor, indoor, and difference values
        # conditions for selections
        state: >
          {% set outdoor = states('sensor.tempo_disc_thd_48fb_temperature') %} {{ outdoor | float if is_number (outdoor) }} #catch unavailable sensor value
          {% set indoor = states('sensor.tempo_disc_thpd_3266_temperature') %} {{ indoor | float if is_number (indoor) }}
          {% set difference = 3 %}
          # check to see if either indoor or outdoor has no value, currently not working
          {% if outdoor = unknown or indoor = unknown %}
            Unavailable
          {% elif outdoor is lt (indoor - difference) %}
            Cooler
          {% elif outdoor is gt (indoor + difference) %}
            Warmer          
          {% else %}
            Nearly Equal
          {%endif %}

I’m not sure if outdoor and indoor are still floats if there is no sensor value, whether they don’t actually exist, or what they contain if they are floats, and how I can test that there is nothing there. So far everything I’ve tried results in: “type error”, “Syntax error”, or “not allowed for String, Int, or Float.” Any help very much appreciated. Thanks much! Also, sorry about the premature posting before code was added. Am having a devil of a time with the web form interface. Using a screenreader is sometimes very problematic with these. Thanks for patience with a newbie!

Your code is written in secret ink :wink:

1 Like

Sorry about that. Somehow it got submitted as I was trying to paste the code! This web interface doesn’t to be very screenreader friendly, or I just haven’t figured it out yet. Thanks much.

  - sensor:
      - name: "Outdoor Temp Difference"
        state: >
          {% set outdoor = states('sensor.tempo_disc_thd_48fb_temperature') | float(0) %}
          {% set indoor = states('sensor.tempo_disc_thpd_3266_temperature') | float(0) %}
          {% set difference = 3 %}
          {% if outdoor < (indoor - difference) %}
            Cooler
          {% elif outdoor > (indoor + difference) %}
            Warmer          
          {% else %}
            Nearly Equal
          {% endif %}
        availability: >
          {{ has_value('sensor.tempo_disc_thd_48fb_temperature') and 
            has_value('sensor.tempo_disc_thpd_3266_temperature') }}
1 Like

Thanks @123 . So, a couple questions.

How does this avoid passing unavailable sensor values to Float? It doesn’t generate errors in the Template editor, but would that continue to show up in the logs as it does now?

And, The availability shows true or false, but how do I get “Unavailable” to show as one of the selector choices? (I think that’s a selector, but I may have the name wrong.) As it is, it appears to leave the selector where it was, but post the availability value separately.

Have you created a new entity of “availability” in this sensor now?

Sorry, just not fully understanding yet, but sure appreciate the learning opportunity. Thanks for your patience.

Here’s the documentation for the availability option.

It’s the standard way to ensure the state option’s template is evaluated only if the availability template reports true. If it reports false, the state option’s template is not evaluated (it’s unavailable).

1 Like

Thanks much. I’m guessing the “conditions” section doesn’t actually execute until after the “availability” section? My programming skills don’t quite help me understand why that condition code doesn’t try to execute first since it comes first in the code, but obviously it works. Thanks again.

The position of the state and availability options in the sensor’s configuration doesn’t control the order that they are evaluated.

If the availability option is present in the sensor’s configuration, its template is evaluated before the state option’s template.

1 Like