Bash script that uploads the current air quality and weather data from the Open-Meteo API to influxdb on an hourly basis
If using telegraf + influxdb the same outcome can be achieved via input.http
plugin + json_v2
parser with the following config (just need to set the correct latitude/longitude in the URL query parameter):
[[inputs.http]]
name_override = "open_meteo_air_quality"
interval = "5m"
startup_error_behavior = "retry"
urls = [
"https://air-quality-api.open-meteo.com/v1/air-quality?timeformat=unixtime&latitude=$LATITUDE&longitude=$LONGITUDE¤t=european_aqi,us_aqi,pm10,pm2_5,carbon_monoxide,nitrogen_dioxide,sulphur_dioxide,ozone,aerosol_optical_depth,dust,uv_index,uv_index_clear_sky"
]
data_format = "json_v2"
[[inputs.http.json_v2]]
[[inputs.http.json_v2.object]]
path = "current"
disable_prepend_keys = true
[[inputs.http]]
name_override = "open_meteo_current_weather"
interval = "5m"
startup_error_behavior = "retry"
urls = [
"https://api.open-meteo.com/v1/forecast?timeformat=unixtime&latitude=$LATITUDE&longitude=$LONGITUDE¤t=temperature_2m,relative_humidity_2m,apparent_temperature,precipitation,rain,showers,snowfall,weather_code,cloud_cover,pressure_msl,surface_pressure,wind_speed_10m,wind_direction_10m,wind_gusts_10m"
]
data_format = "json_v2"
[[inputs.http.json_v2]]
[[inputs.http.json_v2.object]]
path = "current"
disable_prepend_keys = true
-
Configure
open_meteo_exporter.conf
(see the configuration section below). -
Run it.
docker compose up --detach
-
Build the docker image.
docker build . --tag open-meteo-exporter
-
Configure
open_meteo_exporter.conf
(see the configuration section below). -
Run it.
docker run --rm --init --tty --interactive --read-only --cap-drop ALL --security-opt no-new-privileges:true --cpus 2 -m 64m --pids-limit 16 --volume ./open_meteo_exporter.conf:/app/open_meteo_exporter.conf:ro ghcr.io/rare-magma/open-meteo-exporter:latest
For convenience, you can install this exporter with the following command or follow the manual process described in the next paragraph.
make install
$EDITOR $HOME/.config/open_meteo_exporter.conf
-
Copy
open_meteo_exporter.sh
to$HOME/.local/bin/
and make it executable. -
Copy
open_meteo_exporter.conf
to$HOME/.config/
, configure it (see the configuration section below) and make it read only. -
Copy the systemd unit and timer to
$HOME/.config/systemd/user/
:
cp open-meteo-exporter.* $HOME/.config/systemd/user/
- and run the following command to activate the timer:
systemctl --user enable --now open-meteo-exporter.timer
It's possible to trigger the execution by running manually:
systemctl --user start open-meteo-exporter.service
The config file has a few options:
INFLUXDB_HOST='influxdb.example.com'
INFLUXDB_API_TOKEN='ZXhhbXBsZXRva2VuZXhhcXdzZGFzZGptcW9kcXdvZGptcXdvZHF3b2RqbXF3ZHFhc2RhCg=='
ORG='home'
BUCKET='openmeteo'
LONGITUDE='1.23456'
LATITUDE='1.23456'
INFLUXDB_HOST
should be the FQDN of the influxdb server.ORG
should be the name of the influxdb organization that contains the data bucket defined below.BUCKET
should be the name of the influxdb bucket that will hold the data.INFLUXDB_API_TOKEN
should be the influxdb API token value.- This token should have write access to the
BUCKET
defined above.
- This token should have write access to the
LONGITUDE
should be the longitude in numerical value of the location to collect data for.LATITUDE
should be the latitude in numerical value of the location to collect data for.
Run the script manually with bash set to trace:
bash -x $HOME/.local/bin/open_meteo_exporter.sh
Check the systemd service logs and timer info with:
journalctl --user --unit open-meteo-exporter.service
systemctl --user list-timers
- european_aqi:
EAQI
- us_aqi:
USAQI
- pm10:
μg/m³
- pm2_5:
μg/m³
- carbon_monoxide:
μg/m³
- nitrogen_dioxide:
μg/m³
- sulphur_dioxide:
μg/m³
- ozone:
μg/m³
- aerosol_optical_depth:
ratio from 0 to 1
- dust:
μg/m³
- uv_index:
number
- uv_index_clear_sky:
number
- temperature_2m:
°C
- relative_humidity_2m:
%
- apparent_temperature:
°C
- precipitation:
mm
- rain:
mm
- showers:
mm
- snowfall:
cm
- weather_code:
wmo code
- cloud_cover:
%
- pressure_msl:
hPa
- surface_pressure:
hPa
- wind_speed_10m:
km/h
- wind_direction_10m:
°
- wind_gusts_10m:
km/h
open_meteo_air_quality,longitude=1.23456,latitude=1.23456 european_aqi=50,us_aqi=61,pm10=43.4,pm2_5=15.7,carbon_monoxide=145.0,nitrogen_dioxide=0.1,sulphur_dioxide=0.4,ozone=53.0,aerosol_optical_depth=0.19,dust=45.0,uv_index=0.00,uv_index_clear_sky=0.00 1702227600
open_meteo_current_weather,longitude=1.23456,latitude=1.23456 temperature_2m=28.5,relative_humidity_2m=80,apparent_temperature=32.3,precipitation=0.00,rain=0.00,showers=0.00,snowfall=0.00,weather_code=2,cloud_cover=52,pressure_msl=1011.9,surface_pressure=1011.9,wind_speed_10m=19.5,wind_direction_10m=65,wind_gusts_10m=24.5 1702233900
In open-meteo-dashboard.json
there is an example of the kind of dashboard that can be built with open-meteo-exporter
data:
Import it by doing the following:
- Create a dashboard
- Click the dashboard's settings button on the top right.
- Go to JSON Model and then paste there the content of the
open-meteo-dashboard.json
file.
For convenience, you can uninstall this exporter with the following command or follow the process described in the next paragraph.
make uninstall
Run the following command to deactivate the timer:
systemctl --user disable --now open-meteo-exporter.timer
Delete the following files:
~/.local/bin/open_meteo_exporter.sh
~/.config/open_meteo_exporter.conf
~/.config/systemd/user/open-meteo-exporter.timer
~/.config/systemd/user/open-meteo-exporter.service
This project takes inspiration from the following: