Flask + Socket.IO API for reading weather metrics from InfluxDB.
weather_config.pyRequired:
INFLUXDB_URL - InfluxDB URL (for example http://localhost:8086)INFLUXDB_TOKEN - InfluxDB tokenOptional:
INFLUXDB_ORG - default: HAINFLUXDB_BUCKET - default: home_assistantTIMEZONE - default: UTC (for example Europe/Prague)pip install -r requirements.txt
python main.py
Server default address:
http://0.0.0.0:5000
.env from example:cp .env.example .env
Fill real values in .env (mainly INFLUXDB_TOKEN).
Start API (build is automatic):
docker compose up --build api
docker compose down
docker compose --profile test run --rm api-tests
This command builds the image automatically when needed and runs:
python -m unittest discover -v -s tests
Base URL:
http://localhost:5000
GET /weatherResponse:
{
"metrics": [
"absolute_pressure",
"daily_rain",
"dewpoint",
"hourly_rain",
"humidity",
"outdoor_temperature",
"relative_pressure",
"wind_gust",
"wind_speed",
"yearly_rain"
]
}
GET /weather/<metric>?start_time=<time>&end_time=<time>Supported start_time / end_time formats:
2025-01-01T00:00:00+00:00)-1h, -7d)Example:
curl "http://localhost:5000/weather/outdoor_temperature?start_time=-24h&end_time=-1h"
Success (200):
{
"time_values": ["2026-03-05T11:00:00+00:00"],
"measurement_values": [7.2]
}
Validation error (400):
{
"error": "Please provide start_time and end_time parameters in the URL."
}
Unknown metric (404):
{
"error": "Unknown weather metric 'xyz'."
}
GET /weather/<metric>/latestGET /weather/<metric>/latest?sampling_period=-6hNote: sampling_period must be a relative duration (-1h, -30m, -7d).
Example:
curl "http://localhost:5000/weather/humidity/latest?sampling_period=-2h"
Success (200):
{
"time_values": ["2026-03-05T11:12:00+00:00"],
"measurement_values": [63]
}
Validation error (400):
{
"error": "Invalid sampling_period format. Use relative duration like -1h."
}
GET /weather/all_weather_data?start_time=<time>&end_time=<time>Example:
curl "http://localhost:5000/weather/all_weather_data?start_time=-6h&end_time=-1h"
Response shape:
{
"outdoor_temperature": {
"time_values": ["2026-03-05T11:00:00+00:00"],
"measurement_values": [7.2]
},
"humidity": {
"time_values": ["2026-03-05T11:00:00+00:00"],
"measurement_values": [63]
}
}
Socket.IO runs on the same host/port as HTTP.
subscribe with payload { "measurement": "<channel>" }.measurement)outdoor_temperaturehumiditydewpointabsolute_pressurerelative_pressurewind_gustwind_speedhourly_raindaily_rainyearly_rainServer emits events in this format:
<measurement>_update
Example payload (outdoor_temperature_update):
{
"measurement": "outdoor_temperature",
"time_values": ["2026-03-05T11:12:00+00:00"],
"measurement_values": [7.3]
}
Unknown HTTP routes return:
{
"error": "404 Not found",
"message": "This route is not available. Please check your URL."
}
Basic route tests are in:
tests/test_weather_routes.pyRun locally:
python -m unittest discover -v -s tests
Run in Docker:
docker compose --profile test run --rm api-tests
main.py - Flask/Socket.IO app bootstrap + websocket update loopapproutes/weather.py - unified weather REST routesweather_config.py - metric mapping and Influx settingsRead.py - InfluxDB query helpersdocker-compose.yml - local run + test servicesContent type
Image
Digest
sha256:a67265065…
Size
67.2 MB
Last updated
7 months ago
docker pull vlepic/api_server