Battery & UPS Monitor plugin.

Posted on
Fri Feb 01, 2013 1:15 pm
bschollnick2 offline
Posts: 1294
Joined: Oct 17, 2004
Location: Rochester, Ny

Battery & UPS Monitor plugin.

Folks,

What is the Battery & UPS Monitor plugin?

The Battery & UPS Monitor plugin will allow Indigo to monitor any UPS that can natively connect to Mac OS X (typically through USB). This plugin uses the UPS support built-in to the Energy Saver System Preference & Mac OS X, to monitor the status of your UPS and/or Laptop battery.

What can you do with this?


First, the plugin can be configured to check the power status between 1 minutes to 6 hours, and if there is a loss of power, you can have the plugin email a list of email addresses.

Then you can configure up to 2 different battery levels (between 30-90%) where additional emails would be sent.

Second, you can create a Battery & UPS monitor device, which will allow you to create triggers against it's states. By creating a trigger you can monitor the status of the UPS, and send Emails, or perform actions depending on the power or battery condition.

The states that are being stored in the device are:

* AC Power (True / False)
* Battery Level (% of charge)
* Battery Time Remaining (In seconds) [This is zero if on AC power]
* Charging (True / False)
* Model of the UPS
* TimeDateStamp - The Time & Date that the Battery and/or UPS was checked.

This first release is a trial release for v1.00.

The plugin is still logging some data to the indigo log:

For example:

Code: Select all
Feb 1, 2013 1:49:37 PM
  UPS and Battery Monitor            Power Status    - AC Power
  UPS and Battery Monitor            UPS Model       -  -Smart-UPS 1500 RM FW:617.3.D USB FW:1.5
  UPS and Battery Monitor            Charging Status - True
  UPS and Battery Monitor            Battery Charge  - 100
  UPS and Battery Monitor            Sleeping for 1 minutes (60)


https://dl.dropbox.com/u/241415/Battery ... tor100.zip

Please feel free to use it!

I would appreciate it if anyone with a laptop try the plugin, and send me the results. I don't have a laptop to check to see how the plugin will work with it. I believe the code should work identically with a laptop battery vs an UPS, but the proof is in the pudding.

- Benjamin

------
My Plugins for Indigo (v4, v5, and v6) - http://bit.ly/U8XxPG

Security Script for v4 - http://bit.ly/QTgclf
for v5 - http://bit.ly/T6WBKu

Support Forum(s) - http://www.perceptiveautomation.com/userforum/viewforum.php?f=33

Posted on
Fri Feb 01, 2013 4:05 pm
nsheldon offline
Posts: 1848
Joined: Aug 09, 2010
Location: CA

Re: Battery & UPS Monitor plugin.

Great contribution Ben!

I installed it on my Indigo server (Mac mini, Mac OS X 10.7.5, Indigo 6b5). It seems to be working well overall. There were a whole bunch of errors on initial plugin configuration, but it's working fine now. I see this as potentially being very useful.

Here are the errors I got...
Installing and enabling plugin "UPS and Battery Monitor" v1.00
Loading plugin "UPS and Battery Monitor 1.00"
Starting plugin "UPS and Battery Monitor 1.00" (pid 88244)
UPS and Battery Monitor Error Unable to reach update server.
Started plugin "UPS and Battery Monitor 1.00"
UPS and Battery Monitor Power Status - AC Power
UPS and Battery Monitor UPS Model - -Back-UPS RS 1500 FW:8.g9 .D USB FW:g9
UPS and Battery Monitor Charging Status - True
UPS and Battery Monitor Battery Charge - 100
UPS and Battery Monitor Error Error in plugin execution runConcurrentThread:

Traceback (most recent call last):
File "plugin.py", line 177, in runConcurrentThread
<type 'exceptions.KeyError'>: 'key Timing not found in dict'

UPS and Battery Monitor Error plugin runConcurrentThread function returned or failed (will attempt again in 10 seconds)

UPS and Battery Monitor Power Status - AC Power
UPS and Battery Monitor UPS Model - -Back-UPS RS 1500 FW:8.g9 .D USB FW:g9
UPS and Battery Monitor Charging Status - True
UPS and Battery Monitor Battery Charge - 100
UPS and Battery Monitor Error Error in plugin execution runConcurrentThread:

Traceback (most recent call last):
File "plugin.py", line 177, in runConcurrentThread
<type 'exceptions.KeyError'>: 'key Timing not found in dict'

UPS and Battery Monitor Error plugin runConcurrentThread function returned or failed (will attempt again in 10 seconds)


...(above lines repeat 5 more times).

Feb 1, 2013 11:30:17 AM
UPS and Battery Monitor Power Status - AC Power
UPS and Battery Monitor UPS Model - -Back-UPS RS 1500 FW:8.g9 .D USB FW:g9
UPS and Battery Monitor Charging Status - True
UPS and Battery Monitor Battery Charge - 100
UPS and Battery Monitor Sleeping for 1 minutes (60)

I'm assuming the errors are related to the new way in which Indigo 6 creates devices before they've been configured.

If I may make an additional feature suggestion, it'd be nice to optionally squelch the logging (not just in this plugin but in many of your others as well, such as Find My iDevice), perhaps with a plugin configuration checkbox much like the debugging checkbox found in the Perceptive Automation included plugins (and mine too, actually).

In any case, thanks for all the great and exceptionally useful plugins!

Posted on
Fri Feb 01, 2013 6:59 pm
bschollnick2 offline
Posts: 1294
Joined: Oct 17, 2004
Location: Rochester, Ny

Re: Battery & UPS Monitor plugin.

v1.01 is out.

Notable Fixes include:

* an issue with a false positive on charging.
* Fix initial install startup having error messages
* Adding support for no Battery / UPS (AC Power only).

Download link: https://dl.dropbox.com/u/241415/Battery ... tor101.zip

------
My Plugins for Indigo (v4, v5, and v6) - http://bit.ly/U8XxPG

Security Script for v4 - http://bit.ly/QTgclf
for v5 - http://bit.ly/T6WBKu

Support Forum(s) - http://www.perceptiveautomation.com/userforum/viewforum.php?f=33

Posted on
Fri Feb 01, 2013 9:09 pm
tenallero offline
Posts: 94
Joined: Oct 31, 2011
Location: Barcelona

Re: Battery & UPS Monitor plugin.

I am running Indigo 6.beta5
on Mac mini with UPS

with release 1.00, the UPS was recognized:

UPS and Battery Monitor Error Unable to reach update server.
Started plugin "UPS and Battery Monitor 1.00"
UPS and Battery Monitor Power Status - AC Power
UPS and Battery Monitor UPS Model - -ELLIPSE
UPS and Battery Monitor Charging Status - True
UPS and Battery Monitor Battery Charge - 100
UPS and Battery Monitor Error Error in plugin execution runConcurrentThread:

Traceback (most recent call last):
File "plugin.py", line 177, in runConcurrentThread
<type 'exceptions.KeyError'>: 'key Timing not found in dict'


With release 1.01, not:

Starting plugin "UPS and Battery Monitor 1.01" (pid 4659)
UPS and Battery Monitor Error Unable to reach update server.
Started plugin "UPS and Battery Monitor 1.01"
UPS and Battery Monitor Power Status - AC Power - No Battery
UPS and Battery Monitor UPS Model - No Battery or UPS
UPS and Battery Monitor Charging Status - False
UPS and Battery Monitor Battery Charge - 0
UPS and Battery Monitor Sleeping for 5 minutes (300)

Posted on
Sat Feb 02, 2013 7:02 am
keifer1 offline
Posts: 86
Joined: Oct 21, 2012

Re: Battery & UPS Monitor plugin.

Great plugin! Thanks.
Worked with starting errors on ver 1 but have same errors on 1.1 as tenallero above.

Computer Keith
Mt Pleasant SC - Charleston
Indi-Go -Go-Go

Posted on
Sat Feb 02, 2013 7:24 am
bschollnick2 offline
Posts: 1294
Joined: Oct 17, 2004
Location: Rochester, Ny

Re: Battery & UPS Monitor plugin.

keifer1 wrote:
Great plugin! Thanks.
Worked with starting errors on ver 1 but have same errors on 1.1 as tenallero above.


Okay, I'll have to fix that (Obviously!).

I realized that I had not accounted for AC only, with no battery. So I made a small mistake, go ahead and revert.

- Ben

------
My Plugins for Indigo (v4, v5, and v6) - http://bit.ly/U8XxPG

Security Script for v4 - http://bit.ly/QTgclf
for v5 - http://bit.ly/T6WBKu

Support Forum(s) - http://www.perceptiveautomation.com/userforum/viewforum.php?f=33

Posted on
Sat Feb 02, 2013 11:08 am
matt (support) offline
Site Admin
User avatar
Posts: 18068
Joined: Jan 27, 2003
Location: Texas

Re: Battery & UPS Monitor plugin.

Terrific contribution. Thanks Ben!

Image

Posted on
Sat Feb 02, 2013 1:16 pm
tenallero offline
Posts: 94
Joined: Oct 31, 2011
Location: Barcelona

Re: Battery & UPS Monitor plugin.

bschollnick2,

Sorry, It was very late by night and only had energy to report the bug.
But this plugin, and the rest of your job, is great!
Thanks,

Ramon

Posted on
Sun Feb 03, 2013 7:23 am
bschollnick2 offline
Posts: 1294
Joined: Oct 17, 2004
Location: Rochester, Ny

Re: Battery & UPS Monitor plugin.

v1.02 is now tested, and fixes the AC Only power detection bug.

https://dl.dropbox.com/u/241415/Battery ... tor102.zip

- Ben

------
My Plugins for Indigo (v4, v5, and v6) - http://bit.ly/U8XxPG

Security Script for v4 - http://bit.ly/QTgclf
for v5 - http://bit.ly/T6WBKu

Support Forum(s) - http://www.perceptiveautomation.com/userforum/viewforum.php?f=33

Posted on
Sun Feb 03, 2013 10:08 pm
DU Lou offline
Posts: 260
Joined: Mar 08, 2012
Location: Florida

Re: Battery & UPS Monitor plugin.

Great plugin Ben!!!

So here is a general question for Matt/Ben or anyone more savvy with Mac innards than me :)

So I have my Mac power settings to:
1.Never sleep
2.Shutdown with 10 minutes of UPS power remaining
3.Start up automatically after a power failure

The issue is that I think a "Start up automatically after a power failure" only works after a bad shutdown. When the Mac shuts down gracefully defined by the parameters in the power settings when connected to an UPS, there will be no restart when power is restored since the Mac shutdown was graceful.

I found another forum post which eludes to code that looks like it can modify the Mac to do a graceful shutdown but somehow trick it into thinking there was a power failure so when power is restored the Mac powers back up. This would be crucial for us automation freaks who rely on our Macs to run 24/7 as much as possible.

So my question for Matt/Ben/anyone smarter with this stuff than me is: Would you mind taking a look at these links to make sense of the software since I feel it would have value to many on this forum? OR Is there another solution you would recommend?

Copy from Apple Support Communities forum post (on second page):
https://discussions.apple.com/thread/2543898?start=0&tstart=0
Code: Select all
Glenn Gerrard
Re: Start up automatically after a power failure - APC UPS
Aug 17, 2010 3:39 PM (in response to Pondini)
OK - I got it to work.

Euchre - I quoted the wrong UPS, it is the Back-UPS 650 CS, as you can see from the extensive manual (http://www.apcmedia.com/salestools/ASTE-6Z7VBGR0EN.pdf) I had nothing to go on

Pondini - Many thanks for recreating the issue and creating the trouble ticket - it seems things are "working as designed" though

The underlying issue(s) is that the UPS doesn't power-cycle itself (as suggested by Bsteely) and that even when it does, if the shutdown is graceful (ex. shutdown -h now) then the Mac has no reason to come back on when the UPS does power cycle.

Here's the how/what/why of getting it working:
installed apcupsd (http://www.apcupsd.com/) and followed the instructions for install/setup there (this removes the UPS tab in System Preferences)

edited the launchd entry for apcupsd to start with the -p --kill-on-powerfail switch. This tells the APC to shut itself off if the power fails (on the Back-UPS the fixed time for this is a ~180 second window)

edited the apcupsd.conf file to shutdown the iMac in 60 seconds or 50% of battery power left, or 5 minutes of battery power left which ever comes first

edited the apccontrol file to call /usr/libexec/upsshutdown instead of shutdown

edited /usr/libexec/upsshutdown to send "shutdown -hu now" in both a normal shutdown or UPS shutdown. the -u simulates a dirty shutdown (see man shutdown) so when the UPS does powercycle it brings the Mac back online.

I have tested pulling the power and within 60 seconds the Mac goes down, about 2 minutes later the UPS shuts down. When main power is back the UPS comes back and the Mac reboots. Additionally, if main power comes back during the 2 minute window, the UPS still goes down and then power cycles (so it doesn't power cycle twice if you follow).

I would prefer not to have used the -p --kill-on-powerfail switch, however I could not figure out how to insert the "normal" -killpower switch via apccontrol in the halt script (or where the halt script is in OS X actually) but I am open to suggestions.

I think the UPS / Start up after a power failure is working - if the UPS drains completely the Mac goes down hard, but reboots as soon as power is back. I think it's a case of working as designed versus working as expected!

Thanks,
Glenn
iMac, Mac OS X (10.6)
Like (0)


Direct link to above mentioned UPS management software:
http://www.apcupsd.com/

Thank you all,
~Lou

Posted on
Sun Feb 03, 2013 11:08 pm
automaton offline
Posts: 105
Joined: May 23, 2008

Re: Battery & UPS Monitor plugin.

I use APCUPSD to control my UPS and to shut down the mac(s) as described. Here is my best shot at answering. I hope it helps.

If you issue the shell command "shutdown -hu now", it will shutdown the mac so that if you then remove the power and restore it, it will restart automatically. When this is used with APCUPSD, you set your APCUPSD scripts to kill the UPS power just after the mac shuts down with this command.

So... you want to shut down using this "shutdown -hu now" command, and then command the ups to kill the power to the mac. The first part could easily be done via the plugin, but I'm not sure (?) if the plugin can command the UPS to kill the AC power to the Mac. This would be needed if you want to completely control the shut down of the mac from the plugin.

Posted on
Mon Feb 04, 2013 6:10 am
DU Lou offline
Posts: 260
Joined: Mar 08, 2012
Location: Florida

Re: Battery & UPS Monitor plugin.

automaton wrote:
I use APCUPSD to control my UPS and to shut down the mac(s) as described. Here is my best shot at answering. I hope it helps.

If you issue the shell command "shutdown -hu now", it will shutdown the mac so that if you then remove the power and restore it, it will restart automatically. When this is used with APCUPSD, you set your APCUPSD scripts to kill the UPS power just after the mac shuts down with this command.


So are their multiple scripts? One to send the appropriate command to the Mac and another to tell the UPS what to do? I am relatively new to OS X and while I am comfortable in say a Windows registry, I don’t want to hose my Mac by using a wrong switch :)

So... you want to shut down using this "shutdown -hu now" command, and then command the ups to kill the power to the mac. The first part could easily be done via the plugin, but I'm not sure (?) if the plugin can command the UPS to kill the AC power to the Mac. This would be needed if you want to completely control the shut down of the mac from the plugin.


It hadn’t even occurred to me that there might be an option to incorporate APCUPSD in Ben’s plugin. I was just asking in a broader view on how reliable APCUSD is and how difficult is it to setup. BUT you do bring up an intriguing idea! Do you think it would be possible Ben? Challenge!!!!! :mrgreen:

Posted on
Mon Feb 04, 2013 11:04 am
macpro offline
User avatar
Posts: 682
Joined: Dec 29, 2005
Location: Third byte on the right

Re: Battery & UPS Monitor plugin.

This sounds great.
I have two Macs and a Raspberry PI and they all have a UPS.
APCUPSD is monitoring them, so an Indigo plugin that can monitor APCUPSD sites would be great.

(Before anyone starts asking: the Raspberry PI is only used to run APCUPSD to monitor the UPS. On that UPS I'm running my internet modem, router, main switch etc.)

"I tawt I taw a puddy tat!" Tweety

Posted on
Tue Feb 05, 2013 9:34 am
automaton offline
Posts: 105
Joined: May 23, 2008

Re: Battery & UPS Monitor plugin.

I didn't intend to say that an Indigo plugin should do all the tasks that APCUPSD performs. That might be a big job, since it communicates with so many models of UPS, but maybe the Apple drivers can simplify it. I'll leave that to the developer.

APCUPSD has a daemon that monitors the status of the UPS,and can also control the UPS. When the daemon detects certain states (power out, low battery, etc.) then it calls user-modifiable shell scripts that can send emails, modify Indigo variables or whatever you would like to do. It also has a network function and can monitor multiple UPS or shut down multiple computers.

I have found it to work great, however the trickiest part is getting the Mac to startup after the UPS has been exhausted. As you have discovered, if you don't do it right, the Mac won't start up when the power is restored. This involves getting two things just right: the "simulated dirty shutdown of the Mac" and then having APCUPSD kill the UPS immediately after. The "simulated dirty shutdown" is accomplished by using the "shutdown -hu now" command in the appropriate APCUPSD battery/runtime exhausted script, and the UPS Kill power is done by using the -p --kill-on-powerfail switch in the startup script that calls the daemon when your mac starts up.

Getting all of this to work perfectly requires some patience and testing time, and the implementation varies a bit depending on the MacOS version you are using. I haven't found any good MacOS documentation for this (the snippet you found is about the best), so it is somewhat trial and error. I can't remember exactly what I did, but will look around to see if I have some specifics.

The plugin looks great. I don't know if the can be run in parallel with APCUPSD (I'm guessing not), so you may have to choose depending on your needs and the complexity of your UPS setup.

Posted on
Tue Feb 05, 2013 11:45 am
bschollnick2 offline
Posts: 1294
Joined: Oct 17, 2004
Location: Rochester, Ny

Re: Battery & UPS Monitor plugin.

automaton wrote:
The plugin looks great. I don't know if the can be run in parallel with APCUPSD (I'm guessing not), so you may have to choose depending on your needs and the complexity of your UPS setup.


Well, I can't fix the issue with the Mac OS X / Energy Saver control panel not processing a shutdown from APCUSPSD correctly.

But I might be able to get the data from the APCUPSD driver.

It appears that /sbin/apcaccess is the driver?

Can you open a terminal window, and type /sbin/apcaccess and let me know the results? It should list the details for your UPS.

Beyond getting this additional detail, how else can this plugin help?

- Ben

------
My Plugins for Indigo (v4, v5, and v6) - http://bit.ly/U8XxPG

Security Script for v4 - http://bit.ly/QTgclf
for v5 - http://bit.ly/T6WBKu

Support Forum(s) - http://www.perceptiveautomation.com/userforum/viewforum.php?f=33

Who is online

Users browsing this forum: No registered users and 1 guest