APC UPS Backup - Now just needs a plugin

There is no attachment. ;D

  • Garrett
There is no attachment.

Everyone’s a critic!

Downloading now. However I will not be able to test until I get home and swap out my ups that vera is on with an apc unit. However, I’ll have a peak at the code. :slight_smile:

  • Garrett

Excellent, congratulations on your plugin. I think you’ve got all the necessary bits in place, now it’s just a matter of tweaking and adding features.

For the auto-install script that you want to make, how about something like this?

  1. Add a function to your Lua code that writes a shell script to /tmp, and then calls os.execute() on that script.
  2. Add a button to the Control tab in the D_APC.json file (“Install”) and a matching in the I_APC.xml file, so that the above function can be invoked from the Control tab.
  3. In the Lua startup code, check if the installation has been done, and if it hasn’t been, exit the startup with a message that will display to the user in the top right of the dashboard.

The shell script will need all kinds of paranoia checks. Let me know if you need help with it.

Hi Futzle,

Can you show me how to do number 3?

Kristopher

Excellent job! Thanks for sharing your code.

local currentMode = 0
	  if (currentStatus == "ONLINE") then
		if (tonumber(currentLinev) > 0) then
			currentMode = 50
		else
			currentMode = 100
		end
	  end

I’m not sure that the line

currentMode = 100

is reachable. It boils down to the question which values the STATUS variable can contain. Unfortunately, I couldn’t find a definitive answer to that question.

You can test for the existence of a file (say, your /etc/init.d/apcupsd script, or the Kernel module) with Lua code like this:

-- Zero if file exists; nonzero if it doesn't.
local exitStatus = os.execute('sh -c "[ -f /etc/init.d/apcupsd ]"')

The plugin’s startup function should end with

return true

to indicate success. To indicate plugin failure, return a list starting with false:

return false, "APC files not installed", "APC UPS plugin"

The other two strings find their way to the dashboard.

file, msg = io.open(name, "r")
if not file then print(msg) end

If you want to be safe, always “[tt]return[/tt]” all three values, even in the [tt]true[/tt] case. There were issues with intermediate UI4 builds that would freak out if you returned 1 or 2 values… in some cases.

Also, don’t forget to [tt]close[/tt] the file after using the test that @Ap15e put in. It’s the approach I’d take, since it’s lighter weight than forking a child process (if you can avoid it)

is reachable. It boils down to the question which values the STATUS variable can contain. Unfortunately, I couldn't find a definitive answer to that question.

I’ll double check. If you have no line on the system it can still be online, in which case you’re running off just battery. I’ll work on this a little though.

My plan was to have it work like this

0 = no battery, no line
50 = line
100 = battery, no line

Kristopher

IIRC if your’re running on batteries, STATUS is ONBATT.

Would you mind adding

        if value ~= nil
         then
          luup.variable_set("urn:kristopher-us:serviceId:APC1", tostring( identifier ) .. '_VALUE', tostring ( value ), PARENT_DEVICE )
         end

to your plugin? Reason: The dataMine plugin doesn’t support regular expressions to extract the numbers from the status variables.

AUC (APC UPS Companion) is available from [tt]http://forum.micasaverde.com/index.php?topic=7989.0[/tt].

I followed the instructions for adding the USB support and daemon to Vera. Everything worked fine until I got to the last step:

root@MiOS:/etc/apcupsd# apcaccess status
Error contacting apcupsd @ localhost:3551: Connection refused

In the apcupsd.conf file I changed the cable type to USB and UPS type to usb and left device blank. I didn’t make any other changes to the conf file.

Any idea what could be wrong?

Is the apcupsd running?

It must not have been. I restarted it and was able to get some status…but some fields are 0 that shouldn’t be (like line voltage - see the data dump at the bottom).

The display on the Vera webpage isn’t working - it has nil for the values. (see attachment)

When I go into the properties, APCUPSInformation has bad data. (see attachment)

root@MiOS_18558:/etc/apcupsd# apcaccess status
APC : 001,043,1048
DATE : Tue Oct 04 17:35:45 MST 2011
HOSTNAME : MiOS_18558
VERSION : 3.14.7 (1 August 2009) unknown
UPSNAME : ups1
CABLE : USB Cable
MODEL : Back-UPS CS 350
UPSMODE : Stand Alone
STARTTIME: Tue Oct 04 17:04:45 MST 2011
STATUS : ONLINE
LINEV : 000.0 Volts
LOADPCT : 34.0 Percent Load Capacity
BCHARGE : 100.0 Percent
TIMELEFT : 30.8 Minutes
MBATTCHG : 5 Percent
MINTIMEL : 3 Minutes
MAXTIME : 0 Seconds
OUTPUTV : 115.0 Volts
SENSE : Medium
DWAKE : 000 Seconds
DSHUTD : 000 Seconds
LOTRANS : 000.0 Volts
HITRANS : 136.0 Volts
RETPCT : 000.0 Percent
ITEMP : 29.2 C Internal
ALARMDEL : Always
BATTV : 13.5 Volts
LINEFREQ : 59.0 Hz
LASTXFER : Low line voltage
NUMXFERS : 0
TONBATT : 0 seconds
CUMONBATT: 0 seconds
XOFFBATT : N/A
SELFTEST : NO
STATFLAG : 0x07000008 Status Flag
SERIALNO : 4B0944P40555
BATTDATE : 2009-11-01
NOMOUTV : 000 Volts
NOMINV : 120 Volts
NOMBATTV : 12.0 Volts
NOMPOWER : 210 Watts
FIRMWARE : 805.q8.D USB FW:q8
APCMODEL : Back-UPS CS 350
END APC : Tue Oct 04 17:35:51 MST 2011

@Kristopher

Great job! I have an APC Back-UPS ES 550 and just got your plugin working on my Vera2. Looks very nice and I look forward to seeing future tweaks you may do. This is really handy for those of us that don’t have a PC or server on 24x7.

@Ap15e

I like your companion tool a lot. Lots of info in a compact display. (I just wish there was a way to see the info on my iphone. I’m using the free smartphone tool from MCV.)

@ChrisAZ

I had the same results you’ve displayed initially. Make sure you have Kristopher’s APC plugin working correctly, first. (See his apc.zip file and install details further up in this thread). It should display the load level and remaining time. When I installed Ap15e’s companion (AUC), I put the wrong device number in the APCUPSDevID field on the AUC Advanced tab. Once I put the device number for the APC device into the AUC’s APCUPSDevID field, then AUC worked as expected.

Kristopher,

If the [tt]apcupsd[/tt] dies, [tt]apcaccess[/tt] returns [tt]“Error contacting apcupsd @ localhost:3551: Connection refused”[/tt] and your code errors out with [tt]“function refreshCache failed [string “…”]:63: attempt to concatenate local ‘currentTimeleft’ (a nil value)”[/tt] - and doesn’t update [tt]LastUpdate[/tt] and AUC is able to display a warning message. :slight_smile:

If you’re going to fix it, please don’t update [tt]LastUpade[/tt] if [tt]apcaccess[/tt] returns an error.

AUC (APC UPS Companion) V0.2 beta is available from [tt]http://forum.micasaverde.com/index.php?topic=7989.0[tt].

Is anyone using this on V3/UI5?

JOD.