Vaules from python script in OH2 import

Hi,
I have a python script for read out some parameters/values from solar inverter over serial interface like:

#!/usr/bin/python
import serial
import struct
import math
#import matplotlib.pyplot as plt
import random
import binascii
import re
import os
# configure the serial connections
port = serial.Serial(
port=’/dev/ttyRS232’,
baudrate=38400,
parity=serial.PARITY_EVEN,
stopbits=serial.STOPBITS_ONE,
bytesize=serial.EIGHTBITS,
timeout=2,
xonxoff=False,
rtscts=False,
dsrdtr=False
)
# SCADA values checksum attention!
wert1 = ‘\xAA\x00\x01\x00\x00\x00\x65\x00\x00\x00\x0A\x00\x6F\x71\x00\x01\x01\x00\xB8\x0B\x00\x00\x01\x00\xC5\x90’ # Batt_V 3000
wert2 = ‘\xAA\x00\x01\x00\x00\x00\x65\x00\x00\x00\x0A\x00\x6F\x71\x00\x01\x01\x00\xB9\x0B\x00\x00\x01\x00\xC6\x96’ # Batt_temp 3001
wert3 = ‘\xAA\x00\x01\x00\x00\x00\x65\x00\x00\x00\x0A\x00\x6F\x71\x00\x01\x01\x00\x06\x0C\x00\x00\x01\x00\x97\x97’ # Discharge of Day 3078
wert4 = ‘\xAA\x00\x01\x00\x00\x00\x65\x00\x00\x00\x0A\x00\x6F\x71\x00\x01\x01\x00\xBD\x0B\x00\x00\x01\x00\xCA\xAE’ # Charge current 3005
wert5 = ‘\xAA\x00\x01\x00\x00\x00\x59\x02\x00\x00\x0A\x00\x65\x33\x00\x01\x01\x00\x5A\x1B\x00\x00\x01\x00\x77\xAC’ # BSP 7002 SOC
wert6 = ‘\xAA\x00\x01\x00\x00\x00\x59\x02\x00\x00\x0A\x00\x65\x33\x00\x01\x01\x00\x58\x1B\x00\x00\x01\x00\x75\xA0’ # BSP 7000 Batt_V
wert7 = ‘\xAA\x00\x01\x00\x00\x00\x59\x02\x00\x00\x0A\x00\x65\x33\x00\x01\x01\x00\x62\x1B\x00\x00\x01\x00\x7F\xDC’ # Ah discharge yesterday 7010
wert8 = ‘\xAA\x00\x01\x00\x00\x00\x65\x00\x00\x00\x0A\x00\x6F\x71\x00\x01\x01\x00\x40\x0C\x00\x00\x01\x00\x4E\xC5’ # Power output 3136
wert9 = ‘\xAA\x00\x01\x00\x00\x00\x59\x02\x00\x00\x0A\x00\x65\x33\x00\x01\x01\x00\x5F\x1B\x00\x00\x01\x00\x7C\xCa’ # Ah charge today 7007
wertA = ‘\xAA\x00\x01\x00\x00\x00\x58\x02\x00\x00\x0A\x00\x65\x33\x00\x01\x01\x00\x64\x1B\x00\x00\x01\x00\x81\xE8’ # total Ah entladen 7012
wertB = ‘\xAA\x00\x01\x00\x00\x00\x65\x00\x00\x00\x0A\x00\x6F\x71\x00\x01\x01\x00\xCF\x0B\x00\x00\x01\x00\xDC\x1A’ # Output Power 3023
wertC = ‘\xAA\x00\x01\x00\x00\x00\x65\x00\x00\x00\x0A\x00\x6F\x71\x00\x01\x01\x00\xC5\x0B\x00\x00\x01\x00\xD2\xDE’ # input Power 3013
wertD = ‘\xAA\x00\x01\x00\x00\x00\x59\x02\x00\x00\x0A\x00\x65\x33\x00\x01\x01\x00\x59\x1B\x00\x00\x01\x00\x76\xA6’ # BSP 7001 Batt_A
wertE = ‘\xAA\x00\x01\x00\x00\x00\x65\x00\x00\x00\x0A\x00\x6F\x71\x00\x01\x01\x00\xD8\x0B\x00\x00\x01\x00\xE5\x50’ # State of Aux2 3032
wertF = ‘\xAA\x00\x01\x00\x00\x00\x65\x00\x00\x00\x0A\x00\x6F\x71\x00\x01\x01\x00\xD7\x0B\x00\x00\x01\x00\xE4\x4A’ # State of Aux1 3031
cmd2Amp = ‘\xAA\x00\x01\x00\x00\x00\x65\x00\x00\x00\x0E\x00\x73\x79\x00\x02\x02\x00\x72\x04\x00\x00\x05\x00\x00\x00\x00\x40\xBE\x1A’
cmdChargeOff = ‘\xAA\x00\x01\x00\x00\x00\x65\x00\x00\x00\x0B\x00\x70\x73\x00\x02\x02\x00\x65\x04\x00\x00\x05\x00\x00\x71\x05’ #Ladung 0 1125
cmdChargeOn = ‘\xAA\x00\x01\x00\x00\x00\x65\x00\x00\x00\x0B\x00\x70\x73\x00\x02\x02\x00\x65\x04\x00\x00\x05\x00\x01\x72\x06’ #Ladung 1
cmd1 = ‘\xAA\x00\x01\x00\x00\x00\x65\x00\x00\x00\x0E\x00\x73\x79\x00\x02\x02\x00\x53\x04\x00\x00\x50\x00\x00\x00\x00\x40\x9F\xE4’ # max. Inputcurrent 2A 1107

# Read Battery volatage
port.write(wert6)
WR_Ausgabe = port.read(30)
# Read SOC
port.write(wert5)
WR_Ausgabe2 = port.read(30)
# Read current
port.write(wertD)
WR_Ausgabe3 = port.read(30)
# Read today discharge
#port.write(wert7)
#WR_Ausgabe4 = port.read(30)
# Read Ah loading today
port.write(wert9)
WR_Ausgabe5 = port.read(30)
# Read out Wert_I
port.write(wert4)
WR_Ausgabe6 = port.read(30)
# Read AUX2
port.write(wertE)
WR_Ausgabe7 = port.read(30)
# Close port after last read out
port.close()
# Evauate read values
a = WR_Ausgabe[27]
b = WR_Ausgabe[26]
c = WR_Ausgabe[25]
d = WR_Ausgabe[24]

string=a+b+c+d
#round (struct.unpack(“f”, struct.pack(“l”, int(Ausgabe, 16)))[0],2)
help=struct.unpack(">f",string)
Volt= round (help[0],2)

a21 = WR_Ausgabe2[27]
b21 = WR_Ausgabe2[26]
c21 = WR_Ausgabe2[25]
d21 = WR_Ausgabe2[24]

string2=a21+b21+c21+d21
help2=struct.unpack(">f",string2)
SOC= round (help2[0],2)

a31 = WR_Ausgabe3[27]
b31 = WR_Ausgabe3[26]
c31 = WR_Ausgabe3[25]
d31 = WR_Ausgabe3[24]

string3=a31+b31+c31+d31
help3=struct.unpack(">f",string3)
Ampere= round (help3[0],2)

#a41 = WR_Ausgabe4[27]
#b41 = WR_Ausgabe4[26]
#c41 = WR_Ausgabe4[25]
#d41 = WR_Ausgabe4[24]

#string4=a41+b41+c41+d41
#help4=struct.unpack(">f",string4)
#Entladung= round (help4[0],2)
#Summe = Entladung*48/1000

a51 = WR_Ausgabe5[27]
b51 = WR_Ausgabe5[26]
c51 = WR_Ausgabe5[25]
d51 = WR_Ausgabe5[24]

string5=a51+b51+c51+d51
help5=struct.unpack(">f",string5)
Ladung= (help5[0])
Ladung1 = (Ladung*48/1000)
#print (Ladung)
#print (Ladung1)

a61 = WR_Ausgabe6[27]
b61 = WR_Ausgabe6[26]
c61 = WR_Ausgabe6[25]
d61 = WR_Ausgabe6[24]

string6=a61+b61+c61+d61
help6=struct.unpack(">f",string6)
Wert_I= round (help6[0],2)

a71 = WR_Ausgabe7[24]
wert7= ord(a71)
#print [wert7]
#a71 = ‘C0’
#b71 = ‘33’
#c71 = ‘33’
#d71 = ‘33’
#string7=a71+b71+c71+d71
#help7=struct.unpack(">f",string7)
#test=help7[0]
#print [test]

# Display result on terminal
#print Ergebnis
#print Ergebnis2

# write values to file
#f1 = open("/usr/local/etc/volt.txt",“w”)
#f1.write(str(Volt))
#f1.close()
#f2 = open("/usr/local/etc/soc.txt",“w”)
#f2.write(str(SOC))
##f2.write(Ausgabe2)
#f2.close()
#f3 = open("/usr/local/etc/Ladung.txt",“w”)
#f3.write(str(Volt))
#f3.close()
#f4 = open("/usr/local/etc/Ah.txt",“w”)
#f4.write(str(SOC))
#f4.close()

#Transfer values to FHEM
#print [Volt,SOC,Ampere,Ladung1,Summe,Wert_I,AUX2]
os.system(‘perl /opt/fhem/fhem.pl 7072 “setreading Xtender_SOC SOC ‘+str(SOC)+’”’)
os.system(‘perl /opt/fhem/fhem.pl 7072 “setreading Xtender_Batt_V V ‘+str(Volt)+’”’)
os.system(‘perl /opt/fhem/fhem.pl 7072 “setreading Xtender_Batt_A A ‘+str(Ampere)+’”’)
#os.system(‘perl /opt/fhem/fhem.pl 7072 “setreading Xtender_Entladung_Vortag kWh ‘+str(Summe)+’”’)
os.system(‘perl /opt/fhem/fhem.pl 7072 “setreading Xtender_Ladung kWh ‘+str(Ladung1)+’”’)
os.system(‘perl /opt/fhem/fhem.pl 7072 “setreading Wert_I A ‘+str(Wert_I)+’”’)
os.system(‘perl /opt/fhem/fhem.pl 7072 “setreading AUX2 aux2 ‘+str(wert7)+’”’)

This script is for FHEM. Variables are put in virtual FHEM items.

How can I read out this values to Openhab2 items.
Thanks for help.

The three most common approaches are:

  1. Call the script from OH and parse out the data you need in Rules to populate the Items.

  2. Modify your Python script to publish the data using OH’s REST API (install the interactive REST API Docs from the Misc tab in PaperUI).

  3. Modify your Python script to publish the data using MQTT and configure Items in OH to subscribe to the published MQTT data.

Hi Rich,
can you send me some example fro approach Nr. 1 and 2 ?
In rules i thing to use exec binding ?
Big thanks.

  1. https://community.openhab.org/search?q=executeCommandLine%20python

  2. https://github.com/rkoshak/sensorReporter, MQTT Binding (v1.11) Getting Started 101

OK, thanks.
This script should read out some parameters.
I have also problem with set some parameters to inverter.
#!/usr/bin/python

import serial
port = serial.Serial(
port=’/dev/ttyRS232’,
baudrate=38400,
parity=serial.PARITY_EVEN,
stopbits=serial.STOPBITS_ONE,
bytesize=serial.EIGHTBITS,
timeout=2,
xonxoff=False,
rtscts=False,
dsrdtr=False
)

cmd2Amp = ‘\xAA\x00\x01\x00\x00\x00\x65\x00\x00\x00\x0E\x00\x73\x79\x00\x02\x02\x00\x72\x04\x00\x00\x05\x00\x00\x00\x00\x40\xBE\x1A’
cmd4Amp = ‘\xAA\x00\x01\x00\x00\x00\x65\x00\x00\x00\x0E\x00\x73\x79\x00\x02\x02\x00\x72\x04\x00\x00\x05\x00\x00\x00\x80\x40\x3E\x1A’

wert4 = ‘\xAA\x00\x01\x00\x00\x00\x65\x00\x00\x00\x0A\x00\x6F\x71\x00\x01\x01\x00\xBD\x0B\x00\x00\x01\x00\xCA\xAE’ # Charge current 3005

# commandos senden
port.write(cmd2Amp)
port.close()

This isn’t a python forum and I’m no python expert, let alone an expert on your solar inverter. I won’t be able to help with any of that.

1 Like

Thanks for help Rich.
It is a good inspiration to solve my work.