Data Comparison

I have an AQI air quality sensor, I want to output textual information about the state of the air depending on the data from the sensor.
Here’s what I’ve already done:

                 {% set aqi =
                 states('sensor.u_s_air_quality_index') |
                 float %}
                 {% if aqi <= 50 %}
                 ({{states('sensor.u_s_air_quality_index')}})
                        Good
                 {% elif 51 >= aqi <= 100 %}
                 {{states('sensor.u_s_air_quality_index')}}
                        Moderate
                 {% elif 101 >= aqi <= 150 %}
                 {{states('sensor.u_s_air_quality_index')}}
                 	Harmful for sensitive groups
                 {% elif 151 >= aqi <= 200 %}
		 {{states('sensor.u_s_air_quality_index')}}
                        Unhealthy
                 {% elif 201 >= aqi <= 300 %}
                 {{states('sensor.u_s_air_quality_index')}}
                        Very unhealthy
                 {% elif aqi > 300 %}
		 	Dangerous
                 {% endif %}

At the moment, the sensor shows status 59, which in theory should correspond to Moderate, but for some reason my code displays Harmful for sensitive groups

What am I doing wrong?

Your comparison operators are set up incorrectly.

The in line comparison 51 >= aqi <= 100 is effectively the same as 51 >= aqi because there are no values between 51 and 100 that are less than or equal to 51. Following that logic, the first of your clauses that is true for the aqi value 59 is 101 >= aqi which corresponds to Harmful for sensitive groups.

There is also a lot of unnecessary repetition.

{% set aqi = states('sensor.u_s_air_quality_index') | float %}
{{ aqi }}
{% if aqi <= 50 %}
  Good
{% elif 51 <= aqi <= 100 %}
  Moderate
{% elif 101 <= aqi <= 150 %}
  Harmful for sensitive groups
{% elif 151 <= aqi <= 200 %}
  Unhealthy
{% elif 201 <= aqi <= 300 %}
  Very unhealthy
{% elif aqi > 300 %}
  Dangerous
{% endif %}

51 >= aqi <= 100
but judging by the instructions, the sign >= if the left side is greater than or equal to the right side, on the left side there is 51, and on the right side I have the idea of ​​the value of aqi, which is currently 59
51 <= aqi <= 100
and in your example <= if the left side is less than or equal to the right side

Or maybe I didn’t quite understand this instruction, what is being compared to what, can you explain a little more?

In-line comparison is the same as combining two comparisons with an AND operation.

I think it may be easier to see the difference between what the two version if we take each one and summarize it with the same frame of reference.

51 >= x <= 100

51 >= x AND x <= 100
x is less than or equal to 51 AND x is less than or equal to 100
  This will only be true for values less than or equal to 51
51 <= x <= 100

51 <= x AND x <= 100
x is greater than or equal to 51 AND x is less than or equal to 100
  This will be true for all values between 51 and 100
1 Like

Oh, how, I understood the description in the instructions completely differently, or rather, everything is the other way around as it is.
Thank you very much for the detailed explanation.