Skip to content

Bash script that uploads the last 24h weather conditions from the AEMET OpenData API to InfluxDB on a hourly basis

License

Notifications You must be signed in to change notification settings

rare-magma/aemet-exporter

Repository files navigation

aemet-exporter

Bash script that uploads the last 24h weather conditions from the AEMET OpenData API to influxdb on a hourly basis

Dependencies

Relevant documentation

Installation

With Docker

docker-compose

  1. Configure aemet_exporter.conf (see the configuration section below).

  2. Run it.

    docker compose up --detach

docker build & run

  1. Build the docker image.

    docker build . --tag aemet-exporter
  2. Configure aemet_exporter.conf (see the configuration section below).

  3. 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 ./aemet_exporter.conf:/app/aemet_exporter.conf:ro ghcr.io/rare-magma/aemet-exporter:latest

With the Makefile

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/aemet_exporter.conf

Manually

  1. Copy aemet_exporter.sh to $HOME/.local/bin/ and make it executable.

  2. Copy aemet_exporter.conf to $HOME/.config/, configure it (see the configuration section below) and make it read only.

  3. Copy the systemd unit and timer to $HOME/.config/systemd/user/:

cp aemet-exporter.* $HOME/.config/systemd/user/
  1. and run the following command to activate the timer:
systemctl --user enable --now aemet-exporter.timer

It's possible to trigger the execution by running manually:

systemctl --user start aemet-exporter.service

Config file

The config file has a few options:

INFLUXDB_HOST='influxdb.example.com'
INFLUXDB_API_TOKEN='ZXhhbXBsZXRva2VuZXhhcXdzZGFzZGptcW9kcXdvZGptcXdvZHF3b2RqbXF3ZHFhc2RhCg=='
ORG='home'
BUCKET='aemet'
AEMET_API_KEY='ZXhhbXBsZXRva2VuZXhhcXdzZGFzZGptcW9kcXdvZGptcXdvZHF3b2RqbXF3ZHFhc2RhCg=='
AEMET_WEATHER_STATION_CODE='1234Y'
  • 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.
  • AEMET_API_KEY should be the AEMET API key requested in the AEMET OpenData's website
  • AEMET_WEATHER_STATION_CODE should be the alphanumeric code assigned to the weather station. Can be found in AEMET OpenData's website "Seleccione una estación" dropdown.

Troubleshooting

Run the script manually with bash set to trace:

bash -x $HOME/.local/bin/aemet_exporter.sh

Check the systemd service logs and timer info with:

journalctl --user --unit aemet-exporter.service
systemctl --user list-timers

Exported metrics for each hour

Weather stations might have a different set of sensors so not all metrics below might be populated.

  • temperature: °C
  • humidity: %
  • pressure: hPa
  • windspeed: km/h
  • windgust: km/h
  • winddirection: °
  • precipitation: mm
  • dewpoint: °C
  • visibility: km
  • insolation: hours
  • snow: cm

Exported metrics example

aemet_weather_conditions,station=3195 temperature=11.1,humidity=99.0,pressure=949.7,windspeed=0.9,winddirection=271.0,windgust=2.9,precipitation=0.0,dewpoint=11.0,visibility=1.0,insolation=1.0,snow=0.0 1702206000

Example grafana dashboard

In aemet-dashboard.json there is an example of the kind of dashboard that can be built with aemet-exporter data:

example grafana dashboard screenshot

Import it by doing the following:

  1. Create a dashboard
  2. Click the dashboard's settings button on the top right.
  3. Go to JSON Model and then paste there the content of the aemet-dashboard.json file.

Uninstallation

With the Makefile

For convenience, you can uninstall this exporter with the following command or follow the process described in the next paragraph.

make uninstall

Manually

Run the following command to deactivate the timer:

systemctl --user disable --now aemet-exporter.timer

Delete the following files:

~/.local/bin/aemet_exporter.sh
~/.config/aemet_exporter.conf
~/.config/systemd/user/aemet-exporter.timer
~/.config/systemd/user/aemet-exporter.service

Credits

Información elaborada por la Agencia Estatal de Meteorología © AEMET

This project takes inspiration from the following:

About

Bash script that uploads the last 24h weather conditions from the AEMET OpenData API to InfluxDB on a hourly basis

Topics

Resources

License

Stars

Watchers

Forks

Packages