New apcupsd Hass.io add-on

Hi,
apcupsd via usb is not working anymore for me. It worked before the upgrade from 108.3 to 108.5 or the upgrade to HassOS 3.13 from 3.12.
Errormessage in the log is

addon_a722577e_apcupsd: 500 Server Error: Internal Server Error (“error gathering device information while adding custom device “/dev/usb/”: no such file or directory”)

I checked an /dev/usb does not exist anymore. I checked Hardware see only this output:

serial:
    /dev/ttyAMA0
input:
disk:
    /dev/mmcblk0p3
    /dev/mmcblk0p5
    /dev/mmcblk0p1
    /dev/mmcblk0p6
    /dev/mmcblk0p7
    /dev/mmcblk0
    /dev/mmcblk0p4
    /dev/mmcblk0p2
    /dev/mmcblk0p8
gpio:
    gpiochip0
    gpiochip504
audio:
    0:
        name: bcm2835_alsa - bcm2835 ALSA
        type: ALSA
        devices:
            [object Object]
            [object Object]
            [object Object]

Running it on RPI4. As I said worked last weekend. No idea what happend. If somebody can help that would be great.
Reolaced my RPI with a new one I had lying arround and no change. Same issue with fresh installed Hass on the RPI4

Hi @malkie,

Welcome to the community. It looks like your usb serial device might have got renamed during the OS upgrade. I’m fighting this now with Virtualbox in that every time I have to restart the VM, the USB ports get shuffled around. The serial device at the top of your list could be the UPS. Try unplugging the USB cable and see if that goes away. If it does, the add-on likely had a symlink to that device that got lost during the OS upgrade.

Hopefully the developer will jump in and provide guidance. Otherwise, I’ll take a look at the add-on source code a little later this afternoon.

EDIT: Found this: http://www.lolorpi.com/2019/01/setting-up-a-fixed-address-for-a-usb-device-in-linux/

Ken

1 Like

Hi Ken,
I got it finally solved. I had really no idea anymore and connected my windows notebook to the APC and installed the APC Software. It did not recognize the APC as well.
A bit googleing later and another shutdown of my system and a “reboot” of the APC (this time including the breaker at the back) it works again. Relly seems like the Apc system crashed.

Have a nice weekend

Malkie

One thing I had been missing in my previous question was that I needed to paste all of the JSON in the plugin’s UI at once in order for it to auto-convert it to YAML.

The other problem was that I’m off-grid, and therefore on battery a lot of the time. The reported battery state was automatically triggering an immediate shutdown of my HA box before I could mess with it or access the docker container. Reading through the Configuration Directives used during Power Failures section of the apcupsd manual, I thought this config would prevent that:

  "name": "APC UPS",
  "cable": "usb",
  "type": "usb",
  "device": "",
  "extra": [
    {"key": "MINUTES", "val": "-1"},
    {"key": "BATTERYLEVEL", "val": "-1"},
    {"key": "TIMEOUT", "val": "0"},
  ]
}

Which in yaml translated to:

name: APC UPS
cable: usb
type: usb
device: ''
extra:
  - key: MINUTES
    val: '-1'
  - key: BATTERYLEVEL
    val: '-1'
  - key: TIMEOUT
    val: '0'

That fixed 3 of the 4 automatic shutdown cases. The last one was resolved by the exit 99 trick in /share/apcupsd/scripts/doshutdown. And if I’m understanding it correctly, that script itself would also have obviated the need to set the other extra config options.

So I think it’s mostly working as expected now, but I do have a couple questions (that I think may not be possible) about how I’d hoped it would work:

  1. Is there a way to use this as a “switch”, to completely turn off the APC? Might there be a method via the Smart Protocol in the manual? I see there’s a “Turn on UPS” command. Edit: Smart Code K is Shutdown (with grace period), Z is immediate Shutdown (no grace period) and S is Soft Shutdown (will resume with mains power). Any examples of that, or the other smart codes, being implemented in HA?

  2. Is there a way to calibrate the battery sensor readings in a meaningful way? I see mentions of battery constants and calibration for capacity, but I’m not sure those are the solution. For example, when my measured (via multi-meter) battery voltage is 12.1, the UPS reports 10.9V1. With that much difference, I’m hesitant to attempt to use the sensor value (battv on my Back-UPS 700) for automations.

Note:
1 It makes sense that the UPS is initiating a forced shutdown (doshutdown) because at 10.9V, it reasonably thinks the battery is “exhausted”. It also makes sense that I was baffled because 12.1V is still a healthy 50% charge.

Glad to hear you go it figured out. I’ve actually moved a couple of months ago, and don’t currently have HA running, so it’s hard to troubleshoot some things.

I’m not sure the Smart Protocol is the same for USB UPSes. I tried writing directly to the /dev/usb/hiddev0 device with bash, but received errors. You’d likely have to write a program using USB libraries…
Instead, take a look at the apcupsd man page. You’ll see there’s flags there (--killpower, --power-off, etc) that you can use to accomplish what you want. In general, this command is called by a system init script upon shutdown by the UPS. Since this is running in a container, that’s not going to happen and you can call it directly. Just be aware it may require some clever coordination so that you don’t shut down the host before the command can run.

I don’t know much about battery calibration with APC units, though I was able to find this article. I’d suggest starting with that.

Also, make sure you’re measuring accurately. This means measuring the battery with your multimeter while it’s still connected to the system. Beyond that, I don’t have much advice.

1 Like

Awesome! That Ubuntu man page was indeed the answer. Got it to work on the first test from terminal inside the container…

apcupsd -k 

…returns the message “UPS… appears to support BackUPS style hibernate”, and then switches off a few seconds later. It looks like the message means the TIMEOUT is invoked, rather than the more elaborate (eh hem, smarter?) shutdown process of SmartUPS.

I’m not yet sure how to invoke this from within HA, but I’m on the right path. Thank you.

I missed the part about triggering it from HA. Honestly, I can’t think of any way to do that, as there’s no way to trigger something within the add-on.

I kind of expected it would be outside the scope of the add-on. If I do figure it out, I’m still lacking a command to turn the APC on (like long-pressing the power button). Is there something I missed (again), or is that not available outside of the Smart Protocol (which probably can’t be accessed over USB even if my UPS has it, which it may not)?

I can’t find any information if that’s possible. It might be something to ask on the apcupsd mailing list.

Multiple ones really would be neat to have a “dashboard” of all the UPSs across the network. In my case I don’t need Hass.io to care about the UPS at all (my hypervisor will shut down the VMs and itself if it goes to battery) but being able to have a log of what all of them are doing would be amazing to analyze and monitor in one place.

The big “gotya” for me getting the first one working was I didn’t realize the ‘device’ line is different from normal apcupsd, I was putting ‘192.168.1.1:3551’ like what seems to work properly on my various Linux install, but instead I had to put just ‘192.168.1.1’ in the hass.io addon configuration without the port.

You might look into documentation for ‘apctest’, I don’t know that there is a function to command it back on again in apcupsd but if there is, it’s probably in the apctest app. In my experience, the “kill” is usually used to power off the UPS after graceful shutdown and it powers back on by itself when mains power returns.

Is there a way to adjust how rapidly this responds to events?

I tried pulling the plug on my UPS and it was nearly a full minute (about 55 seconds) before the status updated showing line voltage 0.0V and similarly long when I plugged it back in. And the status for ONLINE SLAVE/ONBATT SLAVE takes a full 90 seconds to update.

I tried to add the expected key to make it react faster but it told me its invalid.
Failed to save addon configuration, not a valid value for dictionary value @ data['options']. Got {'name': 'APC UPS', 'cable': 'smart', 'type': 'net', 'device': '192.168.1.1', 'extra': [{'key': 'ONBATTERYDELAY', 'val': 1}]}

Every other system I have that monitors the same UPS over the network with apcupsd is able to tell within ~5 seconds at the longest that it’s lost power. I don’t understand why this isn’t updating?

@mmiller7

I might look at adding multiple UPS support eventually, but I just don’t have the time to do it now.

A few things… Your config is invalid because you used 1 as the value. You need to use "1" (i.e. wrap the value in quotes.) This is because the add-on config parser doesn’t support multiple types.

This add-on just runs the apcupsd process. You’ll need to figure out if it’s apcupsd that’s slow to respond to status changes, or Home Assistant that’s slow to update from apcupsd.

I would try triggering the event, then using apcaccess (i.e. run apcaccess <your HassOS ip>) immediately after to see if apcupsd sees the new status. If it doesn’t then apcupsd probably needs additional configuration options. If apcaccess is showing the updated status, but Home Assistant isn’t, you need to investigate why it’s taking Home Assistant so long to update. (Are you running on an older Pi or is the CPU maxed out?)

Ok - it does seem to react in ~5 seconds looking at apcaccess -h <hassio_name> watching the stats, like I see with other hosts and VMs.

I’m running it on a x86-64 server (Intel® Xeon® E-2176G CPU @ 3.70GHz) with 8GB of RAM for Hassio (HassOS using the Intel-NUC image), I don’t know exactly what I’d look at. Nothing else is slow to update on my dashboard or in the developer states display.

It appears the issue is that the apcupsd home assistant component polls at most every 60 seconds.

Here’s a thread discussing it. One idea there would be to use apcupsd scripts to post to an MQTT and have HA monitor that for a quicker status update.

Oh wow, that’s quite a bummer. While some (like on-battery on-mains) could be done with the apcupsd scripts a lot of the metrics would probably require polling.

Seems like the interval should be configurable, with a beefy enough machine I don’t see why every 1s would be an issue. Since it already can import all the readings it seems silly to have to invent my own script to poll, parse, publish, then import, parse, and create sensors.

I agree. I just finished submitting a PR to add poll_interval as a configuration option. We’ll see how it goes.

1 Like

Hello,
I have latest HassOS 3.13 on fresh RPi4. I have difficulties to integrate UPS APC Back-UPS 950VA (BX950U-FR) with HA. As per basic “lsusb” I see it is visible on RPI:

# lsusb .
Bus 001 Device 001: ID 1d6b:0002
Bus 001 Device 003: ID 0451:16a8
Bus 001 Device 002: ID 2109:3431
Bus 002 Device 001: ID 1d6b:0003
Bus 001 Device 005: ID 051d:0002   <--------

however on Supervisor -> System -> Hardware tab it is not included.

Hardware 
serial:
/dev/ttyAMA0
/dev/ttyACM0
/dev/serial/by-id/usb-Texas_Instruments_TI_CC2531_USB_CDC___0X00124B0018E21116-if00
input:
disk:
/dev/mmcblk0
/dev/mmcblk0p8
/dev/mmcblk0p1
/dev/mmcblk0p5
/dev/mmcblk0p7
/dev/mmcblk0p3
/dev/mmcblk0p2
/dev/mmcblk0p6
/dev/mmcblk0p4
gpio:
gpiochip504
gpiochip0
audio:
0:
name: bcm2835_alsa - bcm2835 ALSA
type: ALSA
devices:
[object Object]
[object Object]
[object Object]

Log from Add-on:

May 21 21:22:32 a722577e-apcupsd syslog.info syslogd started: BusyBox v1.30.1
May 21 19:22:32 a722577e-apcupsd daemon.notice apcupsd[7]: apcupsd 3.14.14 (31 May 2016) unknown startup succeeded
May 21 19:22:32 a722577e-apcupsd daemon.info apcupsd[7]: NIS server startup succeeded

APCUPSD included in configuration.yaml:

# APC UPS
apcupsd:

Did anyone face any issue like this before? I turned off and unplug from AC main UPS but unfortunately issue is still present. Appreciate help, thank you.

EDIT: My Fix was to reinstall hass.os, it worked for 2nd time. Only concern is that this addon is checking APC status each 1 minute, it would be nice to have this value user configurable, lets say every 10 or 20 secs.

Can this be used with serial to USB cable?
I have old APC SU1400 but verry good battery that has serial port only.

Hey, great add on. I have it working fine with USB, however my UPS has a AP9630 network card in it. Firmware’s have all be updated and the network card even supports Modbus…

Anyway, I have installed the apcupsd_net version of this add-on to try to eliminate a USB cable going to the raspberry pi. What am I missing in the config file? I have the below config:


name: APC UPS
cable: ether
type: net
device: '192.168.10.7:apc:apc'
extra: []

I would like the add-on to communicate directly with the UPS and not require a server in-between.

Thanks for the help