Help please, sensor air quality - based on co2 and tvoc values

Hi,
Can someone help me with a sensor that gives a status for the air quality based on the CO2 and TVOC values?

For example:

  • When Co2 gives the value between 400 and 900
    AND the TVOC is between 0 and 65
    the status should be “Very good”

  • When Co2 gives the value between 900 and 1200
    AND the TVOC is between 65 and 220
    the status should be “Good”

if the sensor could also give a color that would be great!!!

thank you very much in advance!

I have something like this but it not working:

  - platform: template
    sensors:
      air_quality:
        friendly_name: "Air quality"
        value_template: >-

		{% set airqualityco2 = states('sensor.toon_luchtkwaliteit_co2_koolstofdioxide') | float %}
		{% set airqualitytvoc = states('sensor.toon_luchtkwaliteit_tvoc_vluchtige_organische_stoffen') | float %}

        {% if 400 < airqualityco2 < 900 %} and {% if 0 < airqualitytvoc < 65 %}
          VeryGood
		{% elif 900 > airqualityco2 < 1200 %} and {% if 65 < airqualitytvoc < 220 %}
          Good
		{% else %}
          Off
		{% endif %}

Your template isn’t indented from the field

Red should have 2 more spaces in front of each line

secondly, your template is incorrect.

		{% set co2 = states('sensor.toon_luchtkwaliteit_co2_koolstofdioxide') | float %}
		{% set tvoc = states('sensor.toon_luchtkwaliteit_tvoc_vluchtige_organische_stoffen') | float %}

        {% if 400 < co2 < 900  and 0 < tvoc < 65 %}
          VeryGood
		{% elif 900 < co2 < 1200 and 65 < tvoc < 220 %}
          Good
		{% else %}
          Off
		{% endif %}

thanks for the quick reply! hmm thats looks more logical. I now have this code but it still falls over an error in the ‘set’ line

sensor:
  - platform: template
    sensors:
      air_quality:
        friendly_name: "Air quality"
        value_template: >-
		  {% set co2 = states('sensor.toon_luchtkwaliteit_co2_koolstofdioxide') | float %}
		  {% set tvoc = states('sensor.toon_luchtkwaliteit_tvoc_vluchtige_organische_stoffen') | float %}
          {% if 400 < co2 < 900 and 0 < tvoc < 65 %}
            VeryGood
		  {% elif 900 < co2 < 1200 and 65 < tvoc < 220 %}
            Good
		  {% else %}
            Off
		  {% endif %}

error:

Error loading /config/configuration.yaml: while scanning for the next token
found character '\t' that cannot start any token
in "/config/packages/test.yaml", line 7, column 1

You’re mixing tabs and spaces in configuration.yaml, you can’t do that. Use either tabs or use spaces. It’s recommended to only use spaces.

I just looked at what you pasted, all the t’s represent where you have tabs that should be spaces

sensor:
  - platform: template
    sensors:
      air_quality:
        friendly_name: "Air quality"
        value_template: >-
tttttttt  {% set co2 = states('sensor.toon_luchtkwaliteit_co2_koolstofdioxide') | float %}
tttttttt  {% set tvoc = states('sensor.toon_luchtkwaliteit_tvoc_vluchtige_organische_stoffen') | float %}
          {% if 400 < co2 < 900 and 0 < tvoc < 65 %}
            VeryGood
tttttttt  {% elif 900 < co2 < 1200 and 65 < tvoc < 220 %}
            Good
tttttttt  {% else %}
            Off
tttttttt  {% endif %}

seriously? I didnt know that :smiley:
It looks like the template is valid now, I will test it further this weekend, thanks and have a good weekend!

All whitespace based files typically only want spaces. Some allow tabs or spaces. It’s always safest to just use spaces.

1 Like

I just tested (couldn’t wait for the weekend :smiley: ). the status seems wrong. the values are currently

  • co2 is 1519
  • tvoc is 193
    but it gives the status “Bad”
sensor:
  - platform: template
    sensors:
      air_quality:
        friendly_name: "AirQuality"
        value_template: >-
          {% set co2 = states('sensor.toon_luchtkwaliteit_co2_koolstofdioxide') | float %}
          {% set tvoc = states('sensor.toon_luchtkwaliteit_tvoc_vluchtige_organische_stoffen') | float %}
          {% if 400 > co2 < 900 and 0 > tvoc < 65 %}
            VeryGood
          {% elif 900 > co2 < 1200 and 65 > tvoc < 220 %}
            Good
          {% elif 1200 > co2 < 2000 and 220 > tvoc < 660 %}
            Fair
          {% elif 2000 > co2 < 5000 and 660 > tvoc < 2200 %}
            Bad
          {% elif 5000 > co2 < 10000 and 2200 > tvoc < 5000 %}
            Warning
          {% else %}
            Off
          {% endif %}

the co2 actually falls into “fair” but TVOC “good”.
if they both can’t get in between, he should take the ELSE right? Or am I missing something here?

value1 < what_you're_checking < value2

You broke all the syntax by flipping the <

the value of tvoc (for status Warning) should be between 2200 and 5000. so a value greater than 2200, don’t I need the “greater than” sign in that case? and at 5000 it must be a value under 5000. then I assume I need to use a “less than” sign
or am I thinking wrong?

So I need to change everything to <
right?
But that doesn’t make sense to me to be honest

Yes

How does it not?

if you want to check a value being greater than 40…

40 < value

if you want to check a value less than 100…

value < 100

if you want to check if the value is greater than 40 but less than 100…

40 < value < 100
1 Like

after I read it again I understand
thanks for your explanation and effort. it now works the way I wanted it to.