HomeDomoticaCollector
A Raspberry Pi hub that collects data from every device in my home — solar, weather, EV charger, awnings, thermostats, AC — stores it, and lets me monitor and control everything from a Telegram bot, a LAN web dashboard, and by voice through Alexa.
The idea
My home is full of “smart” devices, each with its own app and portal: solar inverter, weather station, EV charger, sun awnings, thermostats, air conditioners. I wanted one place to see everything, keep its history, and automate how it behaves. So I built a small hub running on an always-on Raspberry Pi.
What it does
It polls the various home systems on a schedule and stores their readings in a SQLite database:
- Solar & battery — production, consumption, state of charge, energy flows.
- Weather station — temperature, humidity, wind, rain, pressure, UV.
- EV charger — status and energy delivered (OCPP protocol).
- Sun awnings — state and position (Somfy TaHoma).
- Thermostats — temperature, setpoint and mode per zone.
- Air conditioners — status and control (Sensibo).
And it offers three ways to interact:
- Telegram bot with inline menus — status of everything plus direct control: open/close the awnings, set thermostat modes, turn on/adjust the AC, start/stop charging, enable or disable the automations, wake home PCs remotely (Wake-on-LAN) or shut them down over SSH.
- LAN web dashboard — a read-only view of the latest readings, history charts, and daily/monthly/yearly trends, with a light/dark theme.
- Alexa voice control — a custom skill to ask for status, command the awnings, and run “scenes”.
A separate, confirmation-gated emergency command shuts everything down in sequence when needed: awnings, AC, NAS, other PCs, and the router itself last.
The automation engine
The “smart” core is a rule engine: rules are defined in the configuration and evaluated after every weather poll. Examples: close the awnings on wind or rain, put the thermostats into frost protection, switch on the AC when the conditions are right.
Rules support multiple AND conditions across different sources (weather, solar, thermostats), time windows, and two priority levels: critical rules (e.g. strong wind) can fire multiple times and lock out the normal rules on the same devices for the rest of the day.
How it’s built
- A Python (async) app on a Raspberry Pi 4, run as a systemd service with automatic restart and network-wait.
- Modular collectors, one per device family, sharing a common base class; a scheduler polls each at its own interval. Readings go to SQLite with automatic migrations.
- The bot, dashboard and Alexa all read the same tables — no duplicate polling.
A concurrency bug and its cure
After months in production, the rule engine had started silently missing its own triggers — up to eleven times a day — with no visible error in the bot or the dashboard. The cause was a SQLite database under pressure: reads, writes, and a dashboard tab left open in a browser (generating around 1,500 calls a day on its own) were locking each other out, and a lock hit while reading the rules’ context quietly crashed the very cycle that evaluates them. Fixed by switching SQLite to WAL mode, adding indexes on the hottest queries, pausing the dashboard’s polling when its tab is hidden, and making the rule engine tolerate a failed read instead of stopping altogether.
Engineering notes
- Cloudflare Tunnel exposes the HTTPS endpoint for Alexa without opening any ports on the router: the Pi’s IP stays private and TLS is handled by Cloudflare. The dashboard, by contrast, is deliberately kept LAN-only.
- Resilience: the optional features (Alexa, dashboard) are guarded so a failure can never take down the collector; a network watchdog reboots the Pi after a prolonged loss of connectivity.
git push= deploy: a plain push from the Mac also updates the repo on the Raspberry Pi, which reinstalls any changed dependencies, checks that the code compiles before stopping the service, and restarts everything on its own — no manual steps on the Pi for a routine change.- Pragmatic fixes: where
urllibtimed out on the Pi I shelled out tocurl; OAuth tokens are refreshed and persisted across restarts. - Handles device quirks (e.g. one awning wired in reverse — open and close swapped in software).
Stack
Python (async) · Raspberry Pi 4 · SQLite · Telegram Bot · aiohttp · OCPP · Cloudflare Tunnel · systemd
Next steps
- Add more thermostat zones.
- Expand the Alexa intents.