Difference between revisions of "Home Assistant - NUT UPS Energy Tracking"

From ETCwiki
Jump to navigationJump to search
m
 
(4 intermediate revisions by the same user not shown)
Line 1: Line 1:
 
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.
 
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.
 +
 +
Platform: Linux (Debian)
 +
Skill level: Medium
 +
Time required: 30-90 minutes
  
 
== Network UPS Tools ==
 
== Network UPS Tools ==
Line 18: Line 22:
 
       port = auto
 
       port = auto
 
       desc = "Cyberpower CP1000 B"
 
       desc = "Cyberpower CP1000 B"
       pollfreq = 30
+
       pollfreq = 15
 +
      pollinterval = 15
  
 
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)
 
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)
Line 38: Line 43:
 
  MONITOR cyberpowerb@localhost 1 local_mon mysupersecretpass master
 
  MONITOR cyberpowerb@localhost 1 local_mon mysupersecretpass master
  
(optional) You may want to change polling to be less frequent
+
(optional) If your UPS goes stale, change when it assumes it is dead
  POLLFREQ 15
+
  DEADTIME 25
  
(optional) If your UPS goes stale, change when it assumes it is dead
+
(optional) If your UPS goes stale, add or change this line
  DEADTIME 30
+
  MAXAGE 25
  
 
(optional) I stopped the computer from turning off the UPS on reboot by adding "no" or anything else to the end
 
(optional) I stopped the computer from turning off the UPS on reboot by adding "no" or anything else to the end
Line 70: Line 75:
 
** My Sensor shows up as a load percentage (or really an integer 0-100)
 
** My Sensor shows up as a load percentage (or really an integer 0-100)
  
=== Edit configuration.yaml ===
+
=== Create template to calculate watts ===
 
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.
 
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:
 
  template:
 
   - sensor:  
 
   - sensor:  
 
       - name: UPS Office usage
 
       - name: UPS Office usage
 
         unit_of_measurement: kW
 
         unit_of_measurement: kW
         state: "{{ states('sensor.cyberpowerb_load') | float * 6 / 1000 }}"
+
         state: "<nowiki>{{ states('sensor.cyberpowerb_load') | float * 6 / 1000 }}</nowiki>"
 
         state_class: measurement
 
         state_class: measurement
 
         device_class: power
 
         device_class: power
 +
 +
=== Create a calculation of energy usage ===
 +
This interprets your Watt sensor we just made every so often (30 sec for me) and calculates the total energy used in Wh or kWh. This will not be an exact number, but it should be pretty close.
 +
 +
sensor:
 +
  - platform: integration
 +
    source: sensor.ups_office_usage
 +
    name: Office UPS Used
 +
    method: left
 +
    round: 6
 +
 +
=== Track Energy of UPS in Home Assistant ===
 +
Bunch of options here.
 +
# Configuration > Energy > Add the sensor.ups_office_usage
 +
# Dashboard > Add Entities card > add our sensors
 +
 +
== Troubleshooting ==
 +
To test your setup, open a terminal and type
 +
upsc cyberpowerb
 +
or
 +
upsc cyberpowerb@192.168.1.101
 +
 +
'''Connection Refused'''
 +
Check your firewall. Check your upsd.conf for syntax. No colon to separate the listen port in upsd.conf. Restart your NUT services like above.
 +
 +
 +
== External Links ==
 +
* https://networkupstools.org/ - Network UPS Tools
 +
* https://www.home-assistant.io/integrations/nut/ - Home Assistant - Network UPS Tools (NUT)
 +
* https://raspberrypi.stackexchange.com/questions/66611/nut-cyberpower-data-stale - Dealing with stale data
 +
* https://www.home-assistant.io/integrations/integration/ - Home Assistant - integrations and what method: left means
 +
* https://www.reddit.com/r/homeassistant/comments/p1z19p/want_to_add_ups_to_the_new_energy_monitoring/ - Got me through some rough times learning to pull sensor values
 +
 +
[[Category:Home Assistant]]

Latest revision as of 19:29, 24 October 2021

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.

Platform: Linux (Debian)
Skill level: Medium
Time required: 30-90 minutes

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 = 15
     pollinterval = 15

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) If your UPS goes stale, change when it assumes it is dead

DEADTIME 25

(optional) If your UPS goes stale, add or change this line

MAXAGE 25

(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)

Create template to calculate watts

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: "{{ states('sensor.cyberpowerb_load') | float * 6 / 1000 }}"
        state_class: measurement
        device_class: power

Create a calculation of energy usage

This interprets your Watt sensor we just made every so often (30 sec for me) and calculates the total energy used in Wh or kWh. This will not be an exact number, but it should be pretty close.

sensor:
  - platform: integration
    source: sensor.ups_office_usage
    name: Office UPS Used
    method: left
    round: 6

Track Energy of UPS in Home Assistant

Bunch of options here.

  1. Configuration > Energy > Add the sensor.ups_office_usage
  2. Dashboard > Add Entities card > add our sensors

Troubleshooting

To test your setup, open a terminal and type

upsc cyberpowerb

or

upsc cyberpowerb@192.168.1.101

Connection Refused Check your firewall. Check your upsd.conf for syntax. No colon to separate the listen port in upsd.conf. Restart your NUT services like above.


External Links