The Vera Community forums have moved!

General => Power Management => Topic started by: Kristopher on September 14, 2011, 09:56:49 pm

Title: APC UPS Backup - Now just needs a plugin
Post by: Kristopher 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

Title: Re: APC UPS Backup - Now just needs a plugin
Post by: futzle 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?
Title: Re: APC UPS Backup - Now just needs a plugin
Post by: guessed 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...
Title: Re: APC UPS Backup - Now just needs a plugin
Post by: garrettwp 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
Title: Re: APC UPS Backup - Now just needs a plugin
Post by: Ap15e 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?
Title: Re: APC UPS Backup - Now just needs a plugin
Post by: futzle 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 (http://upnp.org/sdcps-and-certification/standards/sdcps/)

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.
Title: Re: APC UPS Backup - Now just needs a plugin
Post by: Ap15e 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).
Title: Re: APC UPS Backup - Now just needs a plugin
Post by: Ap15e on September 16, 2011, 11:37:14 am
... and some suggestions for the GUI presentation.
Title: Re: APC UPS Backup - Now just needs a plugin
Post by: Kristopher on September 22, 2011, 02:08:04 pm
Awesome guys, love the input.  Will get started on the code tonight.
Title: Re: APC UPS Backup - Now just needs a plugin
Post by: Ap15e 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.
Title: Re: APC UPS Backup - Now just needs a plugin
Post by: futzle 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.
Title: Re: APC UPS Backup - Now just needs a plugin
Post by: Kristopher 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?
Title: Re: APC UPS Backup - Now just needs a plugin
Post by: Kristopher 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.
Title: Re: APC UPS Backup - Now just needs a plugin
Post by: Kristopher 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.
Title: Re: APC UPS Backup - Now just needs a plugin
Post by: Ap15e 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 ,,,
Title: Re: APC UPS Backup - Now just needs a plugin
Post by: Ap15e on September 23, 2011, 05:54:41 pm
IMHO you should create a new service (e.g., urn:kristopher-com:serviceId:UPS1) for all UPS variables that don't match the predefined variables (http://wiki.micasaverde.com/index.php/Luup_UPNP_Files) for a light sensor.

Your UPS device should consist of two devices: one predefined device (for compatibility with non-UIx user interfaces) and a specialized UPS device.

GUI presentation (quick and dirty):

Install WAI (http://forum.micasaverde.com/index.php?topic=6441.0)

Code: [Select]
local message = "APC #" .. currentStatus .. " " .. currentLinev .. " " .. currentLoadpct .. " " .. currentBcharge .. " " .. currentBattv .. " " .. currentTimeleft
luup.variable_set( 'urn:upnp-ap15e-com:serviceId:WAI1', 'Location', message, WAI_dev_ID )

Derive your device files from WAI and GWC. Copy with pride. :)

GWC uses HTML injection to display icons and formatted text in the GUI. I don't know whether there is a way without using HTML injection which - of course - isn't politically correct (but doesn't break in UI5 ...).

No idea where to get the icons (they must be freely usable).
Consult the dataMine plugin for information about where to put the icons.
Title: Re: APC UPS Backup - Now just needs a plugin
Post by: futzle on September 23, 2011, 07:46:27 pm
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:
[...]
I wrote some stuff into a light sensor as per Futzle's suggestion.  I have no idea how to make those cool battery icons. [...] If someone at a high level can sketch out the proper way to do the device let me know.

That's a pretty good start.  Here's a few recommendations:

It's allowed (I'd even say it's desirable) to use multiple service IDs in your device.  Pick and choose from among the standard services for things that match up.  For stuff that doesn't match any existing service, invent your own service ID.  For stuff that does match an existing service (LightSensor has CurrentLevel, for example), set the variable twice: once with your service ID and once with the out-of-the-box service ID.  Examples:

Code: [Select]
-- Set battery level.  First is for my code to use, second is for battery icon in dashboard.
-- (The string urn:micasaverde-com:serviceId:HaDevice1 is magical.)
luup.variable_set("urn:kristopher:serviceId:UPS1", "BatteryLevel",
      currentBcharge, CURRENT_APC_DEVICE)
luup.variable_set("urn:micasaverde-com:serviceId:HaDevice1", "BatteryLevel",
      currentBcharge, CURRENT_APC_DEVICE)

Code: [Select]
-- Set load.  First is for my code to use, second is to masquerade as a light sensor.
luup.variable_set("urn:kristopher:serviceId:UPS1", "CurrentLevel",
      currentLoadpct, CURRENT_APC_DEVICE)
luup.variable_set("urn:micasaverde-com:serviceId:LightSensor1", "CurrentLevel",
      currentLoadpct, CURRENT_APC_DEVICE)

Code: [Select]
-- Set online status.  First is to masquerade as a binary switch, second is for my code to use.
luup.variable_set("urn:upnp-org:serviceId:SwitchPower1", "Status",
      (currentStatus == "ONLINE") and 1 or 0, CURRENT_APC_DEVICE)
luup.variable_set("urn:kristopher:serviceId:UPS1", "Status",
      currentStatus, CURRENT_APC_DEVICE)

Code: [Select]
-- No common service knows of this type.  Just set the variable in my namespace.
luup.variable_set("urn:kristopher:serviceId:UPS1", "CurrentVoltage",
      currentLinev, CURRENT_APC_DEVICE)

As Ap15e says, everyone just copies-and-pastes device files.  My usual sequence when making a brand-new device is:

1. Write a stub D_APCUPSBackup1.xml file.  Copy and paste it from an existing device, changing: deviceType, ImplementationFile.  Empty out the serviceList.  Point staticJson to a not-yet-existing D_APCUPSBackup1.json file.  We'll make that later.

2. Write a simple I_APCUPSBackup1.xml file.  Put your Lua in it.  Leave most of the other sections empty.

(By this point you will be able to create a device from this file, see its log output in the Luup log, and see its variables in the Advanced tab.  You can debug your Lua until you are happy that the variables are getting set properly.  The dashboard will have a default device box, and you won't have any events or notifications.)

3. Write the D_APCUPSBackup1.json Static JSON file.  Again, copy and paste from an existing device, or read my reverse-engineering notes (http://wiki.micasaverde.com/index.php/Luup_plugins:_Static_JSON_file) on the wiki.  Leave eventList and sceneList empty for the moment.  While debugging, if you need to make changes to the Static JSON file, you need to restart the Luup engine and clear your browser's cache.

(By this point you will have a customized device appearance in the dashboard.  Whatever status you've opted to include in the dashboard will update every thirty seconds, and if you've defined any tabs in the static JSON file, they'll appear too.)

If all you care about is read-only viewing of the UPS status, you can stop now.

4. Define events in the static Json file.  I haven't written the reverse-engineering notes for this section yet, so copy from something already present, or let us help you write them.  Events are what can trigger scenes and notifications.

(By this point you will have the ability for the plugin to trigger a scene when, say, the UPS goes onto battery, or when the load goes over a certain percentage, or whatever other events you've defined.)

5. Go back to the D_APCUPSBackup1.xml file and fill out the serviceList section.  For each service Id you've ever mentioned, put in a service section.  If you follow my examples above you'll have four.  Three will mention S_*.xml files that already exist.  For the fourth, invent a name (say, S_UPS1.xml) and match it against your urn:kristopher:serviceId:UPS1 service Id.

(You won't notice anything different in the UI, but anyone using "watch variable" or other introspection will be able to act on your status variables changing.)

6. Write the S_UPS1.xml file.  Include any variables that you have mentioned in the urn:kristopher:serviceId:UPS1 service namespace.  If you want to define any actions (for the UPS, acting on an interrupt when the UPS suddenly goes off battery would be a good one), put them in.

(Again, no UI difference, but now you can interact with the device over HTTP, and things like the dataMine plugin will be able to plot your device's variables over time.)
Title: Re: APC UPS Backup - Now just needs a plugin
Post by: Kristopher on September 23, 2011, 10:38:08 pm
Quote
LOADPCT and TIMELEFT are useless for status ONLINE.

I suspect TIMELEFT is probably very dynamic based on current load, and also that the battery probably loses TIMELEFT over time.  So it might not be entirely useless on wall power.  Also, LOADPCT is a non-100% value after an outage.  Might be useful for taking action during a brownout or rolling blackout.

Kristopher

Title: Re: APC UPS Backup - Now just needs a plugin
Post by: Ap15e on September 24, 2011, 07:10:29 am
I suspect that the UPS doesn't measure LOADPCT if STATUS == ONLINE.

May I suggest using a generic 'apcupsd to UPnP' mapping?

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

for identifier,cur_val in string.gmatch( st, '(%w+).-:(.-)\n' )
 do

  print( identifier, cur_val )

 local value = string.match( cur_val, '[0-9.,-]+' )

--  luup.variable_set("urn:kristopher-com:serviceId:UPS1", tostring( identifier ), tostring (cur_val), CURRENT_APC_DEVICE )

  if value ~= nil
   then

print( '************ ',value )

--    luup.variable_set("urn:kristopher-com:serviceId:UPS1", tostring( identifier ) .. '_VALUE', tostring ( value ), CURRENT_APC_DEVICE )

   end

 end

Not perfect, but I could live with it ...
Title: Re: APC UPS Backup - Now just needs a plugin
Post by: Kristopher on September 26, 2011, 02:30:23 pm
Instructions thus far:

1.) Download archive
2.) Extract images into /www/icons
3.) Upload 4 xml/json files via Luup
4.) Create device D_APC.xml

I would like to figure out some way to script an install file.  The installer would need to:

1.) Download the hid.o device into the kernel module directory
2.) echo "hid" > /etc/modules/33-hid
3.) opkg update; opkg install apcupsd
4.) create init script for apcupsd (/etc/rc.*/)
5.) mkdir /www/icons
6.) Download icons from theme with the new filenames

Give it a shot, let me knwo what you think.  Very basic but very functional.

Kristopher
Title: Re: APC UPS Backup - Now just needs a plugin
Post by: garrettwp on September 26, 2011, 02:56:21 pm
There is no attachment.  ;D

- Garrett
Title: Re: APC UPS Backup - Now just needs a plugin
Post by: Kristopher on September 26, 2011, 10:19:48 pm
Quote
There is no attachment.

Everyone's a critic!
Title: Re: APC UPS Backup - Now just needs a plugin
Post by: garrettwp on September 27, 2011, 12:55:07 am
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.  :)

- Garrett
Title: Re: APC UPS Backup - Now just needs a plugin
Post by: futzle on September 27, 2011, 06:50:09 am
Give it a shot, let me knwo what you think.  Very basic but very functional.

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 <action> 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.
Title: Re: APC UPS Backup - Now just needs a plugin
Post by: Kristopher on September 27, 2011, 09:27:50 am
Hi Futzle,

Can you show me how to do number 3?

Kristopher
Title: Re: APC UPS Backup - Now just needs a plugin
Post by: Ap15e on September 27, 2011, 04:19:06 pm
Excellent job! Thanks for sharing your code.

Code: [Select]
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

Code: [Select]
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.
Title: Re: APC UPS Backup - Now just needs a plugin
Post by: futzle on September 27, 2011, 05:08:12 pm
Can you show me how to do number 3?

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:

Code: [Select]
-- 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

Code: [Select]
return true

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

Code: [Select]
return false, "APC files not installed", "APC UPS plugin"

The other two strings find their way to the dashboard.
Title: Re: APC UPS Backup - Now just needs a plugin
Post by: Ap15e on September 27, 2011, 05:33:45 pm
Code: [Select]
file, msg = io.open(name, "r")
if not file then print(msg) end
Title: Re: APC UPS Backup - Now just needs a plugin
Post by: guessed on September 27, 2011, 09:07:44 pm
If you want to be safe, always "return" all three values, even in the true 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 close 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)
Title: Re: APC UPS Backup - Now just needs a plugin
Post by: Kristopher on September 27, 2011, 11:26:31 pm
Quote
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

Title: Re: APC UPS Backup - Now just needs a plugin
Post by: Ap15e on September 28, 2011, 05:19:46 am
IIRC if your're running on batteries, STATUS is ONBATT.
Title: Re: APC UPS Backup - Now just needs a plugin
Post by: Ap15e on October 01, 2011, 06:54:38 am
Would you mind adding

Code: [Select]
        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.
Title: Re: APC UPS Backup - Now just needs a plugin
Post by: Ap15e on October 01, 2011, 06:19:47 pm
AUC (APC UPS Companion) is available from http://forum.micasaverde.com/index.php?topic=7989.0.
Title: Re: APC UPS Backup - Now just needs a plugin
Post by: ChrisAZ on October 04, 2011, 01:35:20 am
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?
Title: Re: APC UPS Backup - Now just needs a plugin
Post by: Ap15e on October 04, 2011, 07:25:58 am
Is the apcupsd running?
Title: Re: APC UPS Backup - Now just needs a plugin
Post by: ChrisAZ on October 04, 2011, 08:51:38 pm
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

Title: Re: APC UPS Backup - Now just needs a plugin
Post by: bmeyer29 on October 04, 2011, 11:33:54 pm
@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.
Title: Re: APC UPS Backup - Now just needs a plugin
Post by: Ap15e on October 05, 2011, 05:17:39 pm
Kristopher,

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

If you're going to fix it, please don't update LastUpade if apcaccess returns an error.
Title: Re: APC UPS Backup - Now just needs a plugin
Post by: Ap15e on October 05, 2011, 05:50:18 pm
AUC (APC UPS Companion) V0.2 beta is available from http://forum.micasaverde.com/index.php?topic=7989.0.
Title: Re: APC UPS Backup - Now just needs a plugin
Post by: JOD on January 20, 2012, 10:48:32 am
Is anyone using this on V3/UI5?

JOD.
Title: Re: APC UPS Backup - Now just needs a plugin
Post by: bmeyer29 on July 06, 2012, 10:51:58 pm
just checking to see if there has been any investigation on ui5/veralite
Title: Re: APC UPS Backup - Now just needs a plugin
Post by: Grain on July 07, 2012, 01:30:51 am
This could be very interesting indeed!

Sent from my SGSII CM9 with Tapatalk 2
Title: Re: APC UPS Backup - Now just needs a plugin
Post by: oTi@ on July 07, 2012, 05:51:04 am
Keep in mind that @Ap15e has ceased active participation in the forum, and access to the plug-ins was revoked.
Title: Re: APC UPS Backup - Now just needs a plugin
Post by: wilme2 on October 02, 2013, 04:17:50 pm
Keep in mind that @Ap15e has ceased active participation in the forum, and access to the plug-ins was revoked.

So I know that all of the plug-ins are user created, but I am at a loss we can't interface VERA with a UPS in 2013, after others were apparently able to do so in 2011.

Is anyone else monitoring an APC UPS via USB with current shareable code?  I am sure some UPS with IP interfaces are working, but I have a giant APC S15 Power Conditioner & Battery Backup in my network closet, and after a scare earlier today, I want to make sure I can monitor it.  The PC attached is used as a DVR, and is sleeping when not recording or playing back recordings...
Title: Re: APC UPS Backup - Now just needs a plugin
Post by: guest15496 on October 05, 2013, 08:39:57 am
We just need a generic HTTP screen scraper plugin, that supports simple authentication also. We can then adopt it for many devices that expose their admin interfaces via a web page.
Title: Re: APC UPS Backup - Now just needs a plugin
Post by: jimpapa on January 01, 2014, 02:52:54 pm
Anyone working on a UPS plugin?

I see a real need to monitor if your power is out and if so.. Can send push notifications and network commands via SSH to servers etc. while on UPS power.

I have been looking for a decent way to monitor if AC power is down at my office and to be notified. 
All vital equipment is on UPS including network closet. 

We wired in a manual transfer switch.   I could not install a natural gas gen with auto transfer switch because our feed to the office suite is only 1/2" pipe.   So I need to get alerted that we lost power.. If I am not in the office and also. Need to shutdown a rack of servers on an APC 3000 UPS

Having vera do this would be great.. 
Title: Re: APC UPS Backup - Now just needs a plugin
Post by: AgileHumor on March 26, 2014, 01:33:08 pm
How to people monitor outages?  I have a Cyperpower UPS, but would like some notification when power goes out.
Title: Re: APC UPS Backup - Now just needs a plugin
Post by: jimpapa on March 26, 2014, 01:45:33 pm
I started a thread a while back about this..

so this is what I came up with and it is working great.

http://forum.micasaverde.com/index.php/topic,22330.msg149194.html#msg149194

Title: Re: APC UPS Backup - Now just needs a plugin
Post by: kabili207 on April 04, 2014, 06:41:13 pm
I spent some time over the past few days parsing the output of apcupsd CGI stats page (http://www.apcupsd.com/manual/manual.html#upsfstatus-cgi) and came up with this. This is for UPS units not connected to the Vera. You need to setup a server running apcupsd to expose the cgi scripts.

After uploading the files attached the the vera, add a device with the following parameters:
Device type: urn:zyrenth-com:device:Ups:1
Device filename: D_ApcUps1.xml
Imp file: I_ApcUps1.xml

After creating the device, go to the device settings and set the url to the cgi script (ex. http://192.168.1.10/cgi-bin/upsfstats.cgi). Make sure you use the upsfstats.cgi script!

Let me know if there is anything you'd like me to add.
Title: Re: APC UPS Backup - Now just needs a plugin
Post by: hharte on May 04, 2014, 02:41:51 pm
Wow, the plugin by kabili207 works great.  I have it running on my Vera3 with three APC Back-UPS Pro.

UPS1: 700VA APC Back-UPS Pro attached to 192.168.0.100 (also hosting the apcupsd webserver, running Windows 8.1)
UPS2: 700VA APC Back-UPS Pro attached to 192.168.0.110 (apcupsd running on Windows 7)
UPS3: 1500VA APC Back-UPS Pro attached to 192.168.0.111 (apcupsd running on Windows 8.1)

The Windows 8.1 "server" machine is running the Apache2 HTTP server via Cygwin and is hosting the gci scripts.  On the machine hosting the webserver, you add each of the other machines to the apcupsd hosts.conf file as follows:

MONITOR 127.0.0.1 "UPS1"
MONITOR 192.168.0.110 "UPS2"
MONITOR 192.168.0.111 "UPS3"

On the remote machines, you need to create an inbound rule in Windows Firewall to unblock TCP port 3551.

Then for each instance of the Vera UPS plugin, you add the appropriate URL from your web server, ie, for the example above:

http://192.168.0.100/cgi-bin/apcupsd/upsfstats.cgi
http://192.168.0.100/cgi-bin/apcupsd/upsfstats.cgi?host=192.168.0.110&temp=F
http://192.168.0.100/cgi-bin/apcupsd/upsfstats.cgi?host=192.168.0.111&temp=F

In order to be able to tell in Vera if there is a power failure, I used the App "Program Logic Event Generator" to monitor the "OnBattery" Device Property of each APC UPS.  If any of them goes to "OnBattery == 1" then it will turn a Virtual Switch (App) to off, indicating a power failure.  When all of the UPS OnBattery == 0, then the power is declared restored, and the Virtual Switch is turned on.

I'm using the Virtual Switch to trigger  scenes for "power failure" and "power restored."  In those scenes, I send push notifications via the "Push Notifications" App to my Android phone.

It is working really well so far.  A couple things I can think of to improve this plugin are as follows:

1. Provide a "motion sensor" capability or make it a virtual switch (like the "Day or Night" app) .
2. Add the ability to add an offset to the wattage meter.  For example, my UPS says the load attached to it is 61W, but when I measure it with a power meter, it is really using 79W.  I guess this is because the UPS itself is consuming some power.
3. Add it to the Vera Apps store for easier download/install.

Thanks again for the useful plugin!

-Howard
Title: Re: APC UPS Backup - Now just needs a plugin
Post by: tomgru on December 16, 2014, 08:03:09 pm
How to people monitor outages?  I have a Cyperpower UPS, but would like some notification when power goes out.

@agilehumor, did you ever come up with a solution for this?
Title: Re: APC UPS Backup - Now just needs a plugin
Post by: tedp on June 14, 2016, 06:47:45 pm
I probably should start a new topic, but I don't want to confuse people given I'm posting a modified implementation file.

I got  kabili207's plug in working -- the biggest hassle was getting IIS to work on my home network. I realized there's not really a reason to have a web-server in the path since all the web-server does is access port 3551 on the individual machines to poll for UPS data. So, I rewrote the implementation file to access individual computers associated with a UPS directly.

I simply replaced socket.http with socket.tcp (along with some other sundry changes).

The file is attached -- instead of specifying a URL on the control page (i.e. http://192.168.1.50/xxxx), just specify the IP address of the computer that is running apcupsd --  i.e.  192.168.1.22 (just the IP, no http or anything else in the field). Make sure there is a firewall exception to allow access to that port on the target computer.

There is probably a good amount of cleanup to do, but it works -- and in conjunction with altUI watches and veraAlerts, I can be notified of power failures.
Title: Re: APC UPS Backup - Now just needs a plugin
Post by: Scarab on December 17, 2016, 03:25:56 pm
Great but has anyone found a way to plug the USB directly into the Vera unit?  Most modern (home) UPSes not only alert during a power outage but also provide power consumption data in WATTs.  Even thought the energy monitoring on Vera is not all that great it would be nice to know what my equipment rack is using compared to the rest of the house. 

Most energy monitors compatible with vera are switched, but I want something more passive so no one can accidently turn off my network equipment.  It would simply be nice to be alerted of a power failure and have the device report power consumption. 
Title: Re: APC UPS Backup - Now just needs a plugin
Post by: tedp on December 20, 2016, 08:52:21 am
Great but has anyone found a way to plug the USB directly into the Vera unit?  Most modern (home) UPSes not only alert during a power outage but also provide power consumption data in WATTs.  Even thought the energy monitoring on Vera is not all that great it would be nice to know what my equipment rack is using compared to the rest of the house. 

Most energy monitors compatible with vera are switched, but I want something more passive so no one can accidently turn off my network equipment.  It would simply be nice to be alerted of a power failure and have the device report power consumption.

See my post at http://forum.micasaverde.com/index.php/topic,38300.msg286318.html on this topic. To get APCUPS working with Vera would require a recompile of some USB drivers which would then have to be installed. That would only be step 1, with additional hurdles afterwards. It's beyond my capabilities. To achieve what you are trying to do, I might try and hook up a BeagleBone or a RaspberryPi to the same UPS as my Vera to APCUPS. FYI: APCUPS only works on APC brand devices. I'm not sure if there are equivalents for other brands (i.e. CyberPower). Also, the lower end APCUPS devices do not report real-time power consumption.
Title: Re: APC UPS Backup - Now just needs a plugin
Post by: Don Phillips on December 20, 2016, 09:58:14 pm
The file is attached -- instead of specifying a URL on the control page (i.e. http://192.168.1.50/xxxx), just specify the IP address of the computer that is running apcupsd --  i.e.  192.168.1.22 (just the IP, no http or anything else in the field). Make sure there is a firewall exception to allow access to that port on the target computer.

Excellent tweak on an excellent plug in.  Enabling IIS is not something I wanted to do. 

I have the device set up, pointing to the computer the UPS is plugged into, 192.168.1.100.  But Vera reports "Data URL is required". I check the device and the URL under setting is blank.  I enter 192.168.1.100 and it reports "error: invalid service". I ensured port 3551 is open to all connections on the .100 computer.  APCUPSD is running and I can see the data, but Vera is not being cooperative.

I re-uploaded the file in case that was the problem - still not working.

Am I overlooking something?
Title: Re: APC UPS Backup - Now just needs a plugin
Post by: tedp on December 21, 2016, 01:41:07 am
The file is attached -- instead of specifying a URL on the control page (i.e. http://192.168.1.50/xxxx), just specify the IP address of the computer that is running apcupsd --  i.e.  192.168.1.22 (just the IP, no http or anything else in the field). Make sure there is a firewall exception to allow access to that port on the target computer.

Excellent tweak on an excellent plug in.  Enabling IIS is not something I wanted to do. 

I have the device set up, pointing to the computer the UPS is plugged into, 192.168.1.100.  But Vera reports "Data URL is required". I check the device and the URL under setting is blank.  I enter 192.168.1.100 and it reports "error: invalid service". I ensured port 3551 is open to all connections on the .100 computer.  APCUPSD is running and I can see the data, but Vera is not being cooperative.

I re-uploaded the file in case that was the problem - still not working.

Am I overlooking something?

hmmm.. I was trying to pull out the relevant LUA code that is the "secret sauce" so you could run it under something like altUI's "Test LUA" page to see if you are actually talking with your APC. Much to my dismay, I'm getting some LUA error when running the code, but the plugin still seems to work fine.

If you want to give it a shot, paste this into the "test code" and change the string for the IP address on line #8. If it works, you should get a printout of the APCUPSD status dump.

Code: [Select]
local socket = require("socket")
local apc = socket.tcp()
apc:settimeout(5)
local st = os.time()
print (st)
local stats = {} 
apc:setoption("tcp-nodelay",true)
apc:connect("192.168.1.xx", 3551)
apc:send("\00\06")
for i = 1,100,1 do  -- there should not be more than 100 lines
local result = apc:receive(2) -- the length comes first
local lineLen = 256* string.byte(result,1) + string.byte(result,2) -- convert length into an integer
local v = apc:receive(lineLen - 1) -- read the rest of the line less the newline
apc:receive(1) -- dump the newline
local kk = v:match("^(.-)%s*:")  -- get the key
local vv = v:match("^.-:%s*(.-)%s*$") --get the value
stats[kk] = vv
if string.find(v,"END.*") ~= nil  then break end -- did we get to the end?
end
apc:close()

    print("fetchSensorData: exec=" .. (os.time() - st))

print(v)
return(true)
Title: Re: APC UPS Backup - Now just needs a plugin
Post by: Don Phillips on December 21, 2016, 05:03:32 pm
I really appreciate the help.  The LUA code seems to run with no errors, but I do not see any "printout" on the screen nor can I figure out where to go to look at a file. Note: I do not use ALTUI.

In the meantime, trying to get IIS running but no luck getting CGI enabled. Tinkering with it I may have caused more damage to settings than anything else.   
Title: Re: APC UPS Backup - Now just needs a plugin
Post by: Don Phillips on December 22, 2016, 02:10:41 pm
Quick follow up: I tested the APCUPSD installation and I am able to get data from a command prompt from another computer by running apcaccess status 192.168.1.100 so at least the app is reachable.  Just not sure why Vera cannot get the same data using the plugin.  ???
Title: Re: APC UPS Backup - Now just needs a plugin
Post by: Don Phillips on December 24, 2016, 01:28:30 pm
I finally figured out what happened.  When I uploaded the new implementation file, Vera added the local path into the file name, C:\Users\Don\Desktop\I_ApcUps1.xml, so when I created the device, I was using the old file not the new one.  Changed it to the long file name and viola! Working like a charm. I have a second UPS powering my network gear and NAS I'll work on next.   
Title: Re: UPS done right
Post by: bucko on December 24, 2016, 08:30:58 pm
This is a Sako 3000VA online UPS that powers all my HA equipment for 10-12 hrs. Cams, Vera, M1 Gold, network, etc.
Title: Re: APC UPS Backup - Now just needs a plugin
Post by: Don Phillips on December 25, 2016, 04:51:23 pm
I made good progress. If both of my UPS's are on battery, a multiswitch is turned on and I get a notification. Only problem I am having is the OnBattery=0 variable in each UPS device does not change to 1 when I unplug them, but stays at 0. I have double checked the APCUPSD app and it shows the status going from ONLINE to ONBATT so something is happening. 

If I manually change the UPS devices to OnBattery=1, all my triggers work perfectly.

I am seeing battery levels, connected wattage, and lots of other variables, from APCUPSD but the OnBattery is the only one I do not see changing. Almost like I am missing something again  ???
Title: Re: APC UPS Backup - Now just needs a plugin
Post by: Don Phillips on December 26, 2016, 10:22:01 am
I always found it interesting how you must give Vera time to get used to new things and this plug-in was no different.

After a couple of days of it running with the new plug-in, this morning I was testing it again and after about 1 to 2 minutes, the OnBattery variable finally changed to 1, triggering an icon change on VeraMate. It got me thinking to also look at the line voltage, since either one indicates a power loss.

So I unplugged the second UPS, again waited a couple of minutes, and the second switch flipped on so the trigger Power Outage tripped, sending me a notification. Working as expected except for the longer than expected delays. I had even set the Vera polling to 1 second (from 5 yesterday) and the APCUPSD polling variable to 5 seconds.

I waited a few minutes, then plugged in the UPS with the larger load. A couple of minutes later plugged in the second UPS, and again after a couple of minutes, received a notification that power was restored.

So everything seems to be working, now I just need to figure out the delay.
Title: Re: APC UPS Backup - Now just needs a plugin
Post by: tedp on December 27, 2016, 12:59:22 am
It seems like you're no longer using IIS -- right? With respect to the delays you were seeing, there are a bunch of timing loops to consider. 1st is the sampling period of apcupsd: I believe the "POLLTIME" setting in the apcupsd.conf file defaults to 60 seconds. I set mine to 10 seconds. Then there is the polltime of the Vera plugin, I set that to 10 seconds as well. I want to get reasonable notifications in case of a power outage, but at the same time I don't want to overtax the sampling. It's also one of the reasons I wanted to take the webserver out of the loop because it introduces yet another timing loop.

I'm glad you got it all going.
Title: Re: APC UPS Backup - Now just needs a plugin
Post by: Don Phillips on December 27, 2016, 07:25:57 pm
Exactly, no need to run IIS to just get the data APCUPSD already serves.

When I said I changed the APCUPSD polling to 5 seconds, it was the ONBATTERYDELAY variable. The POLLTIME variable was remarked out and set to 60, so I set it to 1.  Retested and much peppier now. Not sure why I did not see it before.

Again, thanks for the awesome tweak on an awesome plug-in. Oh, and +1 to you and kabili207.
Title: Re: APC UPS Backup - Now just needs a plugin
Post by: reneboer on December 29, 2016, 10:39:25 am
Hi,

As I have a Rasberry Pi running a web server and openLuup I took a bit of a different route. I installed NUT on the Pi and created the attached plugin to parse the upsc command response. This plug in looks like a standard Door/Windows sensor. Benefit of that is that Vera and apps can better deal with it. The only thing is that the Alerts will trigger a door open notification.

When using it on a Vera you still need a Pi or other server with NUT installed as that cannot run on the Vera it self.

This is tested with an APC UPS attached to a Synology NAS and NUT on a Rasberry Pi.
- Follow these instructions to install NUT https://www.domoticz.com/forum/viewtopic.php?t=2266 (https://www.domoticz.com/forum/viewtopic.php?t=2266) (installing just the nut-client package may suffice, not tested)
- test the installation using the command : upsc ups@(your NAS IP)

When on Vera you need a remote server to run the upsc command.
Put this php script in the default sever directory. Configure the IP address of your NAS.
For a Pi with Apache and default settings this is /var/www/html   
Code: [Select]
<?php
$nas_ip 
'192.168.x.x';
$output shell_exec('upsc ups@'.$nas_ip);
echo 
"<pre>$output</pre>";
?>

When you have installed on openLuup you do not need the php script, but then you need to set the Remote variable to 0.

The plugin is pending approval on the Vera app market, but is available on the ALTUI App Store if you have ALTUI. You can also force an install on your Vera with this URL http://<your vera IP>:3480/data_request?id=action&serviceId=urn:micasaverde-com:serviceId:HomeAutomationGateway1&action=CreatePlugin&PluginNum=8866&Version=32851 (http://<your vera IP>:3480/data_request?id=action&serviceId=urn:micasaverde-com:serviceId:HomeAutomationGateway1&action=CreatePlugin&PluginNum=8866&Version=32851)

Install the plugin by uploading the D_UPSSensor1.xml and I_UPSSensor1.xml files to your Vera. Then manually create a device using these two files.

The device is a default door/windows sensor so it will behave as such. Extra variables will be set with all parameters from the upsc command and others like Watts, just have a look.

The only configuration required is to set the IP address for the web server running the script in the ip attribute.
When running local on openLuup you must enter the IP address of your NAS and set the Remote variable to 0.
   
Some more background information
   GitHub repository    https://github.com/reneboer/vera-UPSSensor (https://github.com/reneboer/vera-UPSSensor)
   variables possible    http://networkupstools.org/docs/user-manual.chunked/apcs01.html (http://networkupstools.org/docs/user-manual.chunked/apcs01.html)
   status details APC    http://www.mathstat.dal.ca/~selinger/ups/backups.html (http://www.mathstat.dal.ca/~selinger/ups/backups.html)

Cheers Rene
Title: Re: APC UPS Backup - Now just needs a plugin
Post by: Don Phillips on December 29, 2016, 07:57:47 pm
Thanks for the alternative option.

I have an RPi3 in a box that I could whip out to monitor the UPS that my Vera is connected to. It may be a winter 2018 project (not enough time).
Title: Re: APC UPS Backup - Now just needs a plugin
Post by: wilme2 on January 04, 2017, 05:12:38 pm
Hi,

As I have a Rasberry Pi running a web server and openLuup I took a bit of a different route. I installed NUT on the Pi and created the attached plugin to parse the upsc command response. This plug in looks like a standard Door/Windows sensor. Benefit of that is that Vera and apps can better deal with it. The only thing is that the Alerts will trigger a door open notification.

When using it on a Vera you still need a Pi or other server with NUT installed as that cannot run on the Vera it self.


Could not this have run all on the Synology NAS?
Title: Re: APC UPS Backup - Now just needs a plugin
Post by: reneboer on January 05, 2017, 05:11:42 am
Hi,

I guess if you find out how NUT talks to the Synology NAS to get the status, then yes I'd think so. No idea how hard that is though. This was a few hours job. Maybe next my try  ;).

Cheers Rene
Title: Re: APC UPS Backup - Now just needs a plugin
Post by: reneboer on January 06, 2017, 06:17:29 am
Hi,

So... one evening later and a managed to figure out how NUT talks to a UPS server (like a Synology NAS) over IP and attached is a version of the plugin that does not require a Pi running NUT and a special script  :D.

If you upload this version and point it to your Synology NAS IP address it should work directly.

It does not support communication via SSL, so if you turned that on on your UPS server it won't work. Also if you have a different server than a Synology NAS you may need to change the UPSName variable and add the correct port to the ip attribute. The default port number is 3493.

BTW, tested on UI7 and openLuup. I do not do UI5 anymore.

This version is pending approval on the Vera App Market, but can can install via this URL http://<your vera IP>:3480/data_request?id=action&serviceId=urn:micasaverde-com:serviceId:HomeAutomationGateway1&action=CreatePlugin&PluginNum=8866&Version=32866 (http://<your vera IP>:3480/data_request?id=action&serviceId=urn:micasaverde-com:serviceId:HomeAutomationGateway1&action=CreatePlugin&PluginNum=8866&Version=32866).


Enjoy.

Cheers Rene
Title: Re: APC UPS Backup - Now just needs a plugin
Post by: wilme2 on January 06, 2017, 10:49:31 am
So... one evening later and a managed to figure out how NUT talks to a UPS server (like a Synology NAS) over IP and attached is a version of the plugin that does not require a Pi running NUT and a special script  :D.

It does not support communication via SSL, so if you turned that on on your UPS server it won't work.

Awesome - thanks!  I do use a Synology NAS, but with SSL, but let me look at it and test.  I am UI5, so can test that too. 
Title: Re: APC UPS Backup - Now just needs a plugin
Post by: Judman13 on March 09, 2017, 02:54:51 pm
Hi,

So... one evening later and a managed to figure out how NUT talks to a UPS server (like a Synology NAS) over IP and attached is a version of the plugin that does not require a Pi running NUT and a special script  :D.

If you upload this version and point it to your Synology NAS IP address it should work directly.

It does not support communication via SSL, so if you turned that on on your UPS server it won't work. Also if you have a different server than a Synology NAS you may need to change the UPSName variable and add the correct port to the ip attribute. The default port number is 3493.

BTW, tested on UI7 and openLuup. I do not do UI5 anymore.

This version is pending approval on the Vera App Market, but can can install via this URL http://<your vera IP>:3480/data_request?id=action&serviceId=urn:micasaverde-com:serviceId:HomeAutomationGateway1&action=CreatePlugin&PluginNum=8866&Version=32866 (http://<your vera IP>:3480/data_request?id=action&serviceId=urn:micasaverde-com:serviceId:HomeAutomationGateway1&action=CreatePlugin&PluginNum=8866&Version=32866).


Enjoy.

Cheers Rene

Will try this tonight! Many thanks!
Title: Re: APC UPS Backup - Now just needs a plugin
Post by: Buxton on September 13, 2018, 08:59:16 pm
Quote
When you have installed on openLuup you do not need the php script, but then you need to set the Remote variable to 0.

Hi Rene,  where is the "Remote" variable referred to in the above.  I can't find it anywhere.

I have installed the plugin on openLuup but cannot get a connection to my NUT server, which is also running on the same machine as openLuup.  I'm using the apcupsd client driver as I could only get apcupsd running on the remote machine connected to my UPS (NUT would not load on the windows box connected to the UPS).
Title: Re: APC UPS Backup - Now just needs a plugin
Post by: reneboer on September 17, 2018, 07:46:18 am
Quote
When you have installed on openLuup you do not need the php script, but then you need to set the Remote variable to 0.

Hi Rene,  where is the "Remote" variable referred to in the above.  I can't find it anywhere.

I have installed the plugin on openLuup but cannot get a connection to my NUT server, which is also running on the same machine as openLuup.  I'm using the apcupsd client driver as I could only get apcupsd running on the remote machine connected to my UPS (NUT would not load on the windows box connected to the UPS).
Hi Buxton,

The Remote variable is only for the V1.0 version. The current release can poll directly. No need to install NUT. Please note that for several servers, for example Synology, both devices must be on the same subnet.

Cheers Rene
Title: Re: APC UPS Backup - Now just needs a plugin
Post by: Buxton on September 17, 2018, 07:38:19 pm
OK thx.  I may try to mod the plugin to work directly with the APCUPS server. There is a network interface to the APCUPS software that provides data from the UPS itself. 
Title: APC UPS Backup Now just needs a plugin
Post by: Leprorp on February 02, 2019, 02:06:59 pm
I just ran a small test run to see how it ran and wow Now it turns up like yz 125 . I just have to run it at the track next week and see what its worth ??????????
It feels strong.