Skip to content

Instantly share code, notes, and snippets.

@jasonsnell
Created August 26, 2020 17:50
Show Gist options
  • Save jasonsnell/86e5e0be7dfa9827c6b0f9912c33e8c4 to your computer and use it in GitHub Desktop.
Save jasonsnell/86e5e0be7dfa9827c6b0f9912c33e8c4 to your computer and use it in GitHub Desktop.
Air Quality AppleScript
-- PurpleAir station ID
set theStationID to "6732"
tell application "JSON Helper"
set theWeather to (fetch JSON from ("https://www.purpleair.com/json?show=" & theStationID) with cleaning feed)
set theStatsA to (read JSON from (Stats of item 1 of results of theWeather))
set theStatsB to (read JSON from (Stats of item 2 of results of theWeather))
set theLocation to Label of item 1 of results of theWeather
set theLat to (Lat of item 1 of results of theWeather)
set theLon to (Lon of item 1 of results of theWeather)
end tell
set sensorA to v1 of theStatsA
set sensorB to v1 of theStatsB
set sensorAtrend to v of theStatsA
set sensorBtrend to v of theStatsB
set sensorRound to round ((sensorA + sensorB) / 2)
set sensorTrend to round ((sensorAtrend + sensorBtrend) / 2)
set theAverageAQI to aqifromPM(sensorRound)
set theCurrentAQI to aqifromPM(sensorTrend)
if (theAverageAQI - theCurrentAQI) > 9 then
set theTrend to ""
else if (theAverageAQI - theCurrentAQI) < -9 then
set theTrend to ""
else
set theTrend to ""
end if
if theAverageAQI > 300 then
set theColor to "#7e0023"
set theStatus to "Hazardous"
else if theAverageAQI > 200 then
set theColor to "#8f3f97"
set theStatus to "Very Unhealthy"
else if theAverageAQI > 150 then
set theColor to "#ff0000"
set theStatus to "Unhealthy"
else if theAverageAQI > 100 then
set theColor to "Orange"
set theStatus to "Unhealthy Sensitive"
else if theAverageAQI > 50 then
set theColor to "#bbbb00"
set theStatus to "Moderate"
else
set theColor to "#00e400"
set theStatus to "Good"
end if
return (theStatus & " (" & theAverageAQI & theTrend & ")|color=" & theColor & "
" & "---" & "
" & theLocation & " (Map)|href=https://www.purpleair.com/map?opt=1/mAQI/a10/cC0&select=" & theStationID & "#14/" & theLat & "/" & theLon)
on aqifromPM(thePM)
if thePM > 350.5 then
return calcAQI(thePM, 500, 401, 500, 350.5)
else if thePM > 250.5 then
return calcAQI(thePM, 400.0, 301.0, 350.4, 250.5)
else if thePM > 150.5 then
return calcAQI(thePM, 300.0, 201.0, 250.4, 150.5)
else if (thePM > 55.5) then
return calcAQI(thePM, 200.0, 151.0, 150.4, 55.5)
else if (thePM > 35.5) then
return calcAQI(thePM, 150.0, 101.0, 55.4, 35.5)
else if (thePM > 12.1) then
return calcAQI(thePM, 100.0, 51.0, 35.4, 12.1)
else if (thePM 0.0) then
return calcAQI(thePM, 50.0, 0.0, 12.0, 0.0)
end if
end aqifromPM
on calcAQI(the CP, the IH, the IL, the BPH, the BPL)
set the A to (the IH - the IL)
set the B to (the BPH - the BPL)
set the C to (the CP - the BPL)
return round ((the A / the B) * the C + the IL)
end calcAQI
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment