Python script UPS Pico to Domoticz

Python and python framework

Moderator: leecollings

Post Reply
jonathan12
Posts: 25
Joined: Sunday 31 December 2017 17:30
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Python script UPS Pico to Domoticz

Post by jonathan12 »

Hi All,

I was looking for a script to import the status of the UPS Pico into Domoticz. The scripts that I found where not doing what I wanted, so I combined 2 scripts to the script I like. I'm a newbee in scripting, so it is not perfect.

Original scripts and thanks to the creators!

# Origional script 1: https://www.domoticz.com/wiki/UPS_Pico Author : JF Hautenauven
# Origional script 2: https://www.domoticz.com/forum/viewtopic.php?t=15442 Author : Kyriakos Naziris

All other info: https://www.domoticz.com/wiki/UPS_Pico

Image

Here is the list of sensors that needs to be created in order to have all the information from the script injected inside domoticz.
#Sensor Name Sensor Type

#UPS Pico Battery Voltage Voltage
#UPS Pico RPi Voltage Voltage
#UPS Pico Battery temperature Temp
#UPS Pico Rpi fan temperature Temp
#UPS Pico firmware version Text
#UPS Pico Power Source Text
#UPS Pico Battery Level Percentage
#UPS Pico Battery Type Text
#UPS Pico BAT Runtime Text
#UPS Pico Charger State Text

Code: Select all

#!/usr/bin/python

#####################################################################################
# pico_status2domoticz.py
# author : Jhead
# updated: 13-3-2020
# Script to show status UPS PIco HV3.0 in Domoticz
# Joined the following scripts together:
# Origional script 1: https://www.domoticz.com/wiki/UPS_Pico Author : JF Hautenauven
# Origional script 2: https://www.domoticz.com/forum/viewtopic.php?t=15442 Author : Kyriakos Naziris
#####################################################################################

# You can install psutil using: sudo pip install psutil
#import psutil

import sys
import ssl
import smbus
import time
import datetime
import time
from urllib2 import urlopen

#####################################################################################
#Here is the list of sensors that needs to be created in order to have all the information from the script injected inside domoticz.
#It's up to you to use these or not but keep in mind that if you don't use them, the python script will need to be modified accordingly, else you'll start receive mails from cron to notify updates failed
#Sensor Name	Sensor Type	Sensor Subtype
#UPS Pico Blue Led	Light/Switch	Switch
#UPS Pico Red Led	Light/Switch	Switch
#UPS Pico Battery Voltage	General	Voltage
#UPS Pico RPi Voltage	General	Voltage
#UPS Pico Battery temperature	Temp	THR128/138, THC138
#UPS Pico Rpi fan temperature	Temp	THR128/138, THC138
#UPS Pico firmware version	General	Text
#UPS Pico Power Source	General	Text
#UPS Pico Battery Level	General	Percentage
#UPS Pico Battery Type General Text
#UPS Pico BAT Runtime Type General Text
#UPS Pico Charger State Type General Text

#####################################################################################


#####################################################################################
# change these values
# domoticz = URL to the Domoticz server (format http://x.x.x.x:port
# idx_ are the id's of the devices corresponding to the data in domoticz
domoticz = "http://192.168.x.x:xxxx"
idx_PoweringMode = xx
idx_BatteryVoltage = xx
idx_RpiVoltage = xx
idx_BatteryTemperature = xx
idx_RpiFanTemperature = xx
idx_FirmwareVersion = xx
idx_BatteryActiveSignal = xx
idx_BatteryLevelPercentage = xx
idx_BatteryChargerStatus = xx
idx_BatteryType = xx
idx_BATRuntime = xx
idx_ChargerState = xx
# End 

#####################################################################################
# SETTINGS
#####################################################################################

# Set your desired temperature symbol
# C = Celsius
# F = Fahrenheit
degrees = "C"

# Do you have a PIco FAN kit installed?
# True or False
fankit = False

# Do you have a to92 temp sensor installed?
# True or False
to92 = True

# Do you have extended power?
# True or False
extpwr = False

#####################################################################################
# It's not necessary to edit anything below, unless you're knowing what to do!
#####################################################################################

import os
import smbus
import time
import datetime

i2c = smbus.SMBus(1)

def fw_version():
   time.sleep(0.1)
   data = i2c.read_byte_data(0x69, 0x26)
   data = format(data,"02x")
   return data
   
def boot_version():
   time.sleep(0.1)
   data = i2c.read_byte_data(0x69, 0x25)
   data = format(data,"02x")
   return data

def pcb_version():
   time.sleep(0.1)
   data = i2c.read_byte_data(0x69, 0x24)
   data = format(data,"02x")
   return data    

def pwr_mode():
   data = i2c.read_byte_data(0x69, 0x00)
   data = data & ~(1 << 7)
   if (data == 1):
      return "RPi_POWERED"
   elif (data == 2):
      return "BAT_POWERED"
   else:
      return "ERROR"
	  
def bat_version():
   time.sleep(0.1)
   data = i2c.read_byte_data(0x6b, 0x07)
   if (data == 0x46):
      return "LiFePO4"
   elif (data == 0x51):
      return "LiFePO4"
   elif (data == 0x53):
      return "LiPO"
   elif (data == 0x50):
      return "LiPO"       
   else:
      return "ERROR"
	  
def bat_runtime():
   time.sleep(0.1)
   data = i2c.read_byte_data(0x6b, 0x01) + 1
   if (data == 0x100):
      return "TIMER_DISABLED"
   elif (data == 0xff):
      return "TIMER_DISABLED"	  
   else:
      data = str(data)+ "_MIN"
      return data	  
   
def bat_level():
   time.sleep(0.1)
   data = i2c.read_word_data(0x69, 0x08)
   data = format(data,"02x")
   return (float(data) / 100)
   
def bat_percentage():
   time.sleep(0.1)
   datavolts = bat_level()
   databattery = bat_version()
   if (databattery == "LiFePO4") or (databattery == "LiFePO4"):
		databatminus = datavolts-2.90
		datapercentage = ((databatminus/0.70))*100
   elif (databattery == "LiPO") or (databattery == "LiPO"):
		databatminus = datavolts-3.4
		datapercentage = ((databatminus/0.899))*100
   return int (datapercentage)
   
   
def charger_state():
   time.sleep(0.1)
   data = i2c.read_byte_data(0x69, 0x20)
   battpercentage = bat_percentage() 
   powermode = pwr_mode()
   databattery = bat_version()
   if (databattery == "LiFePO4") or (databattery == "LiFePO4"):
	 if (data == 0x00) and (powermode == "BAT_POWERED"):		
		return "DISCHARGING"
	 if (data == 0x01) and (powermode == "RPi_POWERED"):	
		return "CHARGING"
	 if (data == 0x00) and (powermode == "RPi_POWERED"):		
		return "CHARGED"		
   if (databattery == "LiPO") or (databattery == "LiPO"):
     if (data == 0x00) and (powermode == "BAT_POWERED"):
		return "DISCHARGING"   
     if (data == 0x00) and (powermode == "RPi_POWERED"):
		return "CHARGED" 	 
     if (data == 0x01) and (powermode == "RPi_POWERED"):
		return "CHARGING" 

def rpi_level():
   time.sleep(0.1)
   data = i2c.read_word_data(0x69, 0x0a)
   data = format(data,"02x")
   powermode = pwr_mode()
   if (powermode == "RPi_POWERED"):
		return (float(data) / 100)
   else:
		return "0.0"

def rpi_cpu_temp():
   time.sleep(0.1)
   data = os.popen('vcgencmd measure_temp').readline()
   data = (data.replace("temp=","").replace("'C\n",""))	
   if (degrees == "C"):
	return data
   elif (degrees == "F"):
	return (float(data) * 9 / 5) + 32
	
def ntc1_temp():
   time.sleep(0.1)
   data = i2c.read_byte_data(0x69, 0x1b)
   data = format(data,"02x")
   if (degrees == "C"):
	return data
   elif (degrees == "F"):
	return (float(data) * 9 / 5) + 32
	
def to92_temp():
   time.sleep(0.1)
   data = i2c.read_byte_data(0x69, 0x1C)
   data = format(data,"02x")
   if (degrees == "C"):
	return data
   elif (degrees == "F"):
	return (float(data) * 9 / 5) + 32

def epr_read():
   time.sleep(0.1)
   data = i2c.read_byte_data(0x69, 0x0c)
   data = format(data,"02x")
   return (float(data) / 100)

def ad2_read():
   time.sleep(0.1)
   data = i2c.read_byte_data(0x69, 0x07)
   data = format(data,"02x")
   return (float(data) / 100)
   
def fan_mode():
   time.sleep(0.1)
   data = i2c.read_byte_data(0x6b, 0x11)
   data = data & ~(1 << 2)
   if (data == 2):
      return "AUTOMATIC"  
   elif (data == 1):
      return "ON"
   elif (data == 0):
      return "OFF"
   else:
      return "ERROR" 	  

def fan_speed():
   time.sleep(0.1)
   data = i2c.read_byte_data(0x6b, 0x12)
   data = format(data,"02x")
   return int (float(data) * 100)

def fan_threshold():
   time.sleep(0.1)
   data = i2c.read_byte_data(0x6B, 0x14)
   data = format(data,"02x")
   if (degrees == "C"):
	return data
   elif (degrees == "F"):
	return (float(data) * 9 / 5) + 32
   
def rs232_state():
   time.sleep(0.1)  
   data = i2c.read_byte_data(0x6b, 0x02)
   if (data == 0x00):
      return "OFF"
   elif (data == 0xff):
      return "OFF"	  
   elif (data == 0x01):
      return "ON @ 4800 pbs"
   elif (data == 0x02):
      return "ON @ 9600 pbs"
   elif (data == 0x03):
      return "ON @ 19200 pbs"
   elif (data == 0x04):
      return "ON @ 34600 pbs"
   elif (data == 0x05):
      return "ON @ 57600 pbs"
   elif (data == 0x0f):
      return "ON @ 115200 pbs"	  
   else:
      return "ERROR"   
   
print " "
print "**********************************************"
print "*      	    UPS PIco HV3.0A Status           *"
print "*      	         Version 6.0                 *"
print "**********************************************"
print " "
print " ","- PIco Firmware..........:",fw_version()
print " ","- PIco Bootloader........:",boot_version()
print " ","- PIco PCB Version.......:",pcb_version()
print " ","- PIco BAT Version.......:",bat_version()
print " ","- PIco BAT Runtime.......:",bat_runtime()
print " ","- PIco rs232 State.......:",rs232_state()
print " "
print " ","- Powering Mode..........:",pwr_mode()
print " ","- Charger State..........:",charger_state()
print " ","- Battery Percentage.....:",bat_percentage(),"%"
print " ","- Battery Voltage........:",bat_level(),"V"
print " ","- RPi Voltage............:",rpi_level(),"V"
print " "

if (degrees == "C"): 
	print " ","- RPi CPU Temperature....:",rpi_cpu_temp(),"C"
	print " ","- NTC1 Temperature.......:",ntc1_temp(),"C"	
elif (degrees == "F"):
	print " ","- RPi CPU Temperature....:",rpi_cpu_temp(),"F"
	print " ","- NTC1 Temperature.......:",ntc1_temp(),"F"	
else:
	print " ","- RPi CPU Temperature....: please set temperature symbol in the script!"
	print " ","- NTC1 Temperature.......: please set temperature symbol in the script!"

if (to92 == True):
	if (degrees == "C"):
		print " ","- TO-92 Temperature......:",to92_temp(),"C"
	elif (degrees == "F"):
		print " ","- TO-92 Temperature......:",to92_temp(),"F"
	else:
		print " ","- TO-92 Temperature......: please set temperature symbol in the script!"
		
if (extpwr == True):	
	print " ","- Extended Voltage.......:",epr_read(),"V"
	print " ","- A/D2 Voltage...........:",ad2_read(),"V"

if (fankit == True):		
	print " "
if (fan_mode() == "AUTOMATIC"):
	print " ","- PIco FAN Mode..........:",fan_mode()	
	if (degrees == "C"):
		print " ","- PIco FAN Temp Threshold:",fan_threshold(),"C"
	elif (degrees == "F"):
		print " ","- PIco FAN Temp Threshold:",fan_threshold(),"F"
	else:
		print " ","- PIco FAN Temp Threshold: please set temperature symbol in the script!"	
else:
	print " ","- PIco FAN Mode..........:",fan_mode()
	if (fan_mode() == "ON"):	
		print " ","- PIco FAN Speed.........:",fan_speed(),"RPM"
	else:
		print " ","- PIco FAN Speed.........: 0 RPM"	
	
	if (degrees == "C"):
		print " ","- PIco FAN Temp Threshold:",fan_threshold(),"C"
	elif (degrees == "F"):
		print " ","- PIco FAN Temp Threshold:",fan_threshold(),"F"
	else:
		print " ","- PIco FAN Temp Threshold: please set temperature symbol in the script!"	
print " "
print "**********************************************"
print "*           Powered by PiModules             *"
print "**********************************************"
print " "

# Write to domoticz

# Battery Voltage
response= urlopen(domoticz+'/json.htm?type=command&param=udevice&idx='+str(idx_BatteryVoltage)+'&nvalue=0&svalue='+str(bat_level()))
response.read()

# Battery Level Percentage
response= urlopen(domoticz+'/json.htm?type=command&param=udevice&idx='+str(idx_BatteryLevelPercentage)+'&nvalue=0&svalue='+str(bat_percentage()))
response.read()

# Battery temperature
response= urlopen(domoticz+'/json.htm?type=command&param=udevice&idx='+str(idx_BatteryTemperature)+'&nvalue=0&svalue='+str(ntc1_temp()))
response.read()

# Firmware Version
response= urlopen(domoticz+'/json.htm?type=command&param=udevice&idx='+str(idx_FirmwareVersion)+'&nvalue=0&svalue='+str(fw_version()))
response.read()

# Rpi Voltage
response= urlopen(domoticz+'/json.htm?type=command&param=udevice&idx='+str(idx_RpiVoltage)+'&nvalue=0&svalue='+str(rpi_level()))
response.read()

# Battery Type
response= urlopen(domoticz+'/json.htm?type=command&param=udevice&idx='+str(idx_BatteryType)+'&nvalue=0&svalue='+str(bat_version()))
response.read()

# Powering Mode
response= urlopen(domoticz+'/json.htm?type=command&param=udevice&idx='+str(idx_PoweringMode)+'&nvalue=0&svalue='+str(pwr_mode()))
response.read()

# Battery Runtime
response= urlopen(domoticz+'/json.htm?type=command&param=udevice&idx='+str(idx_BATRuntime)+'&nvalue=0&svalue='+str(bat_runtime()))
response.read()

# Charger State
response= urlopen(domoticz+'/json.htm?type=command&param=udevice&idx='+str(idx_ChargerState)+'&nvalue=0&svalue='+str(charger_state()))
response.read()


Run it every 5 minutes:
crontab -e
Add this line:
*/5 * * * * /usr/bin/python /home/pi/domoticz/scripts/picostatus2domoticz.py
Attachments
picostatus2domoticz.rar
(2.75 KiB) Downloaded 41 times
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 1 guest