Climate template help required

I have built an outdoor air quality monitor using an SDS011 and ESPhome. The sensor works great but I would like to show the particulate index for each of the 2 PM readings. I have copied/pasted and tweaked the following template but whenever I go above the 1st level on either it doesn’t display anything. It looks correct to my eyes. This is in my sensor.yaml file.

- platform: template
  sensors:
      air_quality_pm10:
        friendly_name: 'Air Quality PM 10'
        value_template: >-
          {% if states('sensor.particulate_matter_10_0um_concentration') | float <= 16 %}Good
          {% elif states('sensor.particulate_matter_10_0um_concentration') | float <= 17 | float > 33 %}Good
          {% elif states('sensor.particulate_matter_10_0um_concentration') | float <= 34 | float > 50 %}Good
          {% elif states('sensor.particulate_matter_10_0um_concentration') | float <= 51 | float > 58 %}Fair
          {% elif states('sensor.particulate_matter_10_0um_concentration') | float <= 59| float > 66 %}Fair
          {% elif states('sensor.particulate_matter_10_0um_concentration') | float <= 67| float > 75 %}Fair
          {% elif states('sensor.particulate_matter_10_0um_concentration') | float <= 76| float > 83 %}Poor
          {% elif states('sensor.particulate_matter_10_0um_concentration') | float <= 84| float > 91 %}Poor
          {% elif states('sensor.particulate_matter_10_0um_concentration') | float <= 92| float > 100 %}Poor
          {% elif states('sensor.particulate_matter_10_0um_concentration') | float > 101 %}Very Poor
          {% endif %}
        entity_id: sensor.pm_10_concentration_sds011

      air_quality_pm25:
        friendly_name: 'Air Quality PM 2.5'
        value_template: >-
          {% if states('sensor.particulate_matter_2_5um_concentration') | float <= 11 %}Good
          {% elif states('sensor.particulate_matter_2_5um_concentration') | float <= 12 | float > 23 %}Good
          {% elif states('sensor.particulate_matter_2_5um_concentration') | float <= 24 | float > 35 %}Good
          {% elif states('sensor.particulate_matter_2_5um_concentration') | float <= 36 | float > 41 %}Fair
          {% elif states('sensor.particulate_matter_2_5um_concentration') | float <= 42 | float > 47 %}Fair
          {% elif states('sensor.particulate_matter_2_5um_concentration') | float <= 48 | float > 53 %}Fair
          {% elif states('sensor.particulate_matter_2_5um_concentration') | float <= 54 | float > 58 %}Poor
          {% elif states('sensor.particulate_matter_2_5um_concentration') | float <= 59 | float > 64 %}Poor
          {% elif states('sensor.particulate_matter_2_5um_concentration') | float <= 67 | float > 70 %}Poor
          {% elif states('sensor.particulate_matter_2_5um_concentration') | float > 71 %}Very Poor
          {% endif %}
        entity_id: sensor.pm_25_concentration_sds011

In the template editor I see true or false depending on the values but the template sensor does not work.

I know I could cut out a few lines to simplify it but it should work either way.

Pics

good bad

Try this:

- platform: template
  sensors:
    air_quality_pm10:
      friendly_name: 'Air Quality PM 10'
      value_template: >
        {% set ppm10 = states('sensor.particulate_matter_10_0um_concentration')|float %}
        {% if ppm10 <= 50 %} Good
        {% elif 50 < ppm10 <= 75 %} Fair
        {% elif 75 < ppm10 <= 100 %} Poor
        {% elif ppm10 > 100 %} Very Poor
        {% endif %}
1 Like

Thank you tom_l that works great and is much more elegant and neater :grinning:

1 Like