From b6e98ef9d9fbdbb2fa37e0875c7adb4482f9518d Mon Sep 17 00:00:00 2001 From: Jesse Kleemann Date: Wed, 10 Jul 2024 18:56:11 +0200 Subject: [PATCH] split config registers in tables --- config.sh10rt.example.yaml | 153 +------------------------------------ main.py | 38 +++++---- 2 files changed, 27 insertions(+), 164 deletions(-) diff --git a/config.sh10rt.example.yaml b/config.sh10rt.example.yaml index 96f9017..17004d7 100644 --- a/config.sh10rt.example.yaml +++ b/config.sh10rt.example.yaml @@ -6,7 +6,7 @@ ip: 192.168.0.55 # ip of Sungrow LAN-Port update_rate: 2 # pull rate in seconds address_offset: -1 scan_batching: 120 -registers: +holding: # # - #pub_topic: "date_time/minutes" # # address: 5004 # # - #pub_topic: "date_time/seconds" @@ -78,28 +78,23 @@ registers: # address: 13052 # - pub_topic: "battery_type" # set_topic: "battery_type/set" - # type: uint16 # address: 13055 # retain: true # - pub_topic: "battery_nominal_voltage" - # type: uint16 # address: 13056 # retain: true # scale: 0.1 # unit: 'V' # - pub_topic: "battery_capacity_specified" - # type: uint16 # address: 13057 # retain: true # unit: 'Ah' - pub_topic: "max_soc" - type: uint16 address: 13058 retain: true scale: 0.1 unit: '%' - pub_topic: "min_soc" - type: uint16 address: 13059 retain: true scale: 0.1 @@ -108,7 +103,6 @@ registers: # set_topic: "no_export/partial/limit/set" # address: 13074 - pub_topic: "export_power_limitation_value" - type: uint16 address: 13074 retain: true unit: 'W' @@ -206,15 +200,10 @@ registers: # - pub_topic: "forced_charge/period_2/target_soc" # set_topic: "forced_charge/period_2/target_soc/set" # address: 13151 - # - # # The following registers are published together in one JSON message to replicate the functionality - # # of pvstats because I didn't want to change my MQTT -> influxdb pipeline when I migrated from pvstats - # # to modbus4mqtt for these numbers. - # + +input: - pub_topic: device_type_code - type: uint16 address: 5000 - table: input retain: true value_map: sh5k-20: 0xd09 @@ -233,134 +222,99 @@ registers: sh6.0rt: 0xe01 sh5.0rt: 0xe00 - pub_topic: nominal_output_power - type: uint16 address: 5001 - table: input retain: true scale: 0.1 unit: 'kW' - pub_topic: output_type - type: uint16 address: 5002 - table: input retain: true - pub_topic: daily_output_energy - type: uint16 address: 5003 - table: input retain: true scale: 0.1 unit: 'kWh' - pub_topic: total_output_energy type: uint32 address: 5004 - table: input retain: true scale: 0.1 unit: 'kWh' # - pub_topic: "solar" # json_key: total_run_time - # type: uint16 # address: 5006 - # table: input # retain: true - pub_topic: inside_temperature type: int16 address: 5008 - table: input retain: true scale: 0.1 unit: '°C' - pub_topic: mppt1_voltage - type: uint16 address: 5011 - table: input retain: true scale: 0.1 unit: 'V' - pub_topic: mppt1_current - type: uint16 address: 5012 - table: input retain: true scale: 0.1 unit: 'A' - pub_topic: mppt2_voltage - type: uint16 address: 5013 - table: input retain: true scale: 0.1 unit: 'V' - pub_topic: mppt2_current - type: uint16 address: 5014 - table: input retain: true scale: 0.1 unit: 'A' - pub_topic: total_dc_power type: uint32 address: 5017 - table: input retain: true unit: 'W' - pub_topic: phase_a_voltage - type: uint16 address: 5019 - table: input retain: true scale: 0.1 unit: 'V' - pub_topic: phase_b_voltage - type: uint16 address: 5020 - table: input retain: true scale: 0.1 unit: 'V' - pub_topic: phase_c_voltage - type: uint16 address: 5021 - table: input retain: true scale: 0.1 unit: 'V' # - pub_topic: "solar" # json_key: inverter_current - # type: uint16 # address: 5022 - # table: input # retain: true # scale: 0.1 # - pub_topic: "solar" # json_key: inverter_ac_output - # type: uint16 # address: 5031 - # table: input # retain: true - pub_topic: reactive_power type: int32 address: 5033 - table: input retain: true - pub_topic: power_factor type: int16 address: 5035 - table: input retain: true scale: 0.001 - pub_topic: grid_frequency - type: uint16 address: 5036 - table: input retain: true scale: 0.1 unit: 'Hz' - pub_topic: system_state - type: uint16 address: 13000 - table: input retain: true value_map: stop: 0x2 @@ -375,217 +329,166 @@ registers: restarting: 0x2501 running_in_external_ems_mode: 0x4000 - pub_topic: running_state - type: uint16 address: 13001 - table: input retain: true - pub_topic: running_state_pv_power - type: uint16 address: 13001 - table: input retain: true mask: 0x1 sensor_type: 'binary' - pub_topic: running_state_battery_charging - type: uint16 address: 13001 - table: input retain: true mask: 0x2 shift: 1 sensor_type: 'binary' - pub_topic: running_state_battery_discharging - type: uint16 address: 13001 - table: input retain: true mask: 0x4 shift: 2 sensor_type: 'binary' - pub_topic: running_state_positive_load_power - type: uint16 address: 13001 - table: input retain: true mask: 0x8 shift: 3 sensor_type: 'binary' - pub_topic: running_state_feed_in_power - type: uint16 address: 13001 - table: input retain: true mask: 0x10 shift: 4 sensor_type: 'binary' - pub_topic: running_state_import_power_from_grid - type: uint16 address: 13001 - table: input retain: true mask: 0x20 shift: 5 sensor_type: 'binary' - pub_topic: running_state_reserved - type: uint16 address: 13001 - table: input retain: true mask: 0x40 shift: 6 sensor_type: 'binary' - pub_topic: running_state_negative_load_power - type: uint16 address: 13001 - table: input retain: true mask: 0x80 shift: 7 sensor_type: 'binary' - pub_topic: daily_pv_generation - type: uint16 address: 13002 - table: input retain: true scale: 0.1 unit: 'kWh' - pub_topic: total_pv_generation type: uint32 address: 13003 - table: input retain: true scale: 0.1 unit: 'kWh' sensor_type: measurement - pub_topic: daily_export_power_from_pv - type: uint16 address: 13005 - table: input retain: true scale: 0.1 unit: 'kWh' - pub_topic: total_export_energy_from_pv type: uint32 address: 13006 - table: input retain: true scale: 0.1 unit: 'kWh' - pub_topic: load_power type: int32 address: 13008 - table: input retain: true unit: 'W' - pub_topic: export_power type: int32 address: 13010 - table: input retain: true unit: 'W' - pub_topic: daily_battery_charge_energy_from_pv - type: uint16 address: 13012 - table: input retain: true scale: 0.1 unit: 'kWh' - pub_topic: total_battery_charge_energy_from_pv type: uint32 address: 13013 - table: input retain: true scale: 0.1 unit: 'kWh' - pub_topic: co2_reduction type: uint32 address: 13015 - table: input retain: true scale: 0.1 unit: 'kg' - pub_topic: daily_direct_energy_consumption - type: uint16 address: 13017 - table: input retain: true scale: 0.1 unit: 'kWh' - pub_topic: total_direct_energy_consumption type: uint32 address: 13018 - table: input retain: true scale: 0.1 unit: 'kWh' - pub_topic: battery_voltage - type: uint16 address: 13020 - table: input retain: true scale: 0.1 unit: 'V' - pub_topic: battery_current - type: uint16 address: 13021 - table: input retain: true scale: 0.1 unit: 'A' - pub_topic: battery_power - type: uint16 address: 13022 - table: input retain: true unit: 'W' - pub_topic: battery_level - type: uint16 address: 13023 - table: input retain: true scale: 0.1 unit: '%' class: 'battery' - pub_topic: battery_state_of_health - type: uint16 address: 13024 - table: input retain: true scale: 0.1 unit: '%' - pub_topic: battery_temperature type: int16 address: 13025 - table: input retain: true scale: 0.1 unit: '°C' - pub_topic: daily_battery_discharge_energy - type: uint16 address: 13026 - table: input retain: true scale: 0.1 unit: 'kWh' - pub_topic: total_battery_discharge_energy type: uint32 address: 13027 - table: input retain: true scale: 0.1 unit: 'kWh' sensor_type: measurement - pub_topic: self_consumption_of_today - type: uint16 address: 13029 - table: input retain: true scale: 0.1 unit: '%' - pub_topic: grid_state - type: uint16 address: 13030 - table: input retain: true # value_map: # off_grid: 170 @@ -593,178 +496,128 @@ registers: - pub_topic: phase_a_current type: int16 address: 13031 - table: input retain: true scale: 0.1 unit: 'A' - pub_topic: phase_b_current type: int16 address: 13032 - table: input retain: true scale: 0.1 unit: 'A' - pub_topic: phase_c_current type: int16 address: 13033 - table: input retain: true scale: 0.1 unit: 'A' - pub_topic: total_active_power type: int32 address: 13034 - table: input retain: true unit: 'W' - pub_topic: daily_import_energy - type: uint16 address: 13036 - table: input retain: true scale: 0.1 unit: 'kWh' - pub_topic: total_import_energy type: uint32 address: 13037 - table: input retain: true scale: 0.1 unit: 'kWh' sensor_type: measurement - pub_topic: battery_capacity - type: uint16 address: 13039 - table: input retain: true scale: 0.1 unit: 'kWh' - pub_topic: daily_charge_energy - type: uint16 address: 13040 - table: input retain: true scale: 0.1 unit: 'kWh' - pub_topic: total_charge_energy type: uint32 address: 13041 - table: input retain: true scale: 0.1 unit: 'kWh' sensor_type: measurement - pub_topic: daily_export_energy - type: uint16 address: 13045 - table: input retain: true scale: 0.1 unit: 'kWh' - pub_topic: total_export_energy type: uint32 address: 13046 - table: input retain: true scale: 0.1 unit: 'kWh' sensor_type: measurement - pub_topic: bms_status - type: uint16 address: 13100 - table: input retain: true - pub_topic: max_charging_current - type: uint16 address: 13101 - table: input retain: true unit: 'A' - pub_topic: max_discharging_current - type: uint16 address: 13102 - table: input retain: true unit: 'A' - pub_topic: warning - type: uint16 address: 13103 - table: input retain: true - pub_topic: protection - type: uint16 address: 13104 - table: input retain: true - pub_topic: fault_1 - type: uint16 address: 13105 - table: input retain: true - pub_topic: fault_2 - type: uint16 address: 13106 - table: input retain: true - pub_topic: soc - type: uint16 address: 13107 - table: input retain: true - pub_topic: soh - type: uint16 address: 13108 - table: input retain: true - pub_topic: battery_current_2 - type: uint16 address: 13109 - table: input retain: true - pub_topic: battery_voltage_2 - type: uint16 address: 13110 - table: input retain: true scale: 0.01 unit: 'V' - pub_topic: cycle_count - type: uint16 address: 13111 - table: input retain: true - pub_topic: average_cell_voltage - type: uint16 address: 13112 - table: input retain: true - pub_topic: max_cell_voltage - type: uint16 address: 13113 - table: input retain: true - pub_topic: min_cell_voltage - type: uint16 address: 13114 - table: input retain: true - pub_topic: battery_pack_voltage - type: uint16 address: 13115 - table: input retain: true - pub_topic: average_cell_temp type: int16 address: 13116 - table: input retain: true - pub_topic: max_cell_temp type: int16 address: 13117 - table: input retain: true - pub_topic: min_cell_temp type: int16 address: 13118 - table: input retain: true diff --git a/main.py b/main.py index c58a728..e99e034 100644 --- a/main.py +++ b/main.py @@ -34,7 +34,7 @@ def __init__(self): 'holding': {}, 'input': {}, } - self.init_register() + self.init_registers() def loop(self): while True: @@ -50,7 +50,7 @@ def exit_handler(self, signum, frame): self.modbus_handler.close() sys.exit(0) - def create_register(self, config_register): + def create_register(self, register_table, config_register): register = {'topic': config_register['pub_topic']} if 'type' in config_register: register['type'] = config_register['type'].strip().lower() @@ -66,21 +66,31 @@ def create_register(self, config_register): if 'shift' in config_register: register['shift'] = config_register['shift'] if register['type'].endswith('32'): - self.registers[config_register.get('table', 'holding')][ + self.registers[register_table][ config_register['address'] + self.address_offset + 1] = {'type': f'{register["type"]}_2'} return register - def init_register(self): - for register in config['registers']: - register_table = register.get('table', 'holding') - new_register = self.create_register(register) - register_address = register['address'] + self.address_offset - if register_address in self.registers[register_table]: - if 'multi' not in self.registers[register_table][register_address]: - self.registers[register_table][register_address]['multi'] = [] - self.registers[register_table][register_address]['multi'].append(new_register) - continue - self.registers[register_table][register_address] = new_register + def init_register(self, register_table, register): + new_register = self.create_register(register_table, register) + register_address = register['address'] + self.address_offset + if register_address in self.registers[register_table]: + if 'multi' not in self.registers[register_table][register_address]: + self.registers[register_table][register_address]['multi'] = [] + self.registers[register_table][register_address]['multi'].append(new_register) + return + self.registers[register_table][register_address] = new_register + + def init_registers(self): + if 'registers' in config: + for register in config['registers']: + register_table = register.get('table', 'holding') + self.init_register(register_table, register) + if 'input' in config: + for register in config['input']: + self.init_register('input', register) + if 'holding' in config: + for register in config['holding']: + self.init_register('holding', register) def read(self): for table in self.registers: