I started looking into the AQI calculations based on the SQL database itself, and discovered that the Home Assistant database seems to be missing a lot of readings from the AirGradient. I’m not sure if this is something about HA, or if the AirGradient device itself is skipping readings, but it’s definitely another kink to work out in getting everything running.
You’ll obviously need to adjust the sensor ID to match yours, but here’s some sample SQL you can run directly against the sqlite database (I run HA in a docker container, not sure how to access the raw db if you run HA-OS). It returns the average state and number of readings for every hour (should be a reading every 3 minutes, so 20 per hour).
SELECT
datetime(FLOOR(AVG(states.last_updated_ts)/3600) * 3600, 'unixepoch') AS by_hour,
COUNT(*) AS count,
AVG(states.state) AS state
FROM
states
INNER JOIN states_meta ON
states.metadata_id = states_meta.metadata_id
-- LEFT JOIN state_attributes ON
-- states.attributes_id = state_attributes.attributes_id
WHERE
-- states_meta.entity_id = 'sensor.airgradient_open_air_airgradient_open_air_particulate_matter_2_5um_concentration'
states_meta.entity_id = 'sensor.airgradient_open_air_airgradient_open_air_particulate_matter_2_5um_concentration_1'
-- OR states_meta.entity_id = 'sensor.airgradient_open_air_airgradient_open_air_particulate_matter_2_5um_concentration_2'
-- states_meta.entity_id = 'sensor.airgradient_open_air_airgradient_open_air_particulate_matter_10_0um_concentration'
-- states_meta.entity_id = 'sensor.airgradient_open_air_airgradient_open_air_particulate_matter_10_0um_concentration_1'
-- OR states_meta.entity_id = 'sensor.airgradient_open_air_airgradient_open_air_particulate_matter_10_0um_concentration_2'
AND states.state != 'unavailable'
AND states.last_updated_ts >= unixepoch(date('now','start of day','-1 day'))
-- AND states.last_updated_ts < unixepoch(date('now','start of day'))
GROUP BY
floor(states.last_updated_ts/3600) -- group by hour
ORDER BY
states.last_updated_ts DESC
Replace 3600
with 60
to group things by minute , which makes it pretty easy to see where there are gaps.
I’ve been watching the on-device logs through its web console and I don’t see any errors or skipped time codes, so I suspect this is a communication issue with esphome and home assistant, rather than something blocking the readings on the device itself.
Anyway, just an annoyance to work through.