Page 1 of 2
APC UPS Monitoring
Posted: Wednesday 15 October 2014 19:55
by kesikun
Here is a way (maybe not the most elegant) to monitor an APC ups and display data in Domoticx on a PI via USB
Use this guide to get the UPS daemon working on your PI
http://raspisimon.no-ip.org/ups.php
In Domoticz create your virtual devices - in my case I created four devices to monitor but if you look at the apcaccess status command there are quite a few variables if you choose to add
Line voltage
Temperature
Load
and finally on battery or not
Here is the bash script - you will need to change the idx numbers to align to your devices
I called my readapc.sh in /home/pi/domoticz/scripts
Code: Select all
#!/bin/bash
#Script to grab APC status and update domoticz vars
SERVER='localhost:8080/'
SERVER_CMD='json.htm?type=command¶m=udevice&'
SWITCH_CMD='json.htm?type=command¶m=switchlight&'
cap_data=`apcaccess status`
#apc_total = "apcaccess status"
#echo "$cap_data"
apc_stat=`awk '/STATUS / {print $3}' <<< "$cap_data"`
apc_volts=`awk '/LINEV / {print $3}' <<< "$cap_data"`
apc_itemp=`awk '/ITEMP / {print $3}' <<< "$cap_data"`
apc_load=`awk '/LOADPCT / {print $3}' <<< "$cap_data"`
#echo $apc_stat
#echo $apc_volts
#echo $apc_itemp
#echo $apc_load
hostcmd="http://${SERVER}${SERVER_CMD}idx=25&nvalue=0&svalue=${apc_itemp}"
curlres=`curl -sS $hostcmd`
hostcmd="http://${SERVER}${SERVER_CMD}idx=26&nvalue=0&svalue=${apc_volts}"
curlres=`curl -sS $hostcmd`
hostcmd="http://${SERVER}${SERVER_CMD}idx=34&nvalue=0&svalue=${apc_load}"
curlres=`curl -sS $hostcmd`
if [ "$apc_stat" = "ONLINE" ]; then
#echo "APC SYSTEM IS ONLINE"
hostcmd="http://${SERVER}${SWITCH_CMD}idx=36&switchcmd=On&level=0"
curlres=`curl -sS $hostcmd`
else
#echo "APC SYSTEM IS OFFLINE"
hostcmd="http://${SERVER}${SWITCH_CMD}idx=36&switchcmd=Off&level=0"
curlres=`curl -sS $hostcmd`
fi
make it executable
chmod +x readapc.sh
test the script to make sure the vars are updating
./readapc.sh
add a cron to run it every minute
crontab -e
add the following to the end of the file
* * * * * sudo ~/domoticz/scripts/readapc.sh
Enjoy ...
Modbreak: i added Code: Select all
tags to improve readability (ThinkPad)[/b][/color]
Re: APC UPS Monitoring
Posted: Tuesday 13 January 2015 2:56
by Rene
I made one too, this one is in Python script for whoever wants it, now with auto shutdown when battery power is down to 10%
Code: Select all
#!/usr/bin/python
#
# Script to read APC battery backups settings and voltages.
# And publish these to domoticz software.
#
# (C)reated by Rtm-soft Jan 2015
#
# This code is Free Software and comes with absolutely
# no warranty and may freely be modified and distributed.
#Modules used in this program
import subprocess #For OS calls
import time
import requests #For URL calls
#Install APC driver with:
#
#Read https://help.ubuntu.com/community/apcupsd or follow below lines
#
#Quick Install for USB-based APC units
# install the daemon: apt-get install apcupsd
# edit /etc/apcupsd/apcupsd.conf and change these lines
# UPSNAME myownups
# UPSCABLE usb
# UPSTYPE usb
# comment out DEVICE (it contains a TTY link, which will prevent it from working)
# edit /etc/default/apcupsd
# change ISCONFIGURED from no to yes
# /etc/init.d/apcupsd start
# apcaccess
#Shutdown access to user without password:
#
#sudo visudo -f /etc/sudoers.d/myOverrides
#
#<myusername> ALL = NOPASSWD: /sbin/shutdown
#Constants (Modify these to match your own settings)
DOMOTICZ_URL = "http://localhost:8080" #http://<username:password@>domoticz-ip<:port>
APCACCESS = "/sbin/apcaccess" #Location of apcaccess program
LINEV = "40" #Idx for Line Voltage
BATTV = "44" #Idx for Battery Voltage
LOADPCT = "45" #Idx for Load Percentage
BCHARGE = "46" #Idx for Battery Charge
MINBATT = 10 #Shut down when battery power is at 10%
#Variables
def apc_probe() :
batt = 100 #Needs to be >MINBATT to not do a false processor stop
dict = {'LINEV' : LINEV, 'BATTV' : BATTV, 'LOADPCT' : LOADPCT, 'BCHARGE' : BCHARGE } #Convert Keyword to Domoticz index.
while True : #Endless loop
res = subprocess.check_output(APCACCESS)
for line in res.split('\n') :
(key,spl,val) = line.partition(': ')
key = key.rstrip() #Strip spaces right of text
val = val.strip() #Remove outside spaces
if key == 'STATUS' and 'ONBATT' in val and batt < MINBATT : #If there is less then 10 percent battery power left and we are offline then shutdown
subprocess.call("sudo shutdown -h now", shell=True) #Edit /etc/sudoers.d/myOverrides to make shutdown without password work
val = val.split(' ',1)[0] #Split using space and only take first part
if key in dict : #Are we interested in this parameter?
if key == 'BCHARGE' :
batt = int(float(val)) #Save battery level for Offline mode.
url= DOMOTICZ_URL+"/json.htm?type=command¶m=udevice&idx="+dict[key]+"&nvalue=0&svalue="+val #Publish value to Domoitcz by means of json.
requests.get(url) #Print url
time.sleep(60) #Take a minute break
#Main Loop
print "APC Probe running..."
apc_probe()
Re: APC UPS Monitoring
Posted: Tuesday 13 January 2015 11:19
by LanWolf
what type sensor you use for these ? especially Line Voltage ?
> I mean in domoticz, what type sensors you add to put the values to. I know you can add dummy sensors, but which type is necessary for these.
Re: APC UPS Monitoring
Posted: Wednesday 14 January 2015 0:43
by Rene
The APC (Brand) is a battery backup (UPS), most of them have a USB cable that you can plug into the device that run Domoticz. The voltage sensor is build into the APC. So you will be reading the line voltage that the UPS is measuring....
Re: APC UPS Monitoring
Posted: Saturday 17 January 2015 18:13
by vmb
Will this script also shutdown the Pi when running on battery power? Or do I have to add Some values or perhaps with blockly?
Re: APC UPS Monitoring
Posted: Saturday 17 January 2015 20:04
by Hendrik
what type off cable do you use?
when i search the internet for a usb to apc cable, they are very expensive
Re: APC UPS Monitoring
Posted: Sunday 18 January 2015 4:27
by Rene
vmb wrote:Will this script also shutdown the Pi when running on battery power? Or do I have to add Some values or perhaps with blockly?
I modified the script to do a shutdown -h when the battery power is down to 10% and the UPS in ONBATT. It requires to make shutdown to work without password, the instructions are in the python script or you can do:
sudo visudo -f /etc/sudoers.d/myOverrides
and add this line with ofcourse myusername your username (its the name before the @ in the prompt)
myusername ALL = NOPASSWD: /sbin/shutdown
it then requires a reboot to make it work.
Greeting from Rene
Re: APC UPS Monitoring
Posted: Sunday 18 January 2015 4:40
by Rene
Hendrik wrote:what type off cable do you use?
when i search the internet for a usb to apc cable, they are very expensive
My APC ES-650 came with the cable. Check this link
http://pinoutsguide.com/UPS/apc_usb_cable_pinout.shtml
Looks like it is a RJ50 but the crimper for this might not be easy to find.
Greetings Rene
Re: APC UPS Monitoring
Posted: Sunday 18 January 2015 11:51
by vmb
Rene wrote:vmb wrote:Will this script also shutdown the Pi when running on battery power? Or do I have to add Some values or perhaps with blockly?
I modified the script to do a shutdown -h when the battery power is down to 10% and the UPS in ONBATT. It requires to make shutdown to work without password, the instructions are in the python script or you can do:
sudo visudo -f /etc/sudoers.d/myOverrides
and add this line with ofcourse myusername your username (its the name before the @ in the prompt)
myusername ALL = NOPASSWD: /sbin/shutdown
it then requires a reboot to make it work.
Greeting from Rene
Thx will check it out, first need to buy an Apc.
Re: APC UPS Monitoring
Posted: Wednesday 19 August 2015 14:40
by TheCondor
I edited the first script just for getting the status (on battery / on line) of my APC monitored by a local NUT instance, thanks for your work guys:
Code: Select all
#!/bin/bash
#Script to grab APC status and update domoticz vars
SERVER='localhost:8077/'
SERVER_CMD='json.htm?type=command¶m=udevice&'
SWITCH_CMD='json.htm?type=command¶m=switchlight&'
apc_stat=`upsc APC |grep ups.status`
#echo $apc_stat
if [ "$apc_stat" = "ups.status: OB DISCHRG" ]; then
#echo "APC SYSTEM IS OFFLINE"
hostcmd="http://127.0.0.1:8077/json.htm?type=command¶m=switchlight&idx=212&switchcmd=Off"
curlres=`curl -sS $hostcmd`
else
#echo "APC SYSTEM IS ONLINE"
hostcmd="http://127.0.0.1:8077/json.htm?type=command¶m=switchlight&idx=212&switchcmd=On"
curlres=`curl -sS $hostcmd`
fi
Re: APC UPS Monitoring
Posted: Monday 07 December 2015 20:26
by macieiks
Hey guys,
I have two Domoticz servers based on Raspberry Pi in different locations. Both have separate APC UPS connected via USB interface. I configured apcupsd package and it is working perfectly. However I have one problem with situation when Raspberry is being shutdown due to low battery percentage when using on-battery. When power is being restored my raspberry does not automatically power on back. Manual reset is needed to restore my Domoticz service. Do you have the solution / workaround for this situation
??
Thanks in advance
Maciek
Re: APC UPS Monitoring
Posted: Monday 07 December 2015 20:35
by jannl
Hm. Strange. When my Pi gets power back it boots. Do you switch of the ups when the Pi shuts down. Not sure if that is possible. My NAS actually only stops all services and stops the disks. After that the NAS switches of the UPS which stops the NAS completely. When the power comes back online both the NAS and the pi power up.
Re: APC UPS Monitoring
Posted: Monday 07 December 2015 21:52
by macieiks
I have connected only Pi + router to my APC. When apcupsd will detect battery percentage (or timeleft) below estimated threshold it automatically sends command to RPI to shutdown (system is shutdown, RPI power led is still flashing). However when the power is restored (and there is still some % of battery) RPI does not boot because it is in this "idle/poweroff" mode. So there is a problem because I have battery threshold setup for 15%, and when I am below these 15% my RPI is being shutdown. There are two scenarios:
a) if the power will be restored in range 1%-14% my RPI will be unreachable after power backup (I need to unplug/plug the usb cable),
b) if the power will not be restored and battery will be drained till the end (0%) APC will shutdown completely and then it will go up when power will be back, in this scenario it is obvious that RPI will come up
but this is not good for battery condition. So I would like to know if there is any way that I could safely shutdown my RPI and after 1 min later shutdown APC itself / or sockets? Then when power will be back APC would just turn on?
Re: APC UPS Monitoring
Posted: Tuesday 08 December 2015 6:58
by jannl
Not sure if that can be the last action the pi does before it shutsdown itself.
Re: APC UPS Monitoring
Posted: Friday 13 May 2016 17:01
by SHadley1138
i think the command structure of 'apcaccess status' has changed. do you have an updated version?
Re: APC UPS Monitoring
Posted: Friday 04 November 2016 21:17
by tOmki
How to change the first script to use Electricity (instant and counter) or Counter dummy sensor ?
Re: APC UPS Monitoring
Posted: Wednesday 04 October 2017 17:17
by Mace
Script doesn't seem to work. I can monitor the values via CGI, but running the OPS script gets me no values...
Re: APC UPS Monitoring
Posted: Saturday 07 October 2017 12:36
by underscore4
hi,Mace, with problem do you face?
I had some initial problems but i solved with this script...of couse the date are suitable for me, but you clud change it for your need it.
It's based on kesikum's script where i have setup blockly to have status change on icon.
Code: Select all
#!/bin/bash
#update domoticz by curl commad;
#Chenge IP and port of your domoticz and IDX to read correct value;
SERVER='yourip:port/'
SERVER_CMD='json.htm?type=command¶m=udevice&'
SWITCH_CMD='json.htm?type=command¶m=switchlight&'
cap_data=`apcaccess status`
#apc_total = "apcaccess status"
#echo "$cap_data"
apc_stat=`awk '/STATUS / {print $3}' <<< "$cap_data"`
apc_itemp=`awk '/ITEMP / {print $3}' <<< "$cap_data"`
apc_load=`awk '/LOADPCT / {print $3}' <<< "$cap_data"`
apc_bcharge=`awk '/BCHARGE / {print $3}' <<< "$cap_data"`
apc_bdc=`awk '/BATTV / {print $3}' <<< "$cap_data"`
#echo $apc_stat
#echo $apc_itemp
#echo $apc_load
#echo $apc_bcharge
#echo $apc_bdc
hostcmd="http://${SERVER}${SERVER_CMD}idx=97&nvalue=0&svalue=${apc_itemp}"
curlres=`curl -sS $hostcmd`
hostcmd="http://${SERVER}${SERVER_CMD}idx=98&nvalue=0&svalue=${apc_load}"
curlres=`curl -sS $hostcmd`
hostcmd="http://${SERVER}${SERVER_CMD}idx=99&nvalue=0&svalue=${apc_bcharge}"
curlres=`curl -sS $hostcmd`
hostcmd="http://${SERVER}${SERVER_CMD}idx=100&nvalue=0&svalue=${apc_bdc}"
curlres=`curl -sS $hostcmd`
if [ "$apc_stat" = "ONLINE" ]; then
#echo "APC SYSTEM IS ONLINE"
hostcmd="http://${SERVER}${SWITCH_CMD}idx=96&switchcmd=On&level=0"
curlres=`curl -sS $hostcmd`
else
#echo "APC SYSTEM IS OFFLINE"
hostcmd="http://${SERVER}${SWITCH_CMD}idx=96&switchcmd=Off&level=0"
curlres=`curl -sS $hostcmd`
fi
set dummy alarm sensor and setup with it
Code: Select all
http://yourip:port/json.htm?type=command¶m=udevice&idx=102&nvalue=1&svalue=Online
http://yourip:port/json.htm?type=command¶m=udevice&idx=102&nvalue=4&svalue=On battery
Re: APC UPS Monitoring
Posted: Saturday 21 October 2017 22:40
by assenzuid
How to remove the .000 behind the Volt sensors?
- Capture.JPG (29.53 KiB) Viewed 18806 times
Re: APC UPS Monitoring
Posted: Saturday 21 October 2017 22:46
by assenzuid
Rene wrote: ↑Tuesday 13 January 2015 2:56
I made one too, this one is in Python script for whoever wants it, now with auto shutdown when battery power is down to 10%
Code: Select all
#!/usr/bin/python
#
# Script to read APC battery backups settings and voltages.
# And publish these to domoticz software.
#
# (C)reated by Rtm-soft Jan 2015
#
# This code is Free Software and comes with absolutely
# no warranty and may freely be modified and distributed.
#Modules used in this program
import subprocess #For OS calls
import time
import requests #For URL calls
#Install APC driver with:
#
#Read https://help.ubuntu.com/community/apcupsd or follow below lines
#
#Quick Install for USB-based APC units
# install the daemon: apt-get install apcupsd
# edit /etc/apcupsd/apcupsd.conf and change these lines
# UPSNAME myownups
# UPSCABLE usb
# UPSTYPE usb
# comment out DEVICE (it contains a TTY link, which will prevent it from working)
# edit /etc/default/apcupsd
# change ISCONFIGURED from no to yes
# /etc/init.d/apcupsd start
# apcaccess
#Shutdown access to user without password:
#
#sudo visudo -f /etc/sudoers.d/myOverrides
#
#<myusername> ALL = NOPASSWD: /sbin/shutdown
#Constants (Modify these to match your own settings)
DOMOTICZ_URL = "http://localhost:8080" #http://<username:password@>domoticz-ip<:port>
APCACCESS = "/sbin/apcaccess" #Location of apcaccess program
LINEV = "40" #Idx for Line Voltage
BATTV = "44" #Idx for Battery Voltage
LOADPCT = "45" #Idx for Load Percentage
BCHARGE = "46" #Idx for Battery Charge
MINBATT = 10 #Shut down when battery power is at 10%
#Variables
def apc_probe() :
batt = 100 #Needs to be >MINBATT to not do a false processor stop
dict = {'LINEV' : LINEV, 'BATTV' : BATTV, 'LOADPCT' : LOADPCT, 'BCHARGE' : BCHARGE } #Convert Keyword to Domoticz index.
while True : #Endless loop
res = subprocess.check_output(APCACCESS)
for line in res.split('\n') :
(key,spl,val) = line.partition(': ')
key = key.rstrip() #Strip spaces right of text
val = val.strip() #Remove outside spaces
if key == 'STATUS' and 'ONBATT' in val and batt < MINBATT : #If there is less then 10 percent battery power left and we are offline then shutdown
subprocess.call("sudo shutdown -h now", shell=True) #Edit /etc/sudoers.d/myOverrides to make shutdown without password work
val = val.split(' ',1)[0] #Split using space and only take first part
if key in dict : #Are we interested in this parameter?
if key == 'BCHARGE' :
batt = int(float(val)) #Save battery level for Offline mode.
url= DOMOTICZ_URL+"/json.htm?type=command¶m=udevice&idx="+dict[key]+"&nvalue=0&svalue="+val #Publish value to Domoitcz by means of json.
requests.get(url) #Print url
time.sleep(60) #Take a minute break
#Main Loop
print "APC Probe running..."
apc_probe()
I'm using this great script, but how to be sure that its running after a reboot op my PI?