History tracking for individual AQI attributes, using the WAQI integration

I’m using the WAQI integration to track local air quality, with a primary interest in the pm2.5 counts. Automation triggers my HVAC fan when pm2.5 levels exceed a certain threshold.

Here’s my WAQI config:

sensor:
  - platform: waqi
    token: !secret aqicn_api_token
    locations:
      - seattle

…which yields this lovely sensor and some nifty attributes:

As you can see, the sensor reports a value of 27, which is the count of whatever the current dominant pollutant is. In this case, it’s ozone.

When the sensor updated for the next hour, the dominant pollutant was pm2.5, with a value of 34. But because the sensor’s historical tracking only considers its primary value – regardless of the dominant pollutant it’s from, which may change multiple times per day – its historical data are unreliable:

I attempted to solve this by creating custom variables for both the dominant pollutant and the pm2.5 count:

var:
  aqi_dominant_pollutant:
    friendly_name: "AQI Dominant Pollutant"
    unique_id: "aqi_dominant_pollutant_variable"
    value_template: "{{ sensor.waqi_beacon_hill_seattle_washington_usa.attributes.dominentpol }}"
    tracked_entity_id:
      - sensor.waqi_beacon_hill_seattle_washington_usa

  aqi_pm25:
    friendly_name: "AQI PM2.5 Count"
    unique_id: "aqi_pm25_variable"
    value_template: "{{ sensor.waqi_beacon_hill_seattle_washington_usa.attributes.pm_2_5 | int }}"
    tracked_entity_id:
      - sensor.waqi_beacon_hill_seattle_washington_usa

I then created automation to update these vars when the timestamp of the WAQI data changes. (Couldn’t figure out how to make the auto-updates work using tracked_entity_id.)

service: var.set
data:
  entity_id:
    - var.aqi_dominant_pollutant
  value_template: >-
    {{ states.sensor.waqi_beacon_hill_seattle_washington_usa.attributes.dominentpol }}
service: var.set
data:
  entity_id:
    - var.aqi_pm25
  value_template: >-
    {{ states.sensor.waqi_beacon_hill_seattle_washington_usa.attributes.pm_2_5 | int }}

Results:

Even though I’m casting these values to int, they still seem to be stored as strings. Hence, I can’t get the pretty graph of historical pm2.5 counts that I really want.

Where am I going wrong? Can I simplify this?

I’m not sure about your ‘simplify’ question. However for the graph question, you might need to force a unit of measure on the sensor, example below. Good hunting!

    unit_of_measurement: "AQI"
1 Like

That did the trick, thank you!

I’m curious if anyone has insights about getting tracked_entity_id to work on entities with attributes. Perhaps I’ll take that question to GitHub.