APC UPS Binding

, ,

I have an APC UPS and I’m monitoring it from OpenHAB. It works fine, but it’s adding a lot of text to my events.log that I don’t really want or need. I’m using the Exec Binding (as described below and as described in previous posts). I can’t find an easy way to keep the Exec Binding from adding the text to the events.log. Any help or suggestions would be greatly appreciated.

I created a Thing using HABmin that looks like the following:


It runs the /sbin/apcaccess command every 5 seconds (if there is a power outage, I’d like to know about it fairly soon).

Then, I have a set of items defined to capture the results from the apcaccess command:

The following rule is used to parse the fields returned by the apcaccess command into the items that I’ve defined:

But I get the following every 5 seconds in the events.log, which obviously results in a lot of wasted disk space. It also makes it more difficult to find the really interesting information in the events.log file.

have a look into NUT Network Ups Tools

i own a APC ups with only a USB connection and thats how i get information from my UPS :wink:

Jeff:
Check out Hilbrand’s brand new binding

it is merged into M4 but will work in any 2.5 version

Hi

Just comment out the LogInfo line

	import java.io.StringReader
    import java.utils.Properties
	
	rule "APC UPS String spliting"
	when
		Item APCUPSQuery_Output received update // Change this Item to your "apcaccess -u" exec query output
//	 or Item testtrigger changed	// Only a test switch to force the rule to run for testing
	then 
	
	//	logInfo("APC Info", "APC UPS Query update = \n"+APCUPSQuery_Output.state)	// Change this Item to your "apcaccess -u" exec query output // Comment out this line to stop it populating your openhab.log file
	
        val results = new Properties().load(new StringReader(APCUPSQuery_Output.state.toString))
	
    //	logInfo("APC Info","APC UPS information.\nUPS Named "+results.get("MODEL")+" is "+results.get("STATUS")+", with an internal temperature of "+results.get("ITEMP")+"°C.\nLoad = "+results.get("LOADPCT")+" Percent, Current Line Voltage is "+results.get("LINEV")+"\nBattery is at "+results.get("BCHARGE")+" percent, giving "+results.get("TIMELEFT")+" minutes at current load, at "+results.get("OUTPUTV")+" Vac.\nCurrent battery voltage "+results.get("BATTV")+" Vdc")
	
	end

Hey Rich

Sorry to bother you.

I’ve had to rebuild my setup (because I completely messed it up) and part of that is trying to load this rule.

I’m running 2.5.3 now and the important line in this rule crashes out and reports
“Error during the execution of rule ‘APC UPS String splitting’: An error occurred during the script execution: null”

Have you any ideas as to what it might me?

import org.eclipse.smarthome.model.script.ScriptServiceUtil
import java.io.StringReader
import java.utils.Properties




rule "APC UPS String spliting"
when

		Time cron "0 0/1 * 1/1 * ? *"
		or Item testswitch01 changed to ON		// Only a test switch to force the rule to run for testing


then 
val String APCQUERY = executeCommandLine("/sbin/apcaccess -u", 5000)
	logInfo("APC Info", "APC UPS Query update = \n"+APCQUERY+"\n")	// Change this Item to your "apcaccess -u" exec query output // Comment out this line to stop it populating your openhab.log file

 
// This next line fails with the error "Error during the execution of rule 'APC UPS String splitting': An error occurred during the script execution: null"

   val results = new Properties().load(new StringReader(APCQUERY.toString)) 

//	logInfo("APC Info","APC UPS information.\nUPS Named "+results.get("MODEL")+" is "+results.get("STATUS")+", with an internal temperature of "+results.get("ITEMP")+"°C.\nLoad = "+results.get("LOADPCT")+" Percent, Current Line Voltage is "+results.get("LINEV")+"\nBattery is at "+results.get("BCHARGE")+" percent, giving "+results.get("TIMELEFT")+" minutes at current load, at "+results.get("OUTPUTV")+" Vac.\nCurrent battery voltage "+results.get("BATTV")+" Vdc")
			//	logInfo("APC Info","\n\n")
			testswitch01.sendCommand(OFF)
end

Split that line into multiple lines. There are three operations on that one line any one of which could be the cause of the error. Create the Properties and save to a variable. Create the StringReader and save to a variable. Then call load on the Properties variable using the StringReader variable. Intersperse log statements so you can figure out which line is failing.

1 Like

Best suggestion.
Thanks :slight_smile:

Hello!
So I’ve spent the last couple of days implementing a small python script that runs on my Linux boxes that run both run apcupsd.

This script polls the state of the UPSs, and posts any updates to MQTT topics, cleaning up the results so that the MQTT topics have nice OH-friendly values (no transformations required).

This also means that the OH config is pretty trivial - I end up with a things file like this:

Thing mqtt:topic:broker:ups-1      "MQTT - Office UPS" (mqtt:broker:broker) {
    Channels:
        Type string     : driver   "Driver"      [ stateTopic="ups-01/driver" ]
        Type string     : header   "Header"      [ stateTopic="ups-01/apc" ]
        Type datetime   : date     "Last seen"   [ stateTopic="ups-01/date"  ]
        Type string     : hostname "Host name"   [ stateTopic="ups-01/hostname" ]
        Type string     : name     "Name"        [ stateTopic="ups-01/upsname" ]
        Type string     : version  "Version"     [ stateTopic="ups-01/version" ]
        Type string     : cable    "Cable"       [ stateTopic="ups-01/cable" ]
        Type string     : model    "Model"       [ stateTopic="ups-01/model" ]
}

(trimmed - it actually has 59 channels!)

… the items are pretty much a 1:1 with the channels.

My solution works really nicely, but is a little more convoluted than those already described here.

… I’m also considering adding support for setting values / performing self-tests etc… (things to do during lockdown… :wink: )

… I’ll probably also see if I can build a docker image once I’m happy with things.

So I guess the question is - are there any APC UPS and OH users out there that are MQTT fans that would be interested in my approach? It’d only take me a little time to tidy my script and post it to github with some instructions if it would be useful?

Jamie.

2 Likes

Oh yes! Plesse share your script. I’d highly aprecciate… :+1:

Looking for some advice on which way to go. I have an APC UPS and ATS that I’d like to get some notifications from. Both are in my server rack and both have the Mgt IF Module that I use to connect to over the LAN using it’s web interface (no serial connection). I’d prefer not to install another prog if possible. I’m also running openHAB on a Windows 10 pc.

Any suggestions?

Thanks
Nathan

OK - so it’s very rough and ready - I’m not great at writing documentation, so I think you’re going to have to be reasonably knowledgeable in Linux, python & MQTT to follow my instructions at the moment, but I’ve put things up on github here https://github.com/JamieTemple/apcups-mqtt

… I will improve the documentation over the weekend - promise :slight_smile:

2 Likes

FYI - I ended up using the OpenHAB SNMP binding and was able to pull all the info I need (and much more) from my UPS and ATS using OIDs. The “fun” part was working out which of the plothora of OIDs I wanted. To do this I used:

  • iReasoning’s MIB Browser : This free tool will “walk” through and display all the OIDs for your device showing both the OID and the Values returned. The next bit is then to work out what a particular OID is
  • OIDRef : This on line reference give a discription of the OID (and the relationships with Partests, Siblings, and Children). Once you work our the navigation, it is not to bad to find the OIDs you want.
1 Like

I’m migrating my UPS from apcaccess to using SNMP as well and was curious if you implemented anything for OID 1.3.6.1.4.1.318.1.1.1.11.1.1 [ upsBasicStateOutputState].
It presents as a 64 character ASCII string of which each character represents a different state of the UPS.
Thanks

Hi Zack. My APC UPS died a horrible death (big bit of burnt out important looking stuff inside the unit). I’ve replaced it with a Cyberpower OLS3000ERT2UA and Mgt Card but not yet had the time to reset up the OID’s.

Hi,
just want to share my generic solution for apcupsd to OpenHAB integration (no exec, no timer, no polling, no binding, just OpenHAB REST APi).

I’ve put two shell scripts named “powerout” and “mainsback” in the directory /etc/apcupsd/ where on is called in case of power outage and the other when power will be back (adjust user, pw, protocol, host, port in curl command).

powerout

#!/bin/sh
curl -X POST --user <user>:<pw> --header "Content-Type: text/plain" --header "Accept: application/json" -d "OFF" "<http|https>://<host>:<port>/rest/items/vHOUS_MainsPower_Virtual_Contact"
exit 0

mainsback

#!/bin/sh
curl -X POST --user <user>:<pw> --header "Content-Type: text/plain" --header "Accept: application/json" -d "ON" "<http|https>://<host>:<port>/rest/items/vHOUS_MainsPower_Virtual_Contact"
exit 0

items file example:

...
Switch vHOUS_MainsPower_Virtual_Contact "Mains House Power Virtual Contact" <switch>
...

All other possible apcupsd (change) events may be used in the same way (e.g. loadlimit, onbattery, runlimit, etc.)

Greetings,
Frank

2 Likes