esphome / feature-requests

ESPHome Feature Request Tracker
https://esphome.io/
417 stars 26 forks source link

Generic UPS monitoring - NUT implementation #1785

Open nagyrobi opened 2 years ago

nagyrobi commented 2 years ago

Describe the problem you have/What new integration you would like Network UPS Tools is a respected project with great experience in handling various power devices, such as uninterruptible power supplies, power distribution units, solar controllers and servers power supply units.

The whole project is written in C. Best part is that it supports a plethora of UPS types connected via GPIO (contact closure) or RS232 serial (most of them). This is all perfect to be used with microcontrollers supported by ESPHome.

NUT is mostly written to run on a Linux OS with the concept of various processes handled as standard separate Linux "daemons" which can trigger various "scripts" based on event information gathered from the UPS device.

My suggestion is in order to avoid reinventing the wheel, to cherry-pick from the "driver" part of it (which is actually not a real driver, just a message parser) the models/protocols which can be usable in ESPHome and implement them as sensors (mostly input/output voltage/current, power, load percentage, battery percentage, charging status etc). Some UPSes allow to be controlled (typically turning OFF/ON loads, trigger battery checks etc.) these could be just implemented as switches/buttons. Since NUT is written in C, it's very likely that code porting could be feasible.

We don't need the rest of NUT (like CGI web GUI; monitoring daemons etc), as ESPHome already has powerful automation features which can be used with sensors/switches, and more advanced stuff can be done the usual way via HA system.

We only need the sensors/switches based on protocols seen in NUT drivers section.

Please describe your use case for this integration and alternatives you've tried: N/A

Additional context For example APC: https://networkupstools.org/protocols/apcsmart.html https://kirbah.github.io/apc-ups/Smart-protocol/

nagyrobi commented 2 years ago

https://github.com/ssieb/custom_components/tree/master/components/apcups

tschaerni commented 1 year ago

https://github.com/ssieb/custom_components/tree/master/components/apcups

That only supports Schneider Electronic (APC) UPS, NUT supports a much wider range of UPS systems

TommySharpNZ commented 8 months ago

Yeah this would be pretty cool if ESPHome could talk to my UPS....

johnboiles commented 8 months ago

I'm mildly interested in spelunking around this. I think for ease of connection with modern UPS devices I'd probably go at it as a USB host implementation so that we wouldn't need RS232. Then you could potentially take an off-the-shelf ESP32-S3 dev board with USB OTG and plug in a USB cable and be good to go.

I believe many of these USB UPS interfaces show up as a HID device, and there's already esp-idf example code for hooking up with HID devices. I'll pick up a dev board for this and see what comes out the USB port when I hook it up.

tschaerni commented 8 months ago

I believe many of these USB UPS interfaces show up as a HID device, and there's already esp-idf example code for hooking up with HID devices. I'll pick up a dev board for this and see what comes out the USB port when I hook it up.

Sadly, the USB-HID implementation is wonky at best on many of those UPS'. Mine for example just randomly becomes unresponsive, and the host which runs NUT then, as a first action before attempting shutdown, restarts the driver.

You can probably find a lot of useful information about what kind of data is being sent in the NUT documentation, here is a first start I suppose: https://networkupstools.org/docs/man/usbhid-ups.html

nagyrobi commented 8 months ago

HID is not enough, because older, and also newer but bigger UPSes still support serial which is a very stable connection method, and all MCUs support some kind of serial connection at hardware level.

johnboiles commented 8 months ago

Sadly, the USB-HID implementation is wonky at best on many of those UPS'

That is a bummer. Would be no fun to need a bunch of kludge-y workarounds. Maybe a watchdog + reboot of the ESP32 could be a good catch-all. At least that's easier on an embedded platform than a full Linux machine.

HID is not enough

I don't disagree! Having both is ideal! My UPS (Cyberpower CP850PFCLCD) does not have serial though so I can't help much with that.

nagyrobi commented 8 months ago

It was just a note for component development, not make it exclusive to hid.

gagnfra commented 7 months ago

Someone got it somewhat working https://github.com/ludoux/esp32-nut-server-usbhid

johnboiles commented 7 months ago

Good find!

On Tue, Feb 20, 2024 at 11:23 AM Francis @.***> wrote:

Someone got it somewhat working https://github.com/ludoux/esp32-nut-server-usbhid

— Reply to this email directly, view it on GitHub https://github.com/esphome/feature-requests/issues/1785#issuecomment-1953424779, or unsubscribe https://github.com/notifications/unsubscribe-auth/AABVN7HIZN65S33VMH7UESLYUQJMBAVCNFSM52I2WXA2U5DIOJSWCZC7NNSXTN2JONZXKZKDN5WW2ZLOOQ5TCOJVGM2DENBXG44Q . You are receiving this because you commented.Message ID: @.***>

mcarbonneaux commented 7 months ago

USB HID implementation first while cover many of the recent smart ups. RS232 can be mostly done i think with uart module.