Last active
February 9, 2025 15:28
-
-
Save Elwell/e244b1eefe817dab76e8358a5ed9b17c to your computer and use it in GitHub Desktop.
send Home Assistant PM sensors to WAQI
This file contains 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
#!/usr/bin/env python3 | |
# Script to extract sensor values from home assistant and republish to WAQI | |
# Andrew Elwell. Feb 2025. Apache 2.0 licence. | |
# IMPORTANT - You *WILL* need to update waqi_token, ha_token and ha_url to suit your installation. | |
# see https://aqicn.org/data-feed/upload-api/ for WAQI | |
# and https://developers.home-assistant.io/docs/auth_api/#long-lived-access-token for HA | |
import requests | |
waqi_token = 'WAQI_TOKEN_HERE' | |
waqi_url = 'https://aqicn.org/sensor/upload/' | |
ha_token = 'HOME_ASSISTANT_TOKEN_HERE' | |
ha_url = 'http://203.0.113.99:8123/api/states/' | |
# Adjust the station details as required - This positions the marker and name is shown in popup and URL | |
station = { 'id': 'INTERNAL_NAME', 'name': 'PUBLIC_NAME, TOWN', | |
'latitude': -35.30820, 'longitude': 149.12437 } | |
# Key will become the 'specie' (see the upload api page linked above for more details) | |
# Value is the sensor entity in your home assistant instance | |
sensors = { 'pm10': 'sensor.particulate_matter_10_0mm_concentration', | |
'pm2.5': 'sensor.particulate_matter_2_5mm_concentration', | |
'pm1.0': 'sensor.particulate_matter_1_0mm_concentration', | |
'temp': 'sensor.fineoffset_ws90_19906_temperature', | |
'humidity': 'sensor.fineoffset_ws90_19906_humidity', | |
'wind speed': 'sensor.fineoffset_ws90_19906_wind_average', | |
'wind gust': 'sensor.fineoffset_ws90_19906_wind_max', | |
'wind direction': 'sensor.fineoffset_ws90_19906_wind_direction' | |
} | |
readings = [] | |
for k in sensors.keys(): | |
r = requests.get(ha_url + sensors[k], headers={'Authorization': 'Bearer ' + ha_token}) | |
readings.append({'specie': k, 'value': r.json()['state'], 'time': r.json()['last_updated'], 'unit': r.json()['attributes']['unit_of_measurement']}) | |
payload = {'token': waqi_token, 'station': station, 'readings': readings} | |
r = requests.post(waqi_url, json=payload) | |
if (r.status_code != 200): | |
print(r.status_code,r.text, payload) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment