Added feature / fixed bug that lead to zero readings when the read head has been taken away from the meter

This commit is contained in:
2025-07-07 13:37:49 +02:00
parent 231d90ece5
commit f23cb435d2
2 changed files with 9 additions and 1 deletions

1
.env
View File

@@ -11,5 +11,6 @@ MQUSER=<USER OF THE MQTT BROKER>
MQPWD=<PASSWORD OF THE MQTT BROKER>
MQTOPIC_CUR=<TOPIC WHERE THE CURRENT STATUS SHALL BE WRITTEN TO>
MQTOPIC_SUM=<TOPIC WHERE THE TOTAL STATUS SHALL BE WRITTEN TO>
BLOCK_ZERO_READS=<BLOCK WRITING DATA TO MQTT WHEN THE FEEDIN or CONSUMPTION COUNTERS ARE READ AS 0 (HAPPENS WHEN THE READ HEAD IS TAKEN FROM THE SMARTMETER)>
PYTHONUNBUFFERED=1

View File

@@ -1,6 +1,7 @@
#!/usr/local/bin/python3
import sys
import serial
from time import sleep
class bcolors:
HEADER = '\033[95m'
@@ -68,6 +69,11 @@ class LGE320:
"T2": read_buffer[read_buffer.find(b'\x07\x01\x00\x02\x08\x00\xFF'):read_buffer.find(b'\x01\x77\x07\x01\x00\x10\x07\x00')], # 2.8.0
"P": read_buffer[read_buffer.find(b'\x07\x01\x00\x10\x07\x00\xFF'):read_buffer.find(b'\x01\x01\x01\x63')], # current power
}
if block_zero_reads and (float(energy_values["T1"]) == 0.0 or float(energy_values["T2"]) == 0.0): # zero reads may happen when the read head is taken from the meter
sleep(1)
return self.read()
return self._get_energy_value(energy_values)
@@ -105,7 +111,8 @@ if __name__ == "__main__": # if run from cli (not imported as a module)
port = "/dev/ttyUSB0" # default to /dev/ttyUSB0 if READER_PORT env var is not set, and print a warning
print(bcolors.WARNING + f"No serial port specified, trying {port}" + bcolors.ENDC)
lge320 = LGE320(port)
block_zero_reads = os.getenv('BLOCK_ZERO_READS', False)
## define the clients
def on_connect(client, userdata, flags, reason_code, properties):