0x00 Overview


Monitoring APC UPS with Domoticz.

0x01 Setup


The most easy (and probably most ugly) way ot doing this is with a simple external script that gets
the information from apcaccess utility and pushes it to Domoticz.
We will monitor 4 values:

You will need to create dummy devices for all of them.
When you create the devices, don't forget to get their IDs - we will need.


0x02 The Script


This is a small remake of a script that I found in Domoticz's forum.

#!/usr/bin/python

import subprocess
import time
import requests


DOMOTICZ_URL = "http://localhost:8000"  #http://<username:password@>domoticz-ip<:port>
APCACCESS   = "/sbin/apcaccess" #Location of apcaccess program
LINEV       = "33"          #Idx for Line Voltage
BATTV       = "34"          #Idx for Battery Voltage
LOADPCT     = "35"          #Idx for Load Percentage
BCHARGE     = "36"          #Idx for Battery Charge


def apc_probe() :
    batt = 100
    dict = {'LINEV' : LINEV, 'BATTV' : BATTV, 'LOADPCT' : LOADPCT, 'BCHARGE' : BCHARGE }
    while True :
        res = subprocess.check_output(APCACCESS)
        for line in res.split('\n') :
            (key,spl,val) = line.partition(': ')
            key = key.rstrip()
            val = val.strip()
            val = val.split(' ',1)[0]
            if key in dict :
                if key == 'BCHARGE' :
                    batt = int(float(val))
                url= DOMOTICZ_URL+"/json.htm?type=command&param=udevice&idx="+dict[key]+"&nvalue=0&svalue="+val
                requests.get(url)
        time.sleep(300)

#Main Loop
print "APC Probe running..."
apc_probe()

Don't forget to update the IDs and Domoticz URL (& credentails if needed) before starting.

The original script was doing a shutdown and so on , but I don't think this is the right place for that.

0x03 Running the script


I'm guessing for most people using crontab to run this script will be the most used solution.
However, I personally think the place for this is the init. So in my case,this is how it runs
q1:3:respawn:/opt/domoticz/scripts/apc_ups.py
Ofcouse you are free to do this the way you this is right.

0x04 End result


UPS Big

UPS Small