Thursday, September 24, 2020

Air Quality Calculation Purple Air API and Python

Stumbled on an article about the Purple Air Air Quality API. This thing has a wealth of knowledge. Had no clue how Air Quality numbers were calculated. The API provides several different metrics for those folks that purchase the Purple Air monitoring sensors. The sensor feed data into the Purple Air network of sensors and display the data on a nice handy map:

You can easily hit the Purple Air api when you find a sensor in your location. Just click on the sensor you want and inspect the URL. Here is an example of the sensor near Vinita Oklahoma:

That Select = in the Address is what you want to key on. Now that you have the sensor ID you can just plug that in to the URL to get the JSON feed for that sensor:

You will get back some neat looking JSON. Now all you need to do is parse it. See below for sample code. This is using Python to parse out the JSON return value and then doing some fancy math to calculate air quality index based on the value returned for PM2.5 Particulate < 2.5 microns pollutant. You can read up more about the Air Quality Index here:,300%20represents%20hazardous%20air%20quality.

You can then capture this data in a DB or display it however you want.


Post a Comment