Created
September 29, 2020 07:27
-
-
Save cablehead/3543ae1412d2d0d505f921da350a191c to your computer and use it in GitHub Desktop.
Push data from specific PurpleAir sensors to an Apple Watch complication with Pushover
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 characters
import requests | |
import json | |
from datetime import timedelta | |
from datetime import datetime | |
def calcAQI(Cp, Ih, Il, BPh, BPl): | |
a = (Ih - Il) | |
b = (BPh - BPl) | |
c = (Cp - BPl) | |
return round((a/b) * c + Il) | |
def aqiFromPM(pm): | |
if (pm > 350.5): | |
return calcAQI(pm, 500, 401, 500, 350.5) | |
if (pm > 250.5): | |
return calcAQI(pm, 400, 301, 350.4, 250.5) | |
if (pm > 150.5): | |
return calcAQI(pm, 300, 201, 250.4, 150.5) | |
if (pm > 55.5): | |
return calcAQI(pm, 200, 151, 150.4, 55.5) | |
if (pm > 35.5): | |
return calcAQI(pm, 150, 101, 55.4, 35.5) | |
if (pm > 12.1): | |
return calcAQI(pm, 100, 51, 35.4, 12.1) | |
if (pm >= 0): | |
return calcAQI(pm, 50, 0, 12, 0) | |
sensors = [ | |
4506, # New school | |
40769, # Jefferson | |
59693, # Farm house | |
] | |
def get_sensor(name): | |
res = requests.get("https://www.purpleair.com/json", params={"show": name}) | |
data = res.json() | |
values = [float(x["PM2_5Value"]) for x in data["results"]] | |
return aqiFromPM(sum(values) / len(values)) | |
def glance(now, text): | |
res = requests.post("https://api.pushover.net/1/glances.json", data={ | |
"token": "...", | |
"user": "...", | |
"title": "Purple Air", | |
"text": text, | |
"subtext": now.strftime("%I:%M%p") | |
}) | |
return res | |
def main(): | |
values = [str(get_sensor(s)) for s in sensors] | |
now = datetime.now() - timedelta(hours=7) | |
post = False | |
if now.hour > 6 and now.hour < 22: | |
post = True | |
res = glance(" | ".join(values)) | |
print(post, now, values, res, res.text) | |
return | |
print(post, now, values) | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment