[RELEASE] APC UPS Monitor Driver

Monitor your APC UPS devices using a Raspberry Pi or any PC (Windows or Mac) that can run apcupsd.

This basic driver monitors several parameters provided by the UPS.

Requirements:

  1. UPS Compatible with APCUPSD
  2. Raspberry Pi or PC/Mac to run apcupsd & web server components.
  3. Ability to carefully follow a long set of instructions. :slight_smile:

Difficulty Level: Hard

I know there has been some demand for this recently so I am releasing this as-is. Due to the complexity, and the ongoing development of HubConnect, I will only be able to offer limited support.

For those using Raspberry Pis I have tried to make the commands as simple as copy-and-paste whenever possible.

Please read and understand these instructions for proceeding.
https://github.com/shackrat/Hubitat/blob/master/UPS%20Monitor/Installation%20Instructions.txt

https://github.com/shackrat/Hubitat/blob/master/UPS%20Monitor/UPSMonitor.groovy

https://github.com/shackrat/Hubitat/blob/master/UPS%20Monitor/apc.php

Good luck!

6 Likes

I will not try, thanks anyway.

3 Likes

This is great. Thank you. I'll have to dig into it later in the month. I loaded up the driver to see what I could access via RM. I would ideally use this to post to [hubip]:8080/hub/shutdown when the battery time remaining was short, but I don't see a way to select batteryRuntime as a condition in RM. What do you suggest for that?

Unfortunately RM doesn't support the powerSource capability. You should be able to use WATO to subscribe to the powerSource attribute and/or timeRemaining attribute.

Or, if you want an instant shutdown, and your apcupsd device is on the same LAN, you can actually have apcupsd send a curl POST to any address.

How to do that is loosely covered in the instructions..

Add the following statement so the block looks as follows:

  onbattery)
  	echo "Power failure on UPS ${2}. Running on batteries." | ${WALL}
  	wget -q -O- "http://localhost/apc.php?a=notify&status=onbattery"
  ;;

You'd have to use a curl post instead.

1 Like

@srwhite, nice job!

I'm so glad you came back to HE. Your development skills along with so many others has made HE a fun and useful gadget.

I come to these forums 20-30 times a day just to see what y'all have come up with next.

Anyways, just ordered my Raspberry Pi to start tinkering.

Good morning sir,

I have this up and running on my Pi but getting an error in the Hubitat logs...

dev:13492019-06-11 07:58:01.267 errorjava.lang.NullPointerException: Cannot invoke method replace() on null object on line 177 (parse)

Also no details on the device page... Obviously I did something wrong... The command on the Pi "apcaccess status" brings back a perfect status update of the UPS... So apcupsd is working... I have Apache and PHP all working... All the tests work... It appears it is an error in the driver? Just to confirm, I setup a virtual device and use this driver correct?

I wish my UPS had more then one USB port on it for monitoring. I already have my NAS connected to the USB port to detect a power outage and if so, shuts my NAS down to prevent instability in my NAS and it's drives. I could see this being useful though for many.

If your NAS is a Qnap (and maybe Synology), you can try this driver. I've been using it for the past few weeks and have a rule to shutdown one of my hubs if the battery level falls below threshold.

1 Like

Ok, figured out the problem... In the groovy file it was looking for data in "battv" (Battery Voltage) which did not exist in the APCUPSD data from my UPS... It appears my UPS was sending that battery voltage data under "outputv" instead. When I changed that in the groovy file, everything worked like a charm!

Thanks shackrat! Awesome work!

I do have QNAP. Thanks! I’ll check this out!

Good morning... ShackRat, can you or anyone else give me some help with setting this up on a windows machine? I have APCUSD working and everything setup via my Hubitat... I'm using this solution in a few places with Raspberry Pi's, but in two locations, I want to drive it with windows machines that are connected to UPS's.

What I can't figure out is the "push" part on the windows machines... On windows, there is an apccontrol.bat file where the the apccontrol file is on the Pi... I've added the same lines as in the directions, but what do I do next?

How do I get it to "push" state changes?

Hi, I was planning on trying this out with my Raspberry Pi Zero-W which already has PiVPN and PiHole running on it. Can I run this alongside those? I know that PiHole uses the lightpd web server and I was wondering if it is possible to run two web servers, namely apache and lightpd, or if you know how to configure this APC monitoring device to work with the lightpd web server. Thank you for any guidance you can provide.

went to do this but the links to the GitHub files are dead all 404ed, could you repost ?

2 Likes

Hello @SuperDupe did you ever get this working on windows?
I have Apcupsd working, using my wamp server the apc.php module sends messages to the HE device, refresh works from HE device page,

Have it working see later post

but the status messages for onbattery, offbattery do not work when the UPS device changes status

I've tried the modifying apccontrol.bat with the wget as shown in the directions, but that does nothing. Also tried modifying the onbattery.vbs but that also does nothing. It did send a email message using the original apcupsd example code with my email credentials, so it is executing.

My sample onbattery.vbs

'///Comment out the next line when testing, then change back when done. ///
On Error Resume Next
Dim xmlhttp As New MSXML2.XMLHTTP, myurl As String
myurl = "http://localhost/yada/yada/apc.php?a=notify&status=onbattery"
xmlhttp.Open "GET", myurl, True
xmlhttp.Send
Set xmlhttp = nothing
'/// Exit with a 0 error level to ensure the apccontrol.bat continues ///
Wscript.Quit 0

No, I gave up... I couldn't get it to work...

I may be able to make this work for me by:
I have it working:

  1. continuing without the notifications, they send a status change without any other information
  2. sending the status information every 10 minutes, that contains all the UPS info including status and batteryRunTime remaining until UPS shutdown. That's all I need to do a graceful shutdown.
  3. Also the windows machine must be changed not to sleep

Update: Can't figure out how to get the Windows scheduler to execute the PHP job rather than just list it. This is really fugly. This means issuing a "refresh" every N minutes from the HE UPS device (or an RM)

Update2: have it working every 10 minutes from windows scheduler with the following command structure.
C:\Path\to php.exe file\php.exe -f C:\Path\to php file\file.php
Task set to wake machine, so sleep "should" be is OK.

Update 3:

  • Added second scheduled task to start wamp.exe, my local PHP server
  • Both tasks changed to start at "System Startup" without a user being logged in. wamp start delayed 2 minutes, php run delayed 3 minutes
  • rebooted system--it worked :fireworks: I'm ferklempt
1 Like

Here's how to execute the "wget" action trigger commands in a Windows machine.

  • In directory C:\apsupsd\etc\apcupsd\
    add the following scripts
    onbattery.vbs
    offbattery.vbs
    powerout.vbs

A sample of the onbattery.vbs script follows. Change "onbattery" to the respective command for the other scripts.

Change dir1/dir2 to directory housing apc.php

'///Windows testing: open a command window: winkey + R, enter Cmd, then execute: cscript C:"\apcupsd\etc\apcupsd\onbattery ///
'///Comment out the next line when testing, then change back when done. ///
On Error Resume Next
Dim xmlhttp
Set xmlhttp = createobject("MSXML2.ServerXMLHTTP")
xmlhttp.Open "GET", "http://localhost/dir1/dir2/apc.php?a=notify&status=onbattery" , False
xmlhttp.Send
Set xmlhttp = nothing
'/// Exit with a 0 error level to ensure the apccontrol.bat continues ///
Wscript.Quit 0

Perhaps someone more VBS proficient than me would be able and willing to improve and modify my VBS scripts, combining them with the apc.php logic, thereby eliminating the need to run a PHP server on Windows.

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.