Home Assistant - NUT UPS Energy Tracking

From ETCwiki
Revision as of 18:52, 24 October 2021 by Ddxfish (talk | contribs)
Jump to navigationJump to search

If you have a UPS (Uninterruptible Power Supply) that has either a USB connection or ethernet connection, you can likely add it to Home Assistant Server as an energy tracking device. This guide is generic enough to work with most USB UPS but I am using a Cyberpower CP1000, and have 2 of them showing up as an energy tracking device in my Home Assistant Energy section.

Network UPS Tools

Install Network UPS Tools

Network UPS Tools also called NUT is the tool we run on Linux to track UPS features.

sudo apt update
sudo apt install nut

Edit /etc/nut/nut.conf

The default mode is none, so lets change it to standalone in this file

MODE=standalone

Edit /etc/nut/ups.conf

We need to add our UPS to this file so NUT knows what driver to use. Add this section to your ups.conf. Name your ups in the square brackets, it doesn't have to say cyberpowerb, this is just mine.

[cyberpowerb]
     driver = usbhid-ups
     port = auto
     desc = "Cyberpower CP1000 B"
     pollfreq = 30

If you are having detection problems, add this line to your ups.conf config just under the driver line. Replace with your UPS vendor ID (find via lsusb)

vendorid = 0764

Edit /etc/nut/upsd.conf

Add a LISTEN directive, you can have more than one. Port separated by a space. Default is 3493. You may not want to open your LAN IP here.

LISTEN 192.168.1.100 3493
LISTEN 127.0.0.1 3493

Edit /etc/nut/upsd.users

Add this to create a user that can read UPS stats.

[local_mon]
      password = mysupersecretpass
      upsmon master

Edit /etc/nut/upsmon.conf

Add your UPS to the bottom to be monitored

MONITOR cyberpowerb@localhost 1 local_mon mysupersecretpass master

(optional) You may want to change polling to be less frequent

POLLFREQ 15

(optional) If your UPS goes stale, change when it assumes it is dead

DEADTIME 30

(optional) I stopped the computer from turning off the UPS on reboot by adding "no" or anything else to the end

POWERDOWNFLAG /etc/killpowerno

Restart NUT Services

This is for systemctl based distros of course.

systemctl restart nut-driver
systemctl restart nut-server
systemctl restart nut-monitor

Query NUT Server for info

This will poll the ups for available monitoring stuff.

upsc cyberpowerb@localhost

Home Assistant UPS Energy Tracking

Add a NUT UPS to Home Assistant

Log into your Home Assistant web interface.

  • Configuration > Integrations > Add Integration
  • Find or search for NUT in the list (Network UPS Tools) and add
  • Enter your server information we created above
    • Hostname is the ip of the server in the LISTEN line usually
    • User is local_mon (from upsd.users)
    • Pass is mysupersecretpass (upsd.users)
  • For my UPS I track sensor.cyberpowerb_load in the list of sensors to enable
    • My Sensor shows up as a load percentage (or really an integer 0-100)

Edit configuration.yaml

This is where you may have to edit things to your needs. My UPS doesn't have a Watt reading. I take the max load of the UPS which is 600W and multiply it by the percentage given me by sensor.cyberpowerb_load. Note, my load percentage is actually an integer 0-100. To make up for this I multiply by 6 instead of 600 for my watts.

template:
  - sensor: 
      - name: UPS Office usage
        unit_of_measurement: kW
        state: "Template:States('sensor.cyberpowerb load')"
        state_class: measurement
        device_class: power