Created
September 1, 2020 20:50
Revisions
-
andersk created this gist
Sep 1, 2020 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,60 @@ #!/usr/bin/env python import json from datetime import datetime import requests SENSOR_ID = 4372 data = requests.get("https://www.purpleair.com/json", {"show": SENSOR_ID}).json() result = data["results"][0] stats = json.loads(result["Stats"]) conc = float(stats["v1"]) conc = int(conc * 10) / 10 # EPA 454/B-18-007 Table 2 green = (0, 228, 0) yellow = (255, 255, 0) orange = (255, 126, 0) red = (255, 0, 0) purple = (143, 63, 151) maroon = (126, 0, 35) # EPA 454/B-18-007 Table 1, 5 for (aqi_low, aqi_high, conc_low, conc_high, color_low, color_high) in [ (0, 50, 0.0, 12.0, green, yellow), (51, 100, 12.1, 35.4, yellow, orange), (101, 150, 35.5, 55.4, orange, red), (151, 200, 55.5, 150.4, red, purple), (201, 300, 150.5, 250.4, purple, maroon), (301, 400, 250.5, 350.4, maroon, maroon), (401, 500, 350.5, 500.4, maroon, maroon), (501, 999, 500.5, 99999.9, maroon, maroon), ]: if conc < conc_high + 0.05: break t = (conc - conc_low) / (conc_high - conc_low) aqi = round(aqi_low + (aqi_high - aqi_low) * t) color = "#" + "".join( format(round(low + (high - low) * t), "02x") for low, high in zip(color_low, color_high) ) print(f'AQI <span color="{color}">{aqi}</span>') print("---") print(result["Label"]) if "humidity" in result: print(f"Humidity {result['humidity']}%") if "temp_f" in result: print(f"Temperature {result['temp_f']}°F") if "pressure" in result: print(f"Pressure {result['pressure']} mbar") print( "Updated", datetime.fromtimestamp(stats["lastModified"] / 1000).isoformat(timespec="seconds"), ) print("Retrieved", datetime.now().isoformat(timespec="seconds")) print( f"View on PurpleAir | href='https://www.purpleair.com/map?select={SENSOR_ID}#14/{result['Lat']}/{result['Lon']}'", )