Comparing a variable number between 2 values in a template condition

I’ve been working on a “Feels Like” sensor based on outdoor temperature, humidity, and windspeed from 3 mqtt sensors created from sdr-rtl input. I thought I had it all working then it died. I’ve been playing with the code in the dev tools templating and I can get pieces to work but when I put it all together it dies.
When T = 80, RH = 94, and windspeed = 1 and the code is commented out (below) feels_like = 81 which is good.

    {% set T = (states('sensor.breeze_pro_temp') | float) %}
{{ T }}
      {% set RH = (states('sensor.breeze_pro_humidity') | float) %}
{{ RH }}
      {% set windspeed = (states('sensor.breeze_pro_wind_speed')|float) %}
{{ windspeed }}
     {% if (80.0 >= T <= 112.0) and (RH < 13) %}
        {% set feels_like = (-42.379 + 2.04901523 * T + 10.14333127 * RH - 0.22475541 * T * RH - 0.00683783 * T * T - 0.05481717 * RH * RH + 0.00122874 * T * T * RH + 0.00085282 * T * RH * RH - 0.00000199 * T * T * RH * RH ) | round (1) %}
      {% elif (80.0 >= T <= 87.0) and (RH > 85) %} 
        {% set feels_like = (-42.379 + 2.04901523 * T + 10.14333127 * RH - 0.22475541 * T * RH - 0.00683783 * T * T - 0.05481717 * RH * RH + 0.00122874 * T * T * RH + 0.00085282 * T * RH * RH - 0.00000199 * T * T * RH * RH ) |round (1) %}
        {% set A1 = ((13-RH)/4) * (((17-( (T-95) |abs ))/17)**0.5) %}
        {% set feels_like = (feels_like - A1) |round(2) %}
{#      {% elif (50.0 > T < 80.0) %}
        {% set feels_like = (-42.379 + (2.04901523 * T) + (10.14333127 * RH) - ((0.22475541 * T) * RH) - ((0.00683783 * T )* T) - (((0.05481717 * RH) * RH) + (((0.00122874 * T) * T) * RH) + ((0.00085282 * T) * RH) * RH) - (((( 0.00000199 * T) * T) * RH) * RH) ) |round (1) %}
        {% set A2 = ((RH-85)/10) * ((87-T)/5) %}
        {% set feels_like = (feels_like + A2) |round(1) %}
      {% elif (T <= 50.0) and (windspeed >0) %}
        {% set feels_like = ((0.6215 * T) -35.75*(windspeed**0.16) + ((0.4275*T)*(windspeed**0.16))+35.74)| round(1)  %} #}
      {% else %}
        {% set feels_like = T| round(1)  %} 
      {% endif %} 
      {{ feels_like }} 
    unit_of_measurement: "°F"

But if I change T = 79, feels_like = 88.21 when it should equal 79 since there should be no other adjustments at that temperature regardless of the humidity measurement. It looks like the comparison if statements are not working as expected. If I change the state T = 45, then the template blows up (below)

ValueError: Template error: round got invalid input '(93.1+27.865221840769436j)' when rendering template '{% set T = (states('sensor.breeze_pro_temp') | float) %}
{{ T }}
      {% set RH = (states('sensor.breeze_pro_humidity') | float) %}
{{ RH }}
      {% set windspeed = (states('sensor.breeze_pro_wind_speed')|float) %}
{{ windspeed }}
     {% if (80.0 >= T <= 112.0) and (RH < 13) %}
        {% set feels_like = (-42.379 + 2.04901523 * T + 10.14333127 * RH - 0.22475541 * T * RH - 0.00683783 * T * T - 0.05481717 * RH * RH + 0.00122874 * T * T * RH + 0.00085282 * T * RH * RH - 0.00000199 * T * T * RH * RH ) | round (1) %}
      {% elif (80.0 >= T <= 87.0) and (RH > 85) %} 
        {% set feels_like = (-42.379 + 2.04901523 * T + 10.14333127 * RH - 0.22475541 * T * RH - 0.00683783 * T * T - 0.05481717 * RH * RH + 0.00122874 * T * T * RH + 0.00085282 * T * RH * RH - 0.00000199 * T * T * RH * RH ) |round (1) %}
        {% set A1 = ((13-RH)/4) * (((17-( (T-95) |abs ))/17)**0.5) %}
        {% set feels_like = (feels_like - A1) |round(2) %}
{#      {% elif (50.0 > T < 80.0) %}
        {% set feels_like = (-42.379 + (2.04901523 * T) + (10.14333127 * RH) - ((0.22475541 * T) * RH) - ((0.00683783 * T )* T) - (((0.05481717 * RH) * RH) + (((0.00122874 * T) * T) * RH) + ((0.00085282 * T) * RH) * RH) - (((( 0.00000199 * T) * T) * RH) * RH) ) |round (1) %}
        {% set A2 = ((RH-85)/10) * ((87-T)/5) %}
        {% set feels_like = (feels_like + A2) |round(1) %}
      {% elif (T <= 50.0) and (windspeed >0) %}
        {% set feels_like = ((0.6215 * T) -35.75*(windspeed**0.16) + ((0.4275*T)*(windspeed**0.16))+35.74)| round(1)  %} #}
      {% else %}
        {% set feels_like = T| round(1)  %} 
      {% endif %} 
      {{ feels_like }} 
    unit_of_measurement: "°F"' but no default was specified

I’ve searched for the proper syntax for comparison since that’s where I’m guessing where the problem lies and cannot find an answer. I put the {# … *} comments in the code to try to isolate the sections that may be causing a problem. It seems like there is no other way to add comments to individual lines in the template editor. I’ve been staring at this for a few days and there’s probably something I’m overlooking. I hope someone can help.

This means the value of T must be within the range of 80 to 120, inclusively.

80.0 <= T <= 112.0

Here is what you are using and it means something altogether different. It means the value of T must be less than or equal to 80 and less than or equal to 112. In effect, the value must be less than or equal to 80.

80.0 >= T <= 112.0

That’s what happens when you stare at the obvious too long! Thank you!!

outside: 40.1, RH = 89%, Feels Like 41.9, Speed 1.05. Just need to double check the formulas.

1 Like