A home event bus: why have one when notifications already exist

One stream of events for the phone, voice and the bot. Why an event describes a fact, not a text, and how the broker died for twelve days unnoticed.

The power monitor at the country house, which I have already written about, sends notifications to the phone, and for a long time that was enough.

Then I wanted more: for the house to announce a power outage by voice, for the family bot to write about it in the shared chat, and for the home automation to be able to react to it. Every new recipient required a separate integration with every source, and so the house got a shared event bus.

An event describes a fact

A source publishes an event once, and recipients subscribe to what they need. The transport is an ordinary MQTT broker.

More important than the broker was the decision about what counts as an event. An event describes a fact: what happened, to what, and how urgent it is. The wording for a human is produced by the recipient.

Simplified, an event looks like this:

{"key": "msg_on_inverter", "priority": "high"}

I did not arrive at this form right away. At first the router sent Russian text, and it arrived as spaces: the router loses Cyrillic. Now the router sends only machine fields, and the recipient assembles the text. In substance this turned out to be more correct than the original version.

Who publishes and who listens

Two publish right now: the power monitor and the router. The monitor has 25 kinds of events and a separate topic with the current state, which the broker retains for new subscribers. The router sends events about the state of the communication links.

The home automation listens, and it has a single point of voice output. The priority is set by the source, the automation only routes:

PriorityWhat happens
urgentby voice, loudly, twice, even during quiet hours
highby voice once, silent at night
the restnot spoken by voice

The decision "whether to wake the house" is made by the one who knows the substance of the event, not by the one who speaks it.

Why the old notifications stayed

It would seem that once there is a broker, a separate service for phone notifications is not needed. But these channels fail in different ways.

In a long power outage the server with the broker will shut down when the battery is drained, while the monitor runs on the router with its own UPS and keeps running. The notification channel has a queue of undelivered messages, the bus does not. On the other hand, the broker is local and survives a break in the link between the home and the country house, and the notification service does not. The channels complement each other, and I did not remove either one.

For the same reason the detection itself stayed in the monitor. It was tempting to move it into the home automation, but that gets updated, restarted and dies together with the house.

My own client and a place for the broker

The power monitor has a "zero external dependencies" principle, so a ready-made library did not fit. One-way publishing is done by a small module on the standard library.

There turned out to be exactly one place for the broker in the network. Both the monitor from the trusted segment and the automation from the untrusted one have to reach it, and the router does not let traffic through in the opposite direction.

Twelve days of silence

At the beginning of September the owner of the broker's files changed. The password file became unreadable for the broker, but the broker kept working on the already open file until it was restarted.

After the restart it went into a crash loop, and two days later it stopped completely. This was found twelve days later: the router's log was packed with messages about an unreachable broker. Nobody was watching the broker, and its absence did not show itself in any way.

Since then the broker runs as the user who owns its files, and monitoring got a check of the broker itself. I never found out who changed the owner of the files: in time it coincided with work on quotas on the server.

Downsides

  • the broker became one more component that has to be watched;
  • there is no queue of undelivered messages. A recipient that was switched off will miss the event, only the current state will remain;
  • events are not encrypted, the bus is designed for a home network;
  • the list of event kinds lives in the source's code. A new recipient has to look into it.

While there is one recipient, the phone, ordinary notifications are enough. The bus became necessary when voice and the bot were added to the phone.

Tags: Architecture, Events, Monitoring, Self-hosting

LinkedIn X Telegram

Read next

Comments