I have been using an Uninterruptable Power Supply (UPS) on and off for years. It was in the last few years when we noticed the lights brightening and dimming on halogen bulbs I decided to look into it deeper and fully realising how important a clean power source was. With that and having many devices at home, I wanted a way to monitor and notify devices to shutdown when needed.
I found two UPS monitoring tools on Debian. APCUPSD seemed better suited to a signle machine setup which can monitor one or more UPS. NUT is better suited to multiple machines with their own UPS to monitor.
They both have a lot of overlapping features and complexity in setup are about the same for a single UPS. NUT seems to go further by allowing user account setup and therefore more extensible.
NUT is a package available in Ubuntu’s repository so install is very easy:
sudo apt install nut
This guide goes through a USB UPS setup. Make sure the UPS USB is plugged in.
In the example above, I’m using an American Power Conversion (APC) unit. Make a note of the output specifically the part after “ID” e.g 051d.
Edit /etc/nut/ups.conf and add the following:
xretry = 5
pollinterval = 2
driver = usbhid-ups
port = auto
vendorid = “051d”
desc = “Home UPS”
if there are any other lines like pollinterval = 5 are commented out by putting a “#” at the start of the line.
Find the device rules and copy it to the system directory. The file differs from machine to machine so run the following command to locate the “-nut-usbups.rules”:
sudo ls /lib/udev/rules.d/ | grep nut
Then move the file. In my case, it was assigned 62 as the rule file name:
sudo cp /lib/udev/rules.d/62-nut-usbups.rules /etc/udev/rules.d/
Restart the computer to pick up the device and associate rules:
sudo shutdown -r now
Start the UPS service:
sudo upsdrvctl start
Then it has found a different USB device. Make sure the vendor ID (or some other parameter. See here for details on other filtering mechanisms.
If it successfully connected denoted in the ouput by displaying details about the USB driver then continue below otherwise go back and check your configuration.
Create a user who can monitor the UPS. Edit /etc/nut/upsd.users with the following:
password = mypassword
actions = SET
instcmds = ALL
Replace mypassword with a suitable password.
Next, allow the connected machine to monitor the UPS with the user configured above. The main thing to add is the monitor line in /etc/nut/upsmon.conf:
MONITOR myups@localhost 1 local_mon mypassword master
Change the following:
The rest of the filt can be tailored such as the command to execute and when to shutdown the machine. See here for details.
If you need other devices on the network to monitor the UPS then edit/etc/nut/upsd.conf. Uncomment the lines starting with LISTEN by removing # at the start of the line. Add the IP addresses of the devices allowed to monitor the device. Alternatively, set the listen from 127.0.0.1 to 0.0.0.0 allow any device on the network although this is not recommended. The number 3493 is the port. Feel free to change this if needed.
Lastly, add the line below to set up NUT as a server (as opposed to standalone mode) so that it will wait for connections from other devices:
The above setup will now monitor and when power is lost shutdown the machine. I have tried using this on a Raspberry Pi with great success and the above setup can be extended to notify other devices on the network if needed and integrates with Home Assistant.