From 36dd7489e5c506673af7adc4dfaa2f638a69ad64 Mon Sep 17 00:00:00 2001 From: ndrsnhs Date: Tue, 18 Mar 2025 11:08:41 +0100 Subject: [PATCH 1/7] change factors an register type --- packages/modules/devices/sofar/sofar/bat.py | 10 ++-- .../modules/devices/sofar/sofar/counter.py | 46 ++++++++----------- .../modules/devices/sofar/sofar/inverter.py | 8 ++-- 3 files changed, 27 insertions(+), 37 deletions(-) diff --git a/packages/modules/devices/sofar/sofar/bat.py b/packages/modules/devices/sofar/sofar/bat.py index 000b64378e..acd293f21b 100644 --- a/packages/modules/devices/sofar/sofar/bat.py +++ b/packages/modules/devices/sofar/sofar/bat.py @@ -25,23 +25,23 @@ def update(self, client: ModbusTcpClient_) -> None: # 0x060D Power_bat2, 0x0614 Power_bat3, 0x061B Power_bat4, 0x0622 Power_bat5, # 0x0629 Power_bat6, 0x0630 Power_bat7, 0x0637 Power_bat8 # 0x0646 Power_bat9, 0x064D Power_bat10, 0x0654 Power_bat11, 0x065B Power_bat12 - power = sum(client.read_input_registers(reg, ModbusDataType.INT_16, unit=self.__modbus_id) + power = sum(client.read_holding_registers(reg, ModbusDataType.INT_16, unit=self.__modbus_id) for reg in [0x0606, 0x060D, 0x0614, 0x061B, 0x0622, 0x0629, 0x0630, - 0x0637, 0x0646, 0x064D, 0x0654, 0x065B]) + 0x0637, 0x0646, 0x064D, 0x0654, 0x065B]) * -10 # 0x0608 SOC_Bat1 UInt16 in % accuracy 1 # 0x060F SOC_bat2, 0x0616 SOC_bat3, 0x061D SOC_bat4, 0x0624 SOC_bat5, # 0x062B SOC_bat6, 0x0632 SOC_bat7, 0x0639 SOC_bat_8 # 0x0648 SOC_bat9, 0x064F SOC_bat10, 0x0656 SOC_bat11, 0x065D SOC_bat12 - soc = sum(client.read_input_registers(0x0608, ModbusDataType.UINT_16, unit=self.__modbus_id) + soc = sum(client.read_holding_registers(0x0608, ModbusDataType.UINT_16, unit=self.__modbus_id) for reg in [0x0608, 0x060F, 0x0616, 0x061D, 0x0624, 0x062B, 0x0632, 0x0639, 0x0648, 0x064F, 0x0656, 0x065D]) # 0x0696 Bat_charge_total LSB UInt32 0,1 kWh # 0x0697 Bat_charge_total UInt32 0,1 kWh - imported = client.read_input_registers( + imported = client.read_holding_registers( 0x0696, ModbusDataType.UINT_32, unit=self.__modbus_id) * 100 # 0x069A Bat_discharge_total LSB UInt32 0,1 kWh # 0x069B Bat:discharge_total UInt32 0,1 kWh - exported = client.read_input_registers( + exported = client.read_holding_registers( 0x069A, ModbusDataType.UINT_32, unit=self.__modbus_id) * 100 bat_state = BatState( diff --git a/packages/modules/devices/sofar/sofar/counter.py b/packages/modules/devices/sofar/sofar/counter.py index 91bc7e6b19..954af12f26 100644 --- a/packages/modules/devices/sofar/sofar/counter.py +++ b/packages/modules/devices/sofar/sofar/counter.py @@ -24,49 +24,39 @@ def __init__(self, def update(self, client: ModbusTcpClient_): # 0x0485 ActivePower_output_total Int16 in kW accuracy 0,01 discharge + charge - # 0x0488 ActivePower_PCC_total Int16 0,01 kW - power = client.read_input_registers(0x0488, ModbusDataType.INT_16, wordorder=Endian.Little, - unit=self.__modbus_id) * -1 + power = client.read_holding_registers(0x0488, ModbusDataType.INT_16, wordorder=Endian.Little, + unit=self.__modbus_id) * -10 # 0x0484 Frequency_Grid UInt16 in Hz accuracy 0,01 - frequency = client.read_input_registers( + frequency = client.read_holding_registers( 0x0484, ModbusDataType.UINT_16, unit=self.__modbus_id) / 100 try: - # 0x048F ActivePower_Output_R UInt16 in V accuracy 0,1 - # 0x0493 ActivePower_PCC_R Int16 in kW accuracy 0,01 - powers = [-value for value in client.read_input_registers( - 0x0493, [ModbusDataType.INT_16] * 1, wordorder=Endian.Little, unit=self.__modbus_id - )] + powers = [ + client.read_holding_registers(0x0493, ModbusDataType.INT_16, unit=self.__modbus_id) * -10, + client.read_holding_registers(0x049E, ModbusDataType.INT_16, unit=self.__modbus_id) * -10, + client.read_holding_registers(0x04A9, ModbusDataType.INT_16, unit=self.__modbus_id) * -10] except Exception: powers = None try: - voltages = [client.read_input_registers( - # 048D Voltage_Phase_R UInt16 in V accuracy 0,1 - 0x048D, ModbusDataType.UINT_16, unit=self.__modbus_id - ) / 10, client.read_input_registers( - # 0498 Voltage_Phase_S UInt16 in V accuracy 0,1 - 0x0498, ModbusDataType.UINT_16, unit=self.__modbus_id - ) / 10, client.read_input_registers( - # 04A3 Voltage_Phase_T UInt16 in V accuracy 0,1 - 0x04A3, ModbusDataType.UINT_16, unit=self.__modbus_id - ) / 10] - if voltages[0] < 1: - voltages[0] = 230 - if voltages[1] < 1: - voltages[1] = 230 - if voltages[2] < 1: - voltages[2] = 230 + voltages = [ + client.read_holding_registers(0x048D, ModbusDataType.UINT_16, unit=self.__modbus_id) * 0.1, + client.read_holding_registers(0x0498, ModbusDataType.UINT_16, unit=self.__modbus_id) * 0.1, + client.read_holding_registers(0x04A3, ModbusDataType.UINT_16, unit=self.__modbus_id) * 0.1] + for voltage in voltages: + if voltage < 1: + voltage = 230 except Exception: voltages = [230, 230, 230] exported = [value * 10 - for value in client.read_input_registers( + for value in client.read_holding_registers( # 0x0692 Energy_Selling_Total UInt32 in kwH accuracy 0,01 LSB # 0x0693 Energy_Selling_Total UInt32 in kwH accuracy 0,01 - 0x0692, [ModbusDataType.UINT_32] * 10, + 0x0692, [ModbusDataType.UINT_32] * 0.1, wordorder=Endian.Little, unit=self.__modbus_id)] imported = [value * 10 - for value in client.read_input_registers( + for value in client.read_holding_registers( # 0x068E Energy_Purchase_Total UInt32 in kwH accuracy 0,01 LSB # 0x068F Energy_Purchase_Total UInt32 in kwH accuracy 0,01 - 0x068E, [ModbusDataType.UINT_32] * 10, + 0x068E, [ModbusDataType.UINT_32] * 0.1, wordorder=Endian.Little, unit=self.__modbus_id)] counter_state = CounterState( diff --git a/packages/modules/devices/sofar/sofar/inverter.py b/packages/modules/devices/sofar/sofar/inverter.py index d73cdfb9f7..9b3e7220d0 100644 --- a/packages/modules/devices/sofar/sofar/inverter.py +++ b/packages/modules/devices/sofar/sofar/inverter.py @@ -27,15 +27,15 @@ def update(self, client: ModbusTcpClient_) -> None: # 0x0595 Power_PV6, 0x0598 Power_PV7, 0x059B Power_PV8, 0x059E Power_PV9, 0x05A1 Power_PV10, # 0x05A4 Power_PV11, 0x05A7 Power_PV12, 0x05AA Power_PV13, # 0x05AD Power_PV14, 0x05B0 Power_PV15, 0x05B3 Power_PV16 - power = sum([client.read_input_registers(reg, ModbusDataType.UINT_16, + power = sum([client.read_holding_registers(reg, ModbusDataType.UINT_16, unit=self.__modbus_id) for reg in [0x0586, 0x0589, 0x058C, 0x058F, 0x0592, 0x0595, 0x0598, 0x059B, 0x059E, 0x05A1, 0x05A4, - 0x05A7, 0x05AA, 0x05AD, 0x05B0, 0x05B3]]) * -1 + 0x05A7, 0x05AA, 0x05AD, 0x05B0, 0x05B3]]) * -10 # 0x05C4 Power_PV_Total UInt16 in kW accuracy 0,1 # 0x0686 PV_Generation_Total UInt32 0,1 kW LSB # 0x0687 PV_Generation_Total UInt32 0,1 kW - exported = client.read_input_registers(0x0686, ModbusDataType.UINT_32, wordorder=Endian.Little, - unit=self.__modbus_id) * 100 + exported = client.read_holding_registers(0x0686, ModbusDataType.UINT_32, wordorder=Endian.Little, + unit=self.__modbus_id) * 100 inverter_state = InverterState( power=power, From 5bc465ec17c82c18a9f7bb6f39a396a82ce80d4d Mon Sep 17 00:00:00 2001 From: ndrsnhs Date: Wed, 19 Mar 2025 07:36:44 +0100 Subject: [PATCH 2/7] change factors --- .../modules/devices/sofar/sofar/counter.py | 20 ++++++++----------- .../modules/devices/sofar/sofar/inverter.py | 2 +- 2 files changed, 9 insertions(+), 13 deletions(-) diff --git a/packages/modules/devices/sofar/sofar/counter.py b/packages/modules/devices/sofar/sofar/counter.py index 954af12f26..f7a713e4d8 100644 --- a/packages/modules/devices/sofar/sofar/counter.py +++ b/packages/modules/devices/sofar/sofar/counter.py @@ -46,18 +46,14 @@ def update(self, client: ModbusTcpClient_): voltage = 230 except Exception: voltages = [230, 230, 230] - exported = [value * 10 - for value in client.read_holding_registers( - # 0x0692 Energy_Selling_Total UInt32 in kwH accuracy 0,01 LSB - # 0x0693 Energy_Selling_Total UInt32 in kwH accuracy 0,01 - 0x0692, [ModbusDataType.UINT_32] * 0.1, - wordorder=Endian.Little, unit=self.__modbus_id)] - imported = [value * 10 - for value in client.read_holding_registers( - # 0x068E Energy_Purchase_Total UInt32 in kwH accuracy 0,01 LSB - # 0x068F Energy_Purchase_Total UInt32 in kwH accuracy 0,01 - 0x068E, [ModbusDataType.UINT_32] * 0.1, - wordorder=Endian.Little, unit=self.__modbus_id)] + # 0x0692 Energy_Selling_Total UInt32 in kwH accuracy 0,01 LSB + # 0x0693 Energy_Selling_Total UInt32 in kwH accuracy 0,01 + exported = client.read_holding_registers(0x0692, ModbusDataType.UINT_32, + wordorder=Endian.Little, unit=self.__modbus_id) * 0.01 + # 0x068E Energy_Purchase_Total UInt32 in kwH accuracy 0,01 LSB + # 0x068F Energy_Purchase_Total UInt32 in kwH accuracy 0,01 + imported = client.read_holding_registers(0x068E, ModbusDataType.UINT_32, + wordorder=Endian.Little, unit=self.__modbus_id) * 0.01 counter_state = CounterState( imported=imported, diff --git a/packages/modules/devices/sofar/sofar/inverter.py b/packages/modules/devices/sofar/sofar/inverter.py index 9b3e7220d0..c508b2a899 100644 --- a/packages/modules/devices/sofar/sofar/inverter.py +++ b/packages/modules/devices/sofar/sofar/inverter.py @@ -35,7 +35,7 @@ def update(self, client: ModbusTcpClient_) -> None: # 0x0686 PV_Generation_Total UInt32 0,1 kW LSB # 0x0687 PV_Generation_Total UInt32 0,1 kW exported = client.read_holding_registers(0x0686, ModbusDataType.UINT_32, wordorder=Endian.Little, - unit=self.__modbus_id) * 100 + unit=self.__modbus_id) * 0.01 inverter_state = InverterState( power=power, From f3b7dd60bc05aba31b517834de8896401ff34faa Mon Sep 17 00:00:00 2001 From: ndrsnhs Date: Wed, 19 Mar 2025 08:43:56 +0100 Subject: [PATCH 3/7] adjust factors for import and export values --- packages/modules/devices/sofar/sofar/bat.py | 4 ++-- packages/modules/devices/sofar/sofar/counter.py | 4 ++-- packages/modules/devices/sofar/sofar/inverter.py | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/packages/modules/devices/sofar/sofar/bat.py b/packages/modules/devices/sofar/sofar/bat.py index acd293f21b..8562a99585 100644 --- a/packages/modules/devices/sofar/sofar/bat.py +++ b/packages/modules/devices/sofar/sofar/bat.py @@ -38,11 +38,11 @@ def update(self, client: ModbusTcpClient_) -> None: # 0x0696 Bat_charge_total LSB UInt32 0,1 kWh # 0x0697 Bat_charge_total UInt32 0,1 kWh imported = client.read_holding_registers( - 0x0696, ModbusDataType.UINT_32, unit=self.__modbus_id) * 100 + 0x0696, ModbusDataType.UINT_32, unit=self.__modbus_id) * 0.001 # 0x069A Bat_discharge_total LSB UInt32 0,1 kWh # 0x069B Bat:discharge_total UInt32 0,1 kWh exported = client.read_holding_registers( - 0x069A, ModbusDataType.UINT_32, unit=self.__modbus_id) * 100 + 0x069A, ModbusDataType.UINT_32, unit=self.__modbus_id) * 0.001 bat_state = BatState( power=power, diff --git a/packages/modules/devices/sofar/sofar/counter.py b/packages/modules/devices/sofar/sofar/counter.py index f7a713e4d8..83b675c8c6 100644 --- a/packages/modules/devices/sofar/sofar/counter.py +++ b/packages/modules/devices/sofar/sofar/counter.py @@ -49,11 +49,11 @@ def update(self, client: ModbusTcpClient_): # 0x0692 Energy_Selling_Total UInt32 in kwH accuracy 0,01 LSB # 0x0693 Energy_Selling_Total UInt32 in kwH accuracy 0,01 exported = client.read_holding_registers(0x0692, ModbusDataType.UINT_32, - wordorder=Endian.Little, unit=self.__modbus_id) * 0.01 + wordorder=Endian.Little, unit=self.__modbus_id) * 0.001 # 0x068E Energy_Purchase_Total UInt32 in kwH accuracy 0,01 LSB # 0x068F Energy_Purchase_Total UInt32 in kwH accuracy 0,01 imported = client.read_holding_registers(0x068E, ModbusDataType.UINT_32, - wordorder=Endian.Little, unit=self.__modbus_id) * 0.01 + wordorder=Endian.Little, unit=self.__modbus_id) * 0.001 counter_state = CounterState( imported=imported, diff --git a/packages/modules/devices/sofar/sofar/inverter.py b/packages/modules/devices/sofar/sofar/inverter.py index c508b2a899..2fb7c99f7f 100644 --- a/packages/modules/devices/sofar/sofar/inverter.py +++ b/packages/modules/devices/sofar/sofar/inverter.py @@ -35,7 +35,7 @@ def update(self, client: ModbusTcpClient_) -> None: # 0x0686 PV_Generation_Total UInt32 0,1 kW LSB # 0x0687 PV_Generation_Total UInt32 0,1 kW exported = client.read_holding_registers(0x0686, ModbusDataType.UINT_32, wordorder=Endian.Little, - unit=self.__modbus_id) * 0.01 + unit=self.__modbus_id) * 0.001 inverter_state = InverterState( power=power, From 7d6d9f13f42f7f10f8e1fa5053ae5088757898a5 Mon Sep 17 00:00:00 2001 From: ndrsnhs Date: Tue, 1 Apr 2025 08:32:53 +0200 Subject: [PATCH 4/7] fix factors and endianness --- packages/modules/devices/sofar/sofar/bat.py | 10 ++++------ packages/modules/devices/sofar/sofar/counter.py | 13 +++---------- packages/modules/devices/sofar/sofar/inverter.py | 4 +--- 3 files changed, 8 insertions(+), 19 deletions(-) diff --git a/packages/modules/devices/sofar/sofar/bat.py b/packages/modules/devices/sofar/sofar/bat.py index 8562a99585..2230ad5e93 100644 --- a/packages/modules/devices/sofar/sofar/bat.py +++ b/packages/modules/devices/sofar/sofar/bat.py @@ -27,22 +27,20 @@ def update(self, client: ModbusTcpClient_) -> None: # 0x0646 Power_bat9, 0x064D Power_bat10, 0x0654 Power_bat11, 0x065B Power_bat12 power = sum(client.read_holding_registers(reg, ModbusDataType.INT_16, unit=self.__modbus_id) for reg in [0x0606, 0x060D, 0x0614, 0x061B, 0x0622, 0x0629, 0x0630, - 0x0637, 0x0646, 0x064D, 0x0654, 0x065B]) * -10 + 0x0637, 0x0646, 0x064D, 0x0654, 0x065B]) * 10 # 0x0608 SOC_Bat1 UInt16 in % accuracy 1 # 0x060F SOC_bat2, 0x0616 SOC_bat3, 0x061D SOC_bat4, 0x0624 SOC_bat5, # 0x062B SOC_bat6, 0x0632 SOC_bat7, 0x0639 SOC_bat_8 # 0x0648 SOC_bat9, 0x064F SOC_bat10, 0x0656 SOC_bat11, 0x065D SOC_bat12 - soc = sum(client.read_holding_registers(0x0608, ModbusDataType.UINT_16, unit=self.__modbus_id) - for reg in [0x0608, 0x060F, 0x0616, 0x061D, 0x0624, 0x062B, 0x0632, - 0x0639, 0x0648, 0x064F, 0x0656, 0x065D]) + soc = client.read_holding_registers(0x0608, ModbusDataType.UINT_16, unit=self.__modbus_id) # 0x0696 Bat_charge_total LSB UInt32 0,1 kWh # 0x0697 Bat_charge_total UInt32 0,1 kWh imported = client.read_holding_registers( - 0x0696, ModbusDataType.UINT_32, unit=self.__modbus_id) * 0.001 + 0x0696, ModbusDataType.UINT_32, unit=self.__modbus_id) * 100 # 0x069A Bat_discharge_total LSB UInt32 0,1 kWh # 0x069B Bat:discharge_total UInt32 0,1 kWh exported = client.read_holding_registers( - 0x069A, ModbusDataType.UINT_32, unit=self.__modbus_id) * 0.001 + 0x069A, ModbusDataType.UINT_32, unit=self.__modbus_id) * 100 bat_state = BatState( power=power, diff --git a/packages/modules/devices/sofar/sofar/counter.py b/packages/modules/devices/sofar/sofar/counter.py index 83b675c8c6..f45d179f0a 100644 --- a/packages/modules/devices/sofar/sofar/counter.py +++ b/packages/modules/devices/sofar/sofar/counter.py @@ -1,6 +1,5 @@ #!/usr/bin/env python3 from typing import Dict, Union -from pymodbus.constants import Endian from dataclass_utils import dataclass_from_dict from modules.common.abstract_device import AbstractCounter @@ -24,8 +23,7 @@ def __init__(self, def update(self, client: ModbusTcpClient_): # 0x0485 ActivePower_output_total Int16 in kW accuracy 0,01 discharge + charge - # 0x0488 ActivePower_PCC_total Int16 0,01 kW - power = client.read_holding_registers(0x0488, ModbusDataType.INT_16, wordorder=Endian.Little, - unit=self.__modbus_id) * -10 + power = client.read_holding_registers(0x0488, ModbusDataType.INT_16, unit=self.__modbus_id) * -10 # 0x0484 Frequency_Grid UInt16 in Hz accuracy 0,01 frequency = client.read_holding_registers( 0x0484, ModbusDataType.UINT_16, unit=self.__modbus_id) / 100 @@ -41,19 +39,14 @@ def update(self, client: ModbusTcpClient_): client.read_holding_registers(0x048D, ModbusDataType.UINT_16, unit=self.__modbus_id) * 0.1, client.read_holding_registers(0x0498, ModbusDataType.UINT_16, unit=self.__modbus_id) * 0.1, client.read_holding_registers(0x04A3, ModbusDataType.UINT_16, unit=self.__modbus_id) * 0.1] - for voltage in voltages: - if voltage < 1: - voltage = 230 except Exception: voltages = [230, 230, 230] # 0x0692 Energy_Selling_Total UInt32 in kwH accuracy 0,01 LSB # 0x0693 Energy_Selling_Total UInt32 in kwH accuracy 0,01 - exported = client.read_holding_registers(0x0692, ModbusDataType.UINT_32, - wordorder=Endian.Little, unit=self.__modbus_id) * 0.001 + exported = client.read_holding_registers(0x0692, ModbusDataType.UINT_32, unit=self.__modbus_id) * 100 # 0x068E Energy_Purchase_Total UInt32 in kwH accuracy 0,01 LSB # 0x068F Energy_Purchase_Total UInt32 in kwH accuracy 0,01 - imported = client.read_holding_registers(0x068E, ModbusDataType.UINT_32, - wordorder=Endian.Little, unit=self.__modbus_id) * 0.001 + imported = client.read_holding_registers(0x068E, ModbusDataType.UINT_32, unit=self.__modbus_id) * 100 counter_state = CounterState( imported=imported, diff --git a/packages/modules/devices/sofar/sofar/inverter.py b/packages/modules/devices/sofar/sofar/inverter.py index 2fb7c99f7f..b4e1dd24b4 100644 --- a/packages/modules/devices/sofar/sofar/inverter.py +++ b/packages/modules/devices/sofar/sofar/inverter.py @@ -1,6 +1,5 @@ #!/usr/bin/env python3 from typing import Dict, Union -from pymodbus.constants import Endian from dataclass_utils import dataclass_from_dict from modules.common.abstract_device import AbstractInverter @@ -34,8 +33,7 @@ def update(self, client: ModbusTcpClient_) -> None: # 0x05C4 Power_PV_Total UInt16 in kW accuracy 0,1 # 0x0686 PV_Generation_Total UInt32 0,1 kW LSB # 0x0687 PV_Generation_Total UInt32 0,1 kW - exported = client.read_holding_registers(0x0686, ModbusDataType.UINT_32, wordorder=Endian.Little, - unit=self.__modbus_id) * 0.001 + exported = client.read_holding_registers(0x0686, ModbusDataType.UINT_32, unit=self.__modbus_id) * 100 inverter_state = InverterState( power=power, From a9ac14f114056281a6cbd5c001d27846da41c318 Mon Sep 17 00:00:00 2001 From: ndrsnhs Date: Tue, 1 Apr 2025 11:45:12 +0200 Subject: [PATCH 5/7] Improve performance by reducing registers --- packages/modules/devices/sofar/sofar/bat.py | 21 ++++++++----------- .../modules/devices/sofar/sofar/counter.py | 10 +-------- .../modules/devices/sofar/sofar/inverter.py | 14 ++----------- 3 files changed, 12 insertions(+), 33 deletions(-) diff --git a/packages/modules/devices/sofar/sofar/bat.py b/packages/modules/devices/sofar/sofar/bat.py index 2230ad5e93..648ce50357 100644 --- a/packages/modules/devices/sofar/sofar/bat.py +++ b/packages/modules/devices/sofar/sofar/bat.py @@ -21,18 +21,15 @@ def __init__(self, self.fault_state = FaultState(ComponentInfo.from_component_config(self.component_config)) def update(self, client: ModbusTcpClient_) -> None: - # 0x0606 Power_bat1 Int16 in kW accuracy 0,01 - # 0x060D Power_bat2, 0x0614 Power_bat3, 0x061B Power_bat4, 0x0622 Power_bat5, - # 0x0629 Power_bat6, 0x0630 Power_bat7, 0x0637 Power_bat8 - # 0x0646 Power_bat9, 0x064D Power_bat10, 0x0654 Power_bat11, 0x065B Power_bat12 - power = sum(client.read_holding_registers(reg, ModbusDataType.INT_16, unit=self.__modbus_id) - for reg in [0x0606, 0x060D, 0x0614, 0x061B, 0x0622, 0x0629, 0x0630, - 0x0637, 0x0646, 0x064D, 0x0654, 0x065B]) * 10 - # 0x0608 SOC_Bat1 UInt16 in % accuracy 1 - # 0x060F SOC_bat2, 0x0616 SOC_bat3, 0x061D SOC_bat4, 0x0624 SOC_bat5, - # 0x062B SOC_bat6, 0x0632 SOC_bat7, 0x0639 SOC_bat_8 - # 0x0648 SOC_bat9, 0x064F SOC_bat10, 0x0656 SOC_bat11, 0x065D SOC_bat12 - soc = client.read_holding_registers(0x0608, ModbusDataType.UINT_16, unit=self.__modbus_id) + # 0x900D High 8 bits: the number of battery packs in parallel + # Lower 8 bits: the number of battery strings in the battery pack + battery_packs = client.read_holding_registers(0x900D, ModbusDataType.UINT_16, unit=self.__modbus_id) >> 8 + # Power bat1 - bat12: INT_16 in kW accuracy 0,01 + power_regs = [0x0606, 0x060D, 0x0614, 0x061B, 0x0622, 0x0629, 0x0630, 0x0637, 0x0646, 0x064D, 0x0654, 0x065B] + + power = sum(client.read_holding_registers(power_regs[idx], ModbusDataType.INT_16, unit=self.__modbus_id) + for idx in range(battery_packs)) * 10 + soc = client.read_holding_registers(0x9012, ModbusDataType.UINT_16, unit=self.__modbus_id) # 0x0696 Bat_charge_total LSB UInt32 0,1 kWh # 0x0697 Bat_charge_total UInt32 0,1 kWh imported = client.read_holding_registers( diff --git a/packages/modules/devices/sofar/sofar/counter.py b/packages/modules/devices/sofar/sofar/counter.py index f45d179f0a..b61475a1dd 100644 --- a/packages/modules/devices/sofar/sofar/counter.py +++ b/packages/modules/devices/sofar/sofar/counter.py @@ -34,13 +34,6 @@ def update(self, client: ModbusTcpClient_): client.read_holding_registers(0x04A9, ModbusDataType.INT_16, unit=self.__modbus_id) * -10] except Exception: powers = None - try: - voltages = [ - client.read_holding_registers(0x048D, ModbusDataType.UINT_16, unit=self.__modbus_id) * 0.1, - client.read_holding_registers(0x0498, ModbusDataType.UINT_16, unit=self.__modbus_id) * 0.1, - client.read_holding_registers(0x04A3, ModbusDataType.UINT_16, unit=self.__modbus_id) * 0.1] - except Exception: - voltages = [230, 230, 230] # 0x0692 Energy_Selling_Total UInt32 in kwH accuracy 0,01 LSB # 0x0693 Energy_Selling_Total UInt32 in kwH accuracy 0,01 exported = client.read_holding_registers(0x0692, ModbusDataType.UINT_32, unit=self.__modbus_id) * 100 @@ -53,8 +46,7 @@ def update(self, client: ModbusTcpClient_): exported=exported, power=power, powers=powers, - frequency=frequency, - voltages=voltages, + frequency=frequency ) self.store.set(counter_state) diff --git a/packages/modules/devices/sofar/sofar/inverter.py b/packages/modules/devices/sofar/sofar/inverter.py index b4e1dd24b4..97f715bf26 100644 --- a/packages/modules/devices/sofar/sofar/inverter.py +++ b/packages/modules/devices/sofar/sofar/inverter.py @@ -21,18 +21,8 @@ def __init__(self, self.fault_state = FaultState(ComponentInfo.from_component_config(self.component_config)) def update(self, client: ModbusTcpClient_) -> None: - # 0x0586 Power_PV1 UInt16 in kW accuracy 0,01 - # 0x0589 Power_PV2, 0x058C Power_PV3, 0x058F Power_PV4, 0x0592 Power_PV5, - # 0x0595 Power_PV6, 0x0598 Power_PV7, 0x059B Power_PV8, 0x059E Power_PV9, 0x05A1 Power_PV10, - # 0x05A4 Power_PV11, 0x05A7 Power_PV12, 0x05AA Power_PV13, - # 0x05AD Power_PV14, 0x05B0 Power_PV15, 0x05B3 Power_PV16 - power = sum([client.read_holding_registers(reg, ModbusDataType.UINT_16, - unit=self.__modbus_id) for reg in [0x0586, 0x0589, 0x058C, 0x058F, 0x0592, - 0x0595, 0x0598, 0x059B, 0x059E, 0x05A1, 0x05A4, - 0x05A7, 0x05AA, 0x05AD, 0x05B0, 0x05B3]]) * -10 - # 0x05C4 Power_PV_Total UInt16 in kW accuracy 0,1 - # 0x0686 PV_Generation_Total UInt32 0,1 kW LSB - # 0x0687 PV_Generation_Total UInt32 0,1 kW + # 0x05C4 Power_PV_Total UINT16 in kW accuracy 0,1 + power = client.read_holding_registers(0x05C4, ModbusDataType.UINT_16, unit=self.__modbus_id) / -10 exported = client.read_holding_registers(0x0686, ModbusDataType.UINT_32, unit=self.__modbus_id) * 100 inverter_state = InverterState( From 4ef06aecd776c0355579643b2e3fc08c2f4e4e12 Mon Sep 17 00:00:00 2001 From: ndrsnhs Date: Tue, 1 Apr 2025 11:46:34 +0200 Subject: [PATCH 6/7] remove whitespace --- packages/modules/devices/sofar/sofar/bat.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/modules/devices/sofar/sofar/bat.py b/packages/modules/devices/sofar/sofar/bat.py index 648ce50357..f42543a991 100644 --- a/packages/modules/devices/sofar/sofar/bat.py +++ b/packages/modules/devices/sofar/sofar/bat.py @@ -24,7 +24,7 @@ def update(self, client: ModbusTcpClient_) -> None: # 0x900D High 8 bits: the number of battery packs in parallel # Lower 8 bits: the number of battery strings in the battery pack battery_packs = client.read_holding_registers(0x900D, ModbusDataType.UINT_16, unit=self.__modbus_id) >> 8 - # Power bat1 - bat12: INT_16 in kW accuracy 0,01 + # Power bat1 - bat12: INT_16 in kW accuracy 0,01 power_regs = [0x0606, 0x060D, 0x0614, 0x061B, 0x0622, 0x0629, 0x0630, 0x0637, 0x0646, 0x064D, 0x0654, 0x065B] power = sum(client.read_holding_registers(power_regs[idx], ModbusDataType.INT_16, unit=self.__modbus_id) From 1d1b791a8b2fb68422847cff45188c860e0e5073 Mon Sep 17 00:00:00 2001 From: ndrsnhs Date: Mon, 7 Apr 2025 15:21:23 +0200 Subject: [PATCH 7/7] correct inverter factor --- packages/modules/devices/sofar/sofar/inverter.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/modules/devices/sofar/sofar/inverter.py b/packages/modules/devices/sofar/sofar/inverter.py index 97f715bf26..c840913ef2 100644 --- a/packages/modules/devices/sofar/sofar/inverter.py +++ b/packages/modules/devices/sofar/sofar/inverter.py @@ -22,7 +22,7 @@ def __init__(self, def update(self, client: ModbusTcpClient_) -> None: # 0x05C4 Power_PV_Total UINT16 in kW accuracy 0,1 - power = client.read_holding_registers(0x05C4, ModbusDataType.UINT_16, unit=self.__modbus_id) / -10 + power = client.read_holding_registers(0x05C4, ModbusDataType.UINT_16, unit=self.__modbus_id) * -100 exported = client.read_holding_registers(0x0686, ModbusDataType.UINT_32, unit=self.__modbus_id) * 100 inverter_state = InverterState(