本帖最后由 honry 于 2018-7-28 09:10 编辑
前言
一直想把UPS信息接入到homeassistant中,但是我的UPS是Eaton,HA中只有APC的接入,经过查询,我的UPS支持开源的UPS管理软件(Network UPS Tools),而HA的插件中是有NUT的,可直接调用
在进行以下操作时请先查询自己的UPS是否在NUT的支持范围内(也就是说只要nut支持的,都可以接入到HA),支持列表https://networkupstools.org/stable-hcl.html 比如我的UPS具体型号并没有在此列表中,那么可以看一下类似的型号有没有(没有9px但是有5px)
经验分享
本文中UPS型号为:Eaton 9PX 1000i RT 2U,通过USB连接到Debian系统中,此机器IP为:192.168.2.5,Homeassistant所在机器IP为192.168.2.6,下面开始接入步骤
1.Debian系统中安装nut服务器
apt-get update
apt-get install nut
复制代码
2.查看UPS
# lsusb
Bus 002 Device 001: ID 1d6b:0003 Linux Foundation 3.0 root hub
Bus 001 Device 002: ID 0463:ffff MGE UPS Systems UPS
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
复制代码
可以看出我的UPS是第二个设备,已连接的UPS Vendor ID : 0463,Device ID : ffff后面可能会用到
3.配置NUT
因为UPS用的是USB连接,首先需要安装USB驱动
打开/etc/nut/ups.conf可直接在文件尾部增加
[eaton]
driver = usbhid-ups
port = auto
desc = "Eaton 9PX 1000i RT 2U"
vendorid = 0463
复制代码
4.启用USB驱动
# upsdrvctl start
Network UPS Tools - UPS driver controller 2.6.4
Network UPS Tools - Generic HID driver 0.37 (2.6.4)
USB communication driver 0.32
Can't claim USB device [0463:ffff]: could not detach kernel driver from interface 0: Operation not permitted
Driver failed to start (exit status=1)
复制代码
如果你不是用root账号执行的话会出现上述错误,需要新建文件
vim /etc/udev/rules.d/90-nut-ups.rules
复制代码
文件内容为(其中idVendor和idProduct的值需要与第一步查看到的一致)
# Eaton 9PX 1000i RT 2U
ACTION=="add", SUBSYSTEM=="usb", ATTR{idVendor}=="0463", ATTR{idProduct}=="ffff", MODE="0660", GROUP="nut"
复制代码
保存后重启udev
service udev restart
复制代码
再次启动,本文中在启动过程中再次报错(预料之中,因为我直接使用的是root)
# upsdrvctl start
Network UPS Tools - UPS driver controller 2.7.4
Network UPS Tools - Generic HID driver 0.41 (2.7.4)
USB communication driver 0.33
Can't claim USB device [0463:ffff]: could not detach kernel driver from interface 0: Operation not permitted
Driver failed to start (exit status=1)
复制代码
查看USB位置
# lsusb
Bus 002 Device 001: ID 1d6b:0003 Linux Foundation 3.0 root hub
Bus 001 Device 002: ID 0463:ffff MGE UPS Systems UPS
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
复制代码
chmod 0666 /dev/bus/usb/[bus number]/[device number]
复制代码
本文中USB位置001 002 即
chmod 0666 /dev/bus/usb/001/002
复制代码
修改权限后再次执行
# upsdrvctl start eaton
Network UPS Tools - UPS driver controller 2.7.4
Network UPS Tools - Generic HID driver 0.41 (2.7.4)
USB communication driver 0.33
Using subdriver: MGE HID 1.39
复制代码
已成功连接UPS
如果ups和ha连接在同一台服务器,理论上来说到此已结束,可直接在ha中增加配置,本文中不是同一台,所以还需要进行设置
5.NUT Server设置
修改/etc/nut/nut.conf
数据服务器配置/etc/nut/upsd.conf
LISTEN <your local ip> 3493
本文中
LISTEN 192.168.2.5 3493
复制代码
查看nutserver状态并设置开机
systemctl status nut-server
systemctl enable nut-server
复制代码
启动nutserver
systemctl start nut-server
复制代码
6.NUT Client设置
切换到192.168.2.6中,安装NUT客户端
apt-get install nut-client
复制代码
修改/etc/nut/nut.conf
测试能否连通:
upsc <UPS名称>@<UPS服务器IP>,本文中:
# upsc [url=mailto:[email protected] ][email protected] [/url]
Init SSL without certificate database
battery.capacity: 7.00
battery.charge: 100
battery.charge.low: 20
battery.charge.restart: 1
battery.charger.status: resting
battery.energysave.delay: 300
battery.protection: yes
battery.runtime: 4458
battery.runtime.low: 180
battery.type: PbAc
device.mfr: EATON
device.model: Eaton 9PX 1000i RT 2U
device.serial: GA10G28008
device.type: ups
driver.name: usbhid-ups
driver.parameter.pollfreq: 30
driver.parameter.pollinterval: 2
driver.parameter.port: auto
driver.parameter.synchronous: no
driver.parameter.vendorid: 0463
driver.version: 2.7.4
driver.version.data: MGE HID 1.39
driver.version.internal: 0.41
input.bypass.current: 0.00
input.bypass.frequency: 50.0
input.bypass.voltage: 221.3
input.frequency: 50.0
input.frequency.nominal: 50
input.voltage: 221.7
input.voltage.nominal: 230
outlet.1.autoswitch.charge.low: 0
outlet.1.current: 0.10
outlet.1.delay.shutdown: 999999
outlet.1.delay.start: 3
outlet.1.desc: PowerShare Outlet 1
outlet.1.id: 1
outlet.1.power: 20
outlet.1.powerfactor: 0.00
outlet.1.realpower: 0
outlet.1.status: on
outlet.1.switchable: yes
outlet.2.autoswitch.charge.low: 0
outlet.2.current: 0.50
outlet.2.delay.shutdown: 999999
outlet.2.delay.start: 6
outlet.2.desc: PowerShare Outlet 2
outlet.2.id: 2
outlet.2.power: 110
outlet.2.powerfactor: 82.00
outlet.2.realpower: 90
outlet.2.status: on
outlet.2.switchable: yes
outlet.current: 0.00
outlet.desc: Main Outlet
outlet.id: 0
outlet.power: 20
outlet.powerfactor: 100.00
outlet.realpower: 20
outlet.switchable: no
output.current: 0.60
output.frequency: 50.0
output.frequency.nominal: 50
output.powerfactor: 0.79
output.voltage: 230.0
output.voltage.nominal: 230
ups.beeper.status: enabled
ups.date: 2012/03/29
ups.delay.shutdown: 20
ups.delay.start: 30
ups.efficiency: 81
ups.firmware: 00.01.3501
ups.load: 14
ups.mfr: EATON
ups.model: Eaton 9PX 1000i RT 2U
ups.power: 140
ups.power.nominal: 1000
ups.productid: ffff
ups.realpower: 110
ups.realpower.nominal: 1000
ups.serial: GA10G28008
ups.shutdown: enabled
ups.start.auto: yes
ups.start.battery: yes
ups.start.reboot: yes
ups.status: OL
ups.temperature: 31.9
ups.test.interval: 2592000
ups.test.result: Done and passed
ups.time: 04:37:38
ups.timer.shutdown: 0
ups.timer.start: 0
ups.type: online
ups.vendorid: 0463
复制代码
可以看到UPS的所有数据,如果只是接入HA的话,至此已结束,可以去配置HA
7.Homeassistant设置
可以参照HA官方插件说明https://www.home-assistant.io/components/sensor.nut/
本文中:
sensor:
- platform: nut
name: Eaton
host: 192.168.2.5
alias: eaton
port: 3493
resources:
- ups.status
- battery.charge
- battery.runtime
- ups.beeper.status
- ups.mfr
- ups.model
- ups.load
- input.voltage
- ups.temperature
复制代码
获取的可以是上面执行upsc [email protected] 查看到的所有传感器。
添加后到HA的设备中查看传感器名称
我的相对比较统一,所有传感器中都带有eaton,对这些传感器进行分组:
UPS:
view: no
name: Eaton UPS
entities:
- sensor.eaton_ups_temperature
- sensor.eaton_battery_charge
- sensor.eaton_battery_runtime
- sensor.eaton_beeper_status
- sensor.eaton_input_voltage
- sensor.eaton_load
- sensor.eaton_manufacturer
- sensor.eaton_model
- sensor.eaton_status_data
复制代码
最终效果
后面还有个性化名称,时长默认是S,转换成时分秒,HA控制UPS开等等,本文不进行展示(其实是我不会)
至此本文全部结束,希望能起到抛砖引玉的作用,各位大神进一步开发出新玩法。
参考文档:
https://networkupstools.org/stable-hcl.html
https://askubuntu.com/questions/113994/trouble-starting-network-ups-tools-with-a-eaton-3s-ups
https://gist.github.com/clungzta/2b5f737f006e1530cc59
本文同时发布在个人博客https://honry.net/eaton-ups-in-homeassistant.html