Author Topic: APC UPS Backup - Now just needs a plugin  (Read 24805 times)

Offline Kristopher

  • Jr. Member
  • **
  • Posts: 54
  • Karma: +0/-0
APC UPS Backup - Now just needs a plugin
« on: September 14, 2011, 09:56:49 pm »
The new Vera3 comes with an integrated battery backup, which is awesome except your modem, all your z-wave devices, wifi, etc is still going to go down.  You need a real battery backup that will stay online long enough to fire off notifications that your net is down.

http://www.amazon.com/dp/B001BTWQDS/

I picked up an el cheapo APC UPS ES 450.  It has 4 battery backup ports and advertises 30 minutes in a power outage.  It has a funny USB to Ethernet cable (the Ethernet end hooks up to the APC), so you can control and monitor it from the Vera2/3.  However, the kernel device does not exist on the Vera and you have to install that.   You'll also need to install the monitoring daemon.   To my knowledge the Vera2 hardware is the same so you'll have to go through these steps:

1.) Log into your Vera as root.

2.) Install the USB-HID kernel object.  You don't need to compile one or anything - you can install this one that's close enough.  

http://www.dd-wrt.com/phpBB2/viewtopic.php?t=65676
http://www.dd-wrt.com/phpBB2/viewtopic.php?p=182132

Here's what I did:

Code: [Select]
root@MiOS_18273:/# cd /lib/modules/2.4.37.9
root@MiOS_18273:/lib/modules/2.4.37.9# wget "http://www.dd-wrt.com/phpBB2/download.php?id=5266" -O hid.o.tgz
Connecting to www.dd-wrt.com (83.141.4.210:80)
hid.o.tgz            100% |*********************************************************************************************************************************************************************************************| 14293  00:00:00 ETA
root@MiOS_18273:/lib/modules/2.4.37.9# tar xzvf hid.o.tgz
hid.o
root@MiOS_18273:/lib/modules/2.4.37.9# insmod -f hid.o
root@MiOS_18273:/lib/modules/2.4.37.9# cat /proc/bus/usb/drivers
         usbdevfs
         hub
         acm
         serial
         usb-storage
 [b]96-111: hiddev[/b]
         hid
root@MiOS_18273:/lib/modules/2.4.37.9# cat /proc/bus/usb/devices | grep HID
I:  If#= 0 Alt= 0 #EPs= 1 Cls=03(HID  ) Sub=00 Prot=00 Driver=[b]hid[/b]

3.) Now that the HID device is installed, we need to install the daemon

Code: [Select]
opkg install "http://downloads.openwrt.org/backfire/10.03/brcm-2.4/packages/apcupsd_3.14.7-1_brcm-2.4.ipk"
Refer to http://www.apcupsd.org/ for more info on how to interact with the device.  


4.) Modify your config file.  Its in /etc/apcupsd/apcupsd.conf
5.) Check to make sure it's running:
Code: [Select]
root@MiOS_18273:/lib/modules/2.4.37.9# apcaccess status
APC      : 001,035,0892
DATE     : Wed Sep 14 20:53:47 CDT 2011
HOSTNAME : MiOS_18273
VERSION  : 3.14.7 (1 August 2009) unknown
UPSNAME  : ups1
CABLE    : USB Cable
MODEL    : Back-UPS ES 450
UPSMODE  : Stand Alone
STARTTIME: Wed Sep 14 20:52:44 CDT 2011
STATUS   : ONLINE
LINEV    : 126.0 Volts
LOADPCT  :   0.0 Percent Load Capacity
BCHARGE  : 100.0 Percent
TIMELEFT : 126.0 Minutes
MBATTCHG : 5 Percent
MINTIMEL : 3 Minutes
MAXTIME  : 0 Seconds
SENSE    : Medium
LOTRANS  : 092.0 Volts
HITRANS  : 139.0 Volts
ALARMDEL : Always
BATTV    : 13.5 Volts
LASTXFER : Unacceptable line voltage changes
NUMXFERS : 0
TONBATT  : 0 seconds
CUMONBATT: 0 seconds
XOFFBATT : N/A
STATFLAG : 0x07000008 Status Flag
MANDATE  : 2011-04-09
SERIALNO : 3B1115X42707
BATTDATE : 2011-04-09
NOMINV   : 120 Volts
NOMBATTV :  12.0 Volts
FIRMWARE : 844.K2 .D USB FW:K2
APCMODEL : Back-UPS ES 450
END APC  : Wed Sep 14 20:53:49 CDT 2011

If anyone wants to attempt to write the plugin with me please let me know.

Thanks,

Kristopher

« Last Edit: September 23, 2011, 02:42:54 am by Kristopher »

Offline futzle

  • Beta Testers
  • Master Member
  • *****
  • Posts: 3226
  • Karma: +180/-8
Re: APC UPS Backup - Now just needs a plugin
« Reply #1 on: September 15, 2011, 07:54:59 am »
If anyone wants to attempt to write the plugin with me please let me know.

Let's do it.

Although I've got one of these, it isn't in the same room as my Vera, so you'll have to be the sole tester.  (Also, I use nut, because I have multiple brands of UPS in my house.)

I propose that we start simple and make the plugin appear as an appliance switch.  On == on A/C power; Off == on battery.  Will that do for a beginning?

Offline guessed

  • Master Member
  • *******
  • Posts: 5294
  • Karma: +90/-22
  • Release compat is not a bolted-on afterthought
Re: APC UPS Backup - Now just needs a plugin
« Reply #2 on: September 15, 2011, 09:54:55 am »
@futile,
If your able to manually switch from mains to battery via the API then a switch makes sense.  Otherwise, if you can only detect what state it's in you might want to used LightSensor1 instead, since it's inherently read-only... And will have a correct rendition on conol points...

Offline garrettwp

  • Beta Testers
  • Master Member
  • *****
  • Posts: 6376
  • Karma: +226/-128
  • Vera 3, Lite, ISY994
Re: APC UPS Backup - Now just needs a plugin
« Reply #3 on: September 15, 2011, 11:23:46 am »
A lightsensor would make more sense. You can use the % value of the light sensor to show the remaining percentage left of the battery when on battery power. My vera is hooked up to a cyperpower ups in the garage which a switch is also plugged into the cyberpower as well. Is available via dd-wrt repository? I have a few apc ups, however they are used in my office which is several floors up from the vera unit.

- Garrett

Offline Ap15e

  • Beta Testers
  • Sr. Hero Member
  • *****
  • Posts: 2000
  • Karma: +11/-0
Re: APC UPS Backup - Now just needs a plugin
« Reply #4 on: September 15, 2011, 01:37:49 pm »
The power loss detection should be event driven.

Excerpt from the excellent apcupsd manual:

Code: [Select]
Apcupsd Notification and Events
When a major event is generated within apcupsd, control is passed to the script apccontrol normally found
in /etc/apcupsd/apccontrol. The event name, and a number of other important parameters are passed to
the script.

I'm sure there are many other ways to get the UPS events to the Luup plugin.

In addition, the plugin could poll the current UPS status and store the UPS variables in corresponding UPnP variables.
Add Luup events to the plugin as needed.

BTW, is there an UPnP service for UPS from the UPnP Forum?

Offline futzle

  • Beta Testers
  • Master Member
  • *****
  • Posts: 3226
  • Karma: +180/-8
Re: APC UPS Backup - Now just needs a plugin
« Reply #5 on: September 15, 2011, 05:36:29 pm »
The power loss detection should be event driven.

Absolutely.  (I was going to ask about that later, to avoid cognitive overload now.) It's good to see that it's possible.

Quote
BTW, is there an UPnP service for UPS from the UPnP Forum?

I did a quick search; not that I could see.  link

A lightsensor would make more sense.

There's no reason why we can't tap into more than one service.  The trouble with the standard light sensor service is that there's no binary event that it can trigger, corresponding to "lost AC power" and "regained AC power".  If we make the plugin set both LightSensor and SwitchPower variables, then users can use the device how they see fit.

We can control the dashboard appearance independently of this decision.

Offline Ap15e

  • Beta Testers
  • Sr. Hero Member
  • *****
  • Posts: 2000
  • Karma: +11/-0
Re: APC UPS Backup - Now just needs a plugin
« Reply #6 on: September 16, 2011, 06:09:40 am »
The plugin should display STATUS, LINEV, LOADPCT, BCHARGE, TIMELEFT, BATTV. It could use red color for the values if outside the nominal values (MBATTCHG, MINTIMEL, NOMINV, NOMBATTV).

Offline Ap15e

  • Beta Testers
  • Sr. Hero Member
  • *****
  • Posts: 2000
  • Karma: +11/-0
Re: APC UPS Backup - Now just needs a plugin
« Reply #7 on: September 16, 2011, 11:37:14 am »
... and some suggestions for the GUI presentation.

Offline Kristopher

  • Jr. Member
  • **
  • Posts: 54
  • Karma: +0/-0
Re: APC UPS Backup - Now just needs a plugin
« Reply #8 on: September 22, 2011, 02:08:04 pm »
Awesome guys, love the input.  Will get started on the code tonight.

Offline Ap15e

  • Beta Testers
  • Sr. Hero Member
  • *****
  • Posts: 2000
  • Karma: +11/-0
Re: APC UPS Backup - Now just needs a plugin
« Reply #9 on: September 22, 2011, 04:03:11 pm »
Perfect timing. :) My UPS ES 700 will arrive tomorrow.

BTW, OUTPUTV ("The voltage the UPS is supplying to your equipment") should be visible in the GUI, too.

Offline futzle

  • Beta Testers
  • Master Member
  • *****
  • Posts: 3226
  • Karma: +180/-8
Re: APC UPS Backup - Now just needs a plugin
« Reply #10 on: September 22, 2011, 05:17:54 pm »
Awesome guys, love the input.  Will get started on the code tonight.

Let us know how we can help, whether it's the Lua code or the XML files.

Offline Kristopher

  • Jr. Member
  • **
  • Posts: 54
  • Karma: +0/-0
Re: APC UPS Backup - Now just needs a plugin
« Reply #11 on: September 23, 2011, 11:42:00 am »
Quote
BTW, OUTPUTV ("The voltage the UPS is supplying to your equipment") should be visible in the GUI, too.

Still trying to see how to do this.  Any ideas?

Offline Kristopher

  • Jr. Member
  • **
  • Posts: 54
  • Karma: +0/-0
Re: APC UPS Backup - Now just needs a plugin
« Reply #12 on: September 23, 2011, 12:14:55 pm »
Quote
Still trying to see how to do this.  Any ideas?

OK I see now. My low-end unit doesn't support this.  No worries though, I should be able to implement for when its available.

Offline Kristopher

  • Jr. Member
  • **
  • Posts: 54
  • Karma: +0/-0
Re: APC UPS Backup - Now just needs a plugin
« Reply #13 on: September 23, 2011, 12:40:58 pm »
OK I'm getting near the part where I need help.  I really don't know best practices for setting up devices and what the json should look like.  I have, however, completely the polling of the apcaccess device.  Here is the snippet:

Code: [Select]
function refreshCache()

        debug("refreshCache called")
        luup.call_timer("refreshCache", 1, "30s", "")

        local pipe = io.popen("apcaccess")
        local status = pipe:read("*all")
        pipe:close()
        log("APC #" .. status)

        if (status ~= nil) then
          debug("Successful execution of apcaccess")

          local currentStatus = string.match(status, "STATUS%s*:%s*([A-Z0-9\. ]+)")
          local currentLinev = string.match(status, "LINEV%s*:%s*([0-9\.]+)")
          local currentLoadpct = string.match(status, "LOADPCT%s*:%s*([0-9\.]+)")
          local currentBcharge = string.match(status, "BCHARGE%s*:%s*([0-9\.]+)")
          local currentBattv = string.match(status, "BATTV%s*:%s*([0-9\.]+)")
          local currentTimeleft = string.match(status, "TIMELEFT%s*:%s*([0-9\.]+)")
          local currentOutputv = string.match(status, "OUTPUTV%s*:%s*([0-9\.]+)")

        log("APC #" .. currentStatus .. " " .. currentLinev .. " " .. currentLoadpct .. " " .. currentBcharge .. " " .. currentBattv .. " " .. currentTimeleft)

          -- Store the current status
          luup.variable_set("urn:micasaverde-com:serviceId:LightSensor1", "CurrentLevel",
              currentLoadpct, CURRENT_APC_DEVICE)
          luup.variable_set("urn:micasaverde-com:serviceId:LightSensor1", "CurrentVoltage",
              currentLinev, CURRENT_APC_DEVICE)
          luup.variable_set("urn:micasaverde-com:serviceId:LightSensor1", "Status",
              currentStatus, CURRENT_APC_DEVICE)
          luup.variable_set("urn:micasaverde-com:serviceId:LightSensor1", "BatteryLevel",
              currentBcharge, CURRENT_APC_DEVICE)
          luup.variable_set("urn:micasaverde-com:serviceId:LightSensor1", "BatteryDate",
              os.time(), CURRENT_APC_DEVICE)
          luup.variable_set("urn:micasaverde-com:serviceId:LightSensor1", "BatteryVoltage",
              currentBattv, CURRENT_APC_DEVICE)
          luup.variable_set("urn:micasaverde-com:serviceId:LightSensor1", "TimeLeft",
              currentTimeleft, CURRENT_APC_DEVICE)
          luup.variable_set("urn:micasaverde-com:serviceId:LightSensor1", "Watts",
              currentOutputv, CURRENT_APC_DEVICE)



        else
          log("APC returned status=" .. status)
        end

        luup.variable_set("urn:micasaverde-com:serviceId:HaDevice1", "LastUpdate",
              os.time(), PARENT_DEVICE)
    end

This works by basically just piping the contents of "apcaccess" into a string, then regexing out the appropriate info.  I wrote some stuff into a light sensor as per Futzle's suggestion.  I have no idea how to make those cool battery icons.  I can make the pattern matching more robust if need be (Outputv, for example, does not exist on my device).  

If someone at a high level can sketch out the proper way to do the device let me know.  For my AirMonitor plugin I created a new device for each metric and it was really not fun to do.

PS: I set the "Watts" to Outputv, which is not correct.  It should be Outputv * whatever the current is.  However, I don't currently know what that is.
« Last Edit: September 23, 2011, 01:41:21 pm by Kristopher »

Offline Ap15e

  • Beta Testers
  • Sr. Hero Member
  • *****
  • Posts: 2000
  • Karma: +11/-0
Re: APC UPS Backup - Now just needs a plugin
« Reply #14 on: September 23, 2011, 05:27:08 pm »
Just for reference (status ONLINE):

Code: [Select]
APC      : 001,035,0895
DATE     : Fri Sep 23 23:11:37 CEST 2011
HOSTNAME : MiOS_XXXXX
VERSION  : 3.14.7 (1 August 2009) unknown
UPSNAME  : UPS_ES_700
CABLE    : USB Cable
MODEL    : Back-UPS ES 700G
UPSMODE  : Stand Alone
STARTTIME: Fri Sep 23 23:11:35 CEST 2011
STATUS   : ONLINE
LINEV    : 232.0 Volts
LOADPCT  :   0.0 Percent Load Capacity
BCHARGE  : 100.0 Percent
TIMELEFT :  43.8 Minutes
MBATTCHG : 5 Percent
MINTIMEL : 3 Minutes
MAXTIME  : 0 Seconds
SENSE    : Medium
LOTRANS  : 180.0 Volts
HITRANS  : 266.0 Volts
ALARMDEL : Always
BATTV    : 13.6 Volts
LASTXFER : No transfers since turnon
NUMXFERS : 0
TONBATT  : 0 seconds
CUMONBATT: 0 seconds
XOFFBATT : N/A
STATFLAG : 0x07000008 Status Flag
MANDATE  : 2011-05-18
SERIALNO : 5
BATTDATE : 2011-05-18
NOMINV   : 230 Volts
NOMBATTV :  12.0 Volts
FIRMWARE : 871.O2 .I USB FW:O2
APCMODEL : Back-UPS ES 700G
END APC  : Fri Sep 23 23:11:53 CEST 2011

LOADPCT and TIMELEFT are useless for status ONLINE.

Output for status ONBATT:

Code: [Select]
APC      : 001,036,0929
DATE     : Fri Sep 23 23:19:37 CEST 2011
HOSTNAME : MiOS_XXXXX
VERSION  : 3.14.7 (1 August 2009) unknown
UPSNAME  : UPS_ES_700
CABLE    : USB Cable
MODEL    : Back-UPS ES 700G
UPSMODE  : Stand Alone
STARTTIME: Fri Sep 23 23:11:35 CEST 2011
STATUS   : ONBATT
LINEV    : 000.0 Volts
LOADPCT  :   4.0 Percent Load Capacity
BCHARGE  : 087.0 Percent
TIMELEFT :  33.5 Minutes
MBATTCHG : 5 Percent
MINTIMEL : 3 Minutes
MAXTIME  : 0 Seconds
SENSE    : Medium
LOTRANS  : 180.0 Volts
HITRANS  : 266.0 Volts
ALARMDEL : Always
BATTV    : 12.7 Volts
LASTXFER : Low line voltage
NUMXFERS : 1
XONBATT  : Fri Sep 23 23:19:17 CEST 2011
TONBATT  : 24 seconds
CUMONBATT: 24 seconds
XOFFBATT : N/A
STATFLAG : 0x07060010 Status Flag
MANDATE  : 2011-05-18
SERIALNO : 5
BATTDATE : 2011-05-18
NOMINV   : 230 Volts
NOMBATTV :  12.0 Volts
FIRMWARE : 871.O2 .I USB FW:O2
APCMODEL : Back-UPS ES 700G
END APC  : Fri Sep 23 23:19:41 CEST 2011

No OUTPUTV ,,,