APC UPS Monitoring

All kinds of 'OS' scripts
Kruu
Posts: 23
Joined: Tuesday 17 January 2017 21:27
Target OS: Raspberry Pi
Domoticz version:
Contact:

Re: APC UPS Monitoring

Post by Kruu » Sunday 19 November 2017 20:51

I am getting communication lost often. Can I do something about it?

2017-11-14 17:35:16 +0100 Communications with UPS lost.

assenzuid
Posts: 165
Joined: Friday 13 November 2015 10:11
Target OS: Raspberry Pi
Domoticz version: Beta
Location: The Netherlands, Emmen Area
Contact:

Re: APC UPS Monitoring

Post by assenzuid » Tuesday 24 April 2018 19:55

Rene wrote:
Tuesday 13 January 2015 3: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()					
Is there a way to see in an log file if it shutdowns the PI is batt. status is 10 min?

Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest