PurpleAir Air Quality Sensor

Sheesh, we’re all doing the same things. Life in CA I guess? So I have a pms5300 on an esphome and it works great. I get the raw values In the log but am struggling with how to get to the AQI conversion. My esphome has a sensor as follows:

  - platform: pmsx003
    type: PMSX003
    pm_1_0:
      filters:
        #- throttle: 60s
        - delta: 2.0
        - sliding_window_moving_average:
            window_size: 45
      name: sensornode_airquality_pm_1_0
      #name: "Particulate Matter <1.0µm Concentration"
    pm_2_5:
      filters:
        #- throttle: 60s
        - delta: 2.0
        - sliding_window_moving_average:
            window_size: 45
        name: sensornode_airquality_pm_2_5

sensornode_airquality_pm_2_5 Contains the 2.5pm value, Now, how do I pump that result into an AQI conversion calculator? I’m so Close but so confused! A template? In my sensor.yaml file? I see your reference to the esphome mods for the HM3301 but I don’t know how to use that.

UPDATE: I figured it out after sleeping on it. Create a new sensor based on a Template:
sensornode_airquality_pm_2_5 is defined in esphome.

Boy, is yaml friggin fussy. WTH is there no line #'s for errors in the template “developer” tab??? you’d think that would be painfully obvious to anyone who has ever tried to debug more than 3 lines of template code!!!

#sensor.yaml:
#
#-----------------------------
  - platform: template
    sensors:
      inside_aqi:
        friendly_name: 'Inside AQI Calc'
        value_template: >-
          {% macro calcAQI(Cp, Ih, Il, BPh, BPl) -%}
          {{ (((Ih - Il)/(BPh - BPl)) * (Cp - BPl) + Il)|round }}
          {%- endmacro %}
          {% if (states('sensor.sensornode_airquality_pm_2_5')|float) > 1000 %}
           invalid
          {% elif (states('sensor.sensornode_airquality_pm_2_5')|float) > 350.5 %}
            {{ calcAQI((states('sensor.sensornode_airquality_pm_2_5')|float), 500.0, 401.0, 500.0, 350.5) }}
          {% elif (states('sensor.sensornode_airquality_pm_2_5')|float) > 250.5 %}
            {{ calcAQI((states('sensor.sensornode_airquality_pm_2_5')|float), 400.0, 301.0, 350.4, 250.5) }}
          {% elif (states('sensor.sensornode_airquality_pm_2_5')|float) > 150.5 %}
            {{ calcAQI((states('sensor.sensornode_airquality_pm_2_5')|float), 300.0, 201.0, 250.4, 150.5) }}
          {% elif (states('sensor.sensornode_airquality_pm_2_5')|float) > 55.5 %}
            {{ calcAQI((states('sensor.sensornode_airquality_pm_2_5')|float), 200.0, 151.0, 150.4, 55.5) }}
          {% elif (states('sensor.sensornode_airquality_pm_2_5')|float) > 35.5 %}
            {{ calcAQI((states('sensor.sensornode_airquality_pm_2_5')|float), 150.0, 101.0, 55.4, 35.5) }}
          {% elif (states('sensor.sensornode_airquality_pm_2_5')|float) > 12.1 %}
            {{ calcAQI((states('sensor.sensornode_airquality_pm_2_5')|float), 100.0, 51.0, 35.4, 12.1) }}
          {% elif (states('sensor.sensornode_airquality_pm_2_5')|float) >= 0.0 %}
            {{ calcAQI((states('sensor.sensornode_airquality_pm_2_5')|float), 50.0, 0.0, 12.0, 0.0) }}
          {% else %}
            invalid
          {% endif %}
        entity_id: sensor.inside_aqi

this defines a new sensor called sensor.inside_aqi that you can use in lovelace
image

type: vertical-stack
cards:
  - type: entities
    entities:
      - sensor.sensornode_airquality_humidity
      - sensor.sensornode_airquality_pressure
      - sensor.sensornode_airquality_temp
      - sensor.sensornode_airquality_pm_2_5
      - sensor.inside_aqi
title: Inside weather
1 Like