Monitoring a UPS with NUT on the Raspberry Pi

Ben
Ben
@benjislab

An Uninterruptible Power Supply (UPS) is crucial for protecting your Raspberry Pi and other devices from power outages and fluctuations. By using Network UPS Tools (NUT) on your Raspberry Pi, you can monitor the status of your UPS and automate safe shutdowns during power failures. This guide will walk you through the steps to set up and configure NUT on your Raspberry Pi.

Equipment Needed

  • Raspberry Pi with Raspberry Pi OS installed
  • UPS with USB or network connection
  • Internet connection
  • Access to the command line

Update Your System

Before starting, ensure your Raspberry Pi is up-to-date with the latest software and security patches.

sudo apt update
sudo apt upgrade

Install NUT

NUT (Network UPS Tools) is a collection of programs for monitoring and managing UPS devices.

  1. Install NUT:
sudo apt install nut

Configure NUT

Step 1: Identify Your UPS

First, identify your UPS to ensure NUT can communicate with it.

  1. List connected USB devices:
lsusb

Look for your UPS in the list of devices.

Step 2: Edit Configuration Files

NUT uses several configuration files located in /etc/nut. You'll need to edit these files to configure your UPS.

  1. Edit ups.conf:
sudo nano /etc/nut/ups.conf

Add a section for your UPS. Replace the placeholder values with the details of your UPS. For example:

[myups]
        driver = usbhid-ups
        port = auto
        desc = "My UPS"

Save and close the file.

  1. Edit upsd.conf:
sudo nano /etc/nut/upsd.conf

Ensure the file contains the following line to allow connections on all network interfaces:

LISTEN 127.0.0.1 3493

Save and close the file.

  1. Edit upsd.users:
sudo nano /etc/nut/upsd.users

Add a section for your NUT user. Replace password with a secure password.

[admin]
        password = password
        actions = SET
        instcmds = ALL

Save and close the file.

  1. Edit upsmon.conf:
sudo nano /etc/nut/upsmon.conf

Add the following lines to configure upsmon to monitor your UPS:

MONITOR myups@localhost 1 admin password master

Save and close the file.

Step 3: Configure Permissions

Ensure the NUT service has the appropriate permissions to communicate with your UPS.

  1. Edit the nut.conf file:
sudo nano /etc/nut/nut.conf

Ensure the file contains the following line:

MODE=standalone

Save and close the file.

Step 4: Start NUT Services

Start and enable the NUT services to run at boot.

sudo systemctl start nut-server
sudo systemctl enable nut-server
sudo systemctl start nut-monitor
sudo systemctl enable nut-monitor

Verify NUT Setup

  1. Check UPS Status:

Use the following command to check the status of your UPS:

upsc myups@localhost

This should display information about your UPS, such as battery status, input voltage, and more.

  1. Test Communication:

Ensure that NUT can communicate with your UPS and receive status updates.

upscmd -u admin -p password myups@localhost test.panel.start

Replace test.panel.start with an appropriate command for your UPS.

Automate Safe Shutdown

To automate safe shutdowns during power outages, configure a shutdown script.

  1. Edit the upsmon.conf file:
sudo nano /etc/nut/upsmon.conf

Add the following lines to define the shutdown command:

SHUTDOWNCMD "/sbin/shutdown -h now"

Save and close the file.

  1. Test the Configuration:

Simulate a power failure by disconnecting the UPS from the mains power. Ensure your Raspberry Pi shuts down safely after a period of time.

Conclusion

By following these steps, you can set up and monitor your UPS with NUT on your Raspberry Pi. This setup ensures your devices are protected from power outages and can shut down safely, preventing data loss and hardware damage. Whether you're running a home server, network storage, or other critical systems, monitoring your UPS with NUT provides peace of mind and reliability.