What happens when the power goes out? Well, not much. At least not for very long.
Several UPS units protect parts of my home infrastructure. That gives me enough time to shut down the systems cleanly after roughly ten minutes. Since several devices share a UPS, I use Network UPS Tools (NUT) in a client/server setup.
I also want to know when mains power disappears, how the battery is doing, and when it might be time to replace it. For my home automation system, OpenHAB, MQTT was the obvious bridge.
About this article: I built the original setup in December 2020. The concept is still useful, but Python packaging and systemd have changed. The examples below are updated for modern Debian/Ubuntu installations. Always check the upstream project's README before deploying.
NUT to MQTT
The starting point is a running NUT server that exposes the UPS values. I used the community project ups-mqtt to publish those values via MQTT.
Install the basics on Debian or Ubuntu:
sudo apt update
sudo apt install python3 python3-venv python3-pip nut git
sudo git clone https://github.com/dniklewicz/ups-mqtt.git /opt/ups-mqtt
cd /opt/ups-mqtt
python3 -m venv .venv
.venv/bin/python -m pip install paho-mqtt
The virtual environment is deliberate. Installing Python packages globally with sudo pip can conflict with system-managed Python installations. Depending on the project's current requirements, you may need more dependencies from its README.
Now adjust the project's configuration file to point at the NUT server and MQTT broker. Give the bridge a dedicated MQTT user with only the permissions it needs. On the machine running the NUT server, allow the bridge's NUT client to connect.
For a first test:
cd /opt/ups-mqtt
.venv/bin/python ups-mqtt.py
Messages should now appear on your MQTT broker. I use MQTT Explorer for quick checks, because it lets me inspect topics and payloads before I even touch OpenHAB.
A systemd service
A bridge should come back after a reboot. I prefer running it as a dedicated unprivileged user:
sudo useradd --system --home-dir /opt/ups-mqtt --shell /usr/sbin/nologin upsmqtt
sudo chown -R upsmqtt:upsmqtt /opt/ups-mqtt
Create the file /etc/systemd/system/ups-mqtt.service:
[Unit]
Description=NUT to MQTT bridge
Wants=network-online.target
After=network-online.target
[Service]
Type=simple
User=upsmqtt
Group=upsmqtt
WorkingDirectory=/opt/ups-mqtt
ExecStart=/opt/ups-mqtt/.venv/bin/python /opt/ups-mqtt/ups-mqtt.py
Restart=on-failure
RestartSec=5
[Install]
WantedBy=multi-user.target
Then enable it and check its status:
sudo systemctl daemon-reload
sudo systemctl enable --now ups-mqtt.service
sudo systemctl status ups-mqtt.service
To follow the logs:
sudo journalctl -u ups-mqtt.service -f
The entry point and config format of a community project can change. Check the current repository before blindly copying either the command or the service file.
Putting the data to work
Once NUT publishes MQTT messages, OpenHAB can subscribe to the relevant topics. UPS status, battery charge and estimated runtime are especially interesting. I can display them in the automation interface and use them for notifications.
One important distinction: monitoring and shutdown are separate jobs. MQTT is great for status and alerts, but the actual clean shutdown should remain with NUT and its clients. If a broker goes down during a power cut, I still want the servers to shut down safely.
That was my setup back in 2020. The details of Linux packaging evolve, but the basic idea remains sound: let NUT manage the UPS, use MQTT to make its data visible, and never make the shutdown depend on the dashboard.