APC UPS Monitoring

All kinds of 'OS' scripts

Moderator: leecollings

User avatar
kesikun
Posts: 2
Joined: Wednesday 15 October 2014 19:22
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: UK
Contact:

APC UPS Monitoring

Post 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&param=udevice&'
SWITCH_CMD='json.htm?type=command&param=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]
Last edited by ThinkPad on Wednesday 14 January 2015 8:22, edited 1 time in total.
Reason: Added [code] tags to improve readability
Rene
Posts: 18
Joined: Tuesday 23 December 2014 2:07
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: APC UPS Monitoring

Post 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&param=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()					
Last edited by Rene on Sunday 18 January 2015 4:29, edited 2 times in total.
LanWolf
Posts: 36
Joined: Wednesday 20 August 2014 10:33
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: APC UPS Monitoring

Post 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.
Last edited by LanWolf on Wednesday 14 January 2015 15:39, edited 1 time in total.
*/ Just A Byte /*
Rene
Posts: 18
Joined: Tuesday 23 December 2014 2:07
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: APC UPS Monitoring

Post 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....
vmb
Posts: 26
Joined: Thursday 02 October 2014 14:35
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: APC UPS Monitoring

Post 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?
Hendrik
Posts: 18
Joined: Saturday 04 January 2014 20:20
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Netherlands
Contact:

Re: APC UPS Monitoring

Post by Hendrik »

what type off cable do you use?
when i search the internet for a usb to apc cable, they are very expensive
CubieTruck with Domoticz
RfxTrx 433Mhz Usb, Rfx Sensor, Z-Wave
Slave Raspberry Pi with Domoticz, Piface, P1 usb
Rene
Posts: 18
Joined: Tuesday 23 December 2014 2:07
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: APC UPS Monitoring

Post 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
Rene
Posts: 18
Joined: Tuesday 23 December 2014 2:07
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: APC UPS Monitoring

Post 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
vmb
Posts: 26
Joined: Thursday 02 October 2014 14:35
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: APC UPS Monitoring

Post 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.
TheCondor
Posts: 78
Joined: Thursday 18 June 2015 10:32
Target OS: Linux
Domoticz version:
Contact:

Re: APC UPS Monitoring

Post 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&param=udevice&'
SWITCH_CMD='json.htm?type=command&param=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&param=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&param=switchlight&idx=212&switchcmd=On"
curlres=`curl -sS $hostcmd`

fi
macieiks
Posts: 36
Joined: Tuesday 07 July 2015 12:00
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: APC UPS Monitoring

Post 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 :D ??

Thanks in advance
Maciek ;)
RPI2 + RFLink + PiFace D2 + Aeon Z-Wave Gen5 + Foscams FI9821P&R2 + MySensors + UPS APC Back-UPS 950VA
RPI2 + RFXtrx433e + Foscams FI9821P + MySensors + UPS APC Back-UPS 950VA
RPIB+ + TP-Link MR3420 + Huawei E173 GarageDomoticz :)
jannl
Posts: 625
Joined: Thursday 02 October 2014 6:36
Target OS: Raspberry Pi / ODroid
Domoticz version: 2022.2
Location: Geleen
Contact:

Re: APC UPS Monitoring

Post 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.
macieiks
Posts: 36
Joined: Tuesday 07 July 2015 12:00
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: APC UPS Monitoring

Post 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 :D 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?
RPI2 + RFLink + PiFace D2 + Aeon Z-Wave Gen5 + Foscams FI9821P&R2 + MySensors + UPS APC Back-UPS 950VA
RPI2 + RFXtrx433e + Foscams FI9821P + MySensors + UPS APC Back-UPS 950VA
RPIB+ + TP-Link MR3420 + Huawei E173 GarageDomoticz :)
jannl
Posts: 625
Joined: Thursday 02 October 2014 6:36
Target OS: Raspberry Pi / ODroid
Domoticz version: 2022.2
Location: Geleen
Contact:

Re: APC UPS Monitoring

Post by jannl »

Not sure if that can be the last action the pi does before it shutsdown itself.
SHadley1138
Posts: 47
Joined: Thursday 10 March 2016 14:03
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: APC UPS Monitoring

Post by SHadley1138 »

i think the command structure of 'apcaccess status' has changed. do you have an updated version?
tOmki
Posts: 9
Joined: Monday 02 February 2015 12:29
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.5877
Location: Poland
Contact:

Re: APC UPS Monitoring

Post by tOmki »

How to change the first script to use Electricity (instant and counter) or Counter dummy sensor ?
Raspberry Pi B+, 4x DS18B20, RFLink Gateway, UPS APC Back-UPS CS 650VA,
Mace
Posts: 65
Joined: Monday 21 August 2017 19:52
Target OS: Windows
Domoticz version: 3.8153
Location: Rhoon
Contact:

Re: APC UPS Monitoring

Post by Mace »

Script doesn't seem to work. I can monitor the values via CGI, but running the OPS script gets me no values...
underscore4
Posts: 20
Joined: Sunday 19 March 2017 22:06
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: APC UPS Monitoring

Post 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&param=udevice&'
SWITCH_CMD='json.htm?type=command&param=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&param=udevice&idx=102&nvalue=1&svalue=Online

http://yourip:port/json.htm?type=command&param=udevice&idx=102&nvalue=4&svalue=On battery
assenzuid
Posts: 135
Joined: Friday 13 November 2015 9:11
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: The Netherlands, Emmen Area
Contact:

Re: APC UPS Monitoring

Post by assenzuid »

How to remove the .000 behind the Volt sensors?
Capture.JPG
Capture.JPG (29.53 KiB) Viewed 18792 times
assenzuid
Posts: 135
Joined: Friday 13 November 2015 9:11
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: The Netherlands, Emmen Area
Contact:

Re: APC UPS Monitoring

Post 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&param=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?
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest