From c9a8fcc5a7fbbca1c0440f4b9f0649b6832ee1aa Mon Sep 17 00:00:00 2001 From: okaegi Date: Sun, 6 Aug 2023 11:26:19 +0200 Subject: [PATCH] idm .... MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Aktuelle Leistung wird nun bei jedem Durchlauf gelesen. Pv Leistung wird nun immer geschrieben (egal ob Gerät on oder off ist) Variablen zentral initialisiert --- packages/modules/smarthome/idm/watt.py | 54 ++++++++++++++------------ 1 file changed, 29 insertions(+), 25 deletions(-) diff --git a/packages/modules/smarthome/idm/watt.py b/packages/modules/smarthome/idm/watt.py index 52004b530..b63cda12c 100644 --- a/packages/modules/smarthome/idm/watt.py +++ b/packages/modules/smarthome/idm/watt.py @@ -37,12 +37,28 @@ count5 = 0 with open(file_stringcount5, 'w') as f: f.write(str(count5)) +# aktuelle Leistung lesen +client = ModbusTcpClient(ipadr, port=502) +# test +# start = 3501 +# navvers = "2" +# prod +start = 4122 +if navvers == "2": + rr = client.read_input_registers(start, 2, unit=1) +else: + rr = client.read_holding_registers(start, 2, unit=1) +raw = struct.pack('>HH', rr.getRegister(1), rr.getRegister(0)) +lkw = float(struct.unpack('>f', raw)[0]) +aktpower = int(lkw*1000) +modbuswrite = 0 +neupower = 0 +# pv modus +pvmodus = 0 +if os.path.isfile(file_stringpv): + with open(file_stringpv, 'r') as f: + pvmodus = int(f.read()) if count5 == 0: - # pv modus - pvmodus = 0 - if os.path.isfile(file_stringpv): - with open(file_stringpv, 'r') as f: - pvmodus = int(f.read()) # log counter count1 = 999 if os.path.isfile(file_stringcount): @@ -53,22 +69,7 @@ count1 = 0 with open(file_stringcount, 'w') as f: f.write(str(count1)) - # aktuelle Leistung lesen - client = ModbusTcpClient(ipadr, port=502) - # test - # start = 3501 - # navvers = "2" - # prod - start = 4122 - if navvers == "2": - rr = client.read_input_registers(start, 2, unit=1) - else: - rr = client.read_holding_registers(start, 2, unit=1) - raw = struct.pack('>HH', rr.getRegister(1), rr.getRegister(0)) - lkw = float(struct.unpack('>f', raw)[0]) - aktpower = int(lkw*1000) # logik nur schicken bei pvmodus - modbuswrite = 0 if pvmodus == 1: modbuswrite = 1 # Nur positiven Uberschuss schicken, nicht aktuelle Leistung @@ -101,10 +102,6 @@ pvwnew = builder.to_registers() # json return power = aktuelle Leistungsaufnahme in Watt, # on = 1 pvmodus, powerc = counter in kwh - answer = '{"power":' + str(aktpower) + ',"powerc":0' - answer += ',"send":' + str(modbuswrite) + ',"sendpower":' + str(neupower) - answer += ',"on":' + str(pvmodus) + '}' - writeret(answer, devicenumber) if count1 < 3: log.info(" %d ipadr %s ueberschuss %6d Akt Leistung %6d Pv %6d" % (devicenumber, ipadr, uberschuss, aktpower, pvwatt)) @@ -113,7 +110,14 @@ # modbus write if modbuswrite == 1: client.write_registers(74, regnew, unit=1) - client.write_registers(78, pvwnew, unit=1) if count1 < 3: log.info("devicenr %d ipadr %s device written by modbus " % (devicenumber, ipadr)) + client.write_registers(78, pvwnew, unit=1) +else: + if pvmodus == 99: + pvmodus = 0 +answer = '{"power":' + str(aktpower) + ',"powerc":0' +answer += ',"send":' + str(modbuswrite) + ',"sendpower":' + str(neupower) +answer += ',"on":' + str(pvmodus) + '}' +writeret(answer, devicenumber)