Skip to content

About

Small web page to adjust the heating per room from a phone: target temperature and window-open pause for Sonoff TRVZB thermostats through Zigbee2MQTT.

Resources

Stars

1 star

Watchers

1 watching

Forks

Repository files navigation

heating-control

A small web page to adjust the heating per room from a phone on the home network: change a room's target temperature, mark a window as open so the room doesn't heat, or lower every room while away and get the old targets back on their own. It controls Sonoff TRVZB thermostats through Zigbee2MQTT.

The heating page at phone width: a green All OK status in the top right, then Office and Bedroom cards, each with the room temperature, whether it is heating, the target, buttons to change it, and Air 5, 15, 30 and 60 buttons

The page can be added to the phone's home screen with its own icon: "Share → Add to Home Screen" in Safari, "Add to home screen" in Chrome. On iPhone it then opens full-screen. Android Chrome only installs a page as an app over HTTPS, so over plain HTTP it adds a shortcut, which may open as a normal browser tab. The page has no service worker and caches nothing, so it always shows the live state and only works on the home network. Offline use or notifications would need HTTPS too. The icons are drawn by internal/web/icon.go: change it and run go generate ./....

Airing

"Air 5/15/30/60" sends system_mode: off for that many minutes, with 5 for a quick Stoßlüften. When the time is up, or on "Heat again", the room gets back the mode and target from before.

Airing is saved, so it carries on after a restart or release: a room that should be off is turned off again, and one whose time ran out while the service was down gets heat back right away. A change that can't be saved isn't made, and the page says so. See Saved state.

  • A target changed during airing is applied when it ends.
  • A thermostat leaving off during airing gets off once more. If it leaves off again, someone is at the radiator, so airing ends. So turning it on by hand during airing takes two tries.
  • Commands repeat until the thermostat confirms them, every 30 s for about 5 minutes. A target then gives up and shows the thermostat's value. Off and heat-again keep trying every 5 minutes and show a problem on the page, because giving up would mean heating with the window open or a cold room.
  • A thermostat that is off with no airing is left alone, because someone may have turned it off on purpose. The page offers "Heat again" for it.

Away

"Away…" lowers every room to one target until a date and time, then raises each room back to the target it had before, on its own. The page suggests 17 °C and the next day; the end can be at most 30 days ahead. While away, the page shows when it ends and what each room gets back, and "Back now" ends it early.

  • Only rooms whose target is known take part. A room whose target someone changed during away is left alone at the end.
  • Starting away again during away sets a new end and target, and keeps the targets from before.
  • Raising the target back follows the rules for the end of airing: it repeats until the thermostat confirms it and never gives up, and the page shows a problem if it takes longer than about 5 minutes. A thermostat that is off then gets the target once it heats again, and a room airing at the end gets it when airing ends.
  • Away is saved, so it carries on after a restart. If the end passed while the service was down, the targets come back as soon as it runs again.

Requirements

Needs Why Tested with
MQTT broker How the service reaches Zigbee2MQTT. It connects without a username or TLS, because those aren't supported yet Mosquitto 2.1
Zigbee2MQTT Talks to the thermostats. The service uses <baseTopic>/<trv> for state, and /set and /get for commands, with device friendly names from the config 2.14
Sonoff TRVZB thermostats The service relies on how they behave, see the comment in controller.go, so other models may not work Firmware 1.3.0

Optional:

  • A room sensor in Zigbee2MQTT that reports temperature, for the temperature on the page. Without one, the page shows the thermostat's value. A thermostat only regulates on a room sensor if something else feeds it external_temperature_input: this service doesn't.
  • Prometheus Operator, for the chart's serviceMonitor.enabled. /metrics works without it.

The chart needs Kubernetes and Helm. Without them, run the binary or image with -config <file>, using the same settings as config in the chart's values.yaml.

It also needs a writable directory that survives a restart, dataDir: the chart mounts a persistent volume there. See Saved state.

Saved state

The service keeps one SQLite database, heating-control.db in dataDir. It holds only what can't come back from the thermostats: for each room that is airing, when airing ends, the mode to go back to, and a target chosen during airing; and for each room that is away, when away ends, the away target and the target to go back to. A room's rows are deleted once its thermostat confirms it is heating again or has its target back, so the file is a few kilobytes. Everything else is in memory and comes back from the thermostats' reports. Migrations are part of the binary and run at start; an older binary refuses a newer database.

A request is answered only once its change is saved. If saving fails, the change isn't made and the page shows "Not changed". When airing ends while the database can't be written, the room still gets heat back, and the database catches up later.

If the file is lost, nothing unsafe happens: a room that was airing stays off until someone taps "Heat again" (the TrvStuckOffWarning alert fires after 2 hours), a target chosen during airing is lost, and rooms that are away stay at the away target until someone raises them. Because of that, restoring a backup is optional: an old copy may even hold airing or away that is long over, which then simply ends at once. The service starts with an empty database if there is none, but refuses to start if it can't open or migrate the file.

To copy the database while the service runs:

kubectl exec -n <namespace> deploy/<release> -- /heating-control backup /data/backup.db

The target must not exist yet. Copying it off the node is up to the cluster's backup.

Deployment

The Helm chart in charts/heating-control is published with every release as oci://ghcr.io/subpathdev/charts/heating-control. Set config.broker and config.rooms; adding a room is one more entry in config.rooms. The chart creates a 64 Mi persistent volume for config.dataDir (see persistence in values.yaml), and keeps it when the release is uninstalled. Run one replica only, and give any other instance its own config.clientId: a second client with the same ID disconnects the first.

API

internal/api/openapi.yaml (OpenAPI 3.0) is the contract between the backend and the page, and a running server also serves it at /api/openapi.yaml. The Go types and server interface are generated from it, so change the file first, then run go generate ./.... CI fails if the generated code is out of date. /healthz, /readyz and /metrics are for operations and aren't part of the contract.

Releases

Merging to main releases automatically, based on conventional commits: feat: is a minor bump and fix: a patch, while docs:, ci:, chore: and the like don't release. While the version is 0.x, a breaking change is a minor bump too. A release publishes ghcr.io/subpathdev/heating-control:<version>, the Helm chart of the same version and a GitHub release. To deploy it, merge the chart bump in the repo that runs it.

About

Small web page to adjust the heating per room from a phone: target temperature and window-open pause for Sonoff TRVZB thermostats through Zigbee2MQTT.

Resources

Stars

1 star

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages