Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 35 additions & 19 deletions packages/modules/devices/alpha_ess/alpha_ess/counter.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,36 +40,52 @@ def update(self):
0x6, [modbus.ModbusDataType.INT_32] * 3, unit=self.__modbus_id)
exported *= 10
imported *= 10
imported, exported = self.peak_filter.check_values(power, imported, exported)
currents = [val / 230 for val in self.__tcp_client.read_holding_registers(
Comment thread
benderl marked this conversation as resolved.
0x0000, [ModbusDataType.INT_32]*3, unit=self.__modbus_id)]
counter_state = CounterState(
currents=currents,
imported=imported,
exported=exported,
power=power
)
else:
power = self.__tcp_client.read_holding_registers(0x0021, ModbusDataType.INT_32, unit=self.__modbus_id)
exported, imported = [
val * 10 for val in self.__tcp_client.read_holding_registers(
0x0010, [ModbusDataType.INT_32] * 2, unit=self.__modbus_id
)]
currents = [val / 10 for val in self.__tcp_client.read_holding_registers(
0x0017, [ModbusDataType.INT_16]*3, unit=self.__modbus_id)]
powers = self.__tcp_client.read_holding_registers(
0x001b, [ModbusDataType.INT_32]*3, unit=self.__modbus_id)
voltages = self.__tcp_client.read_holding_registers(
0x0014, [ModbusDataType.UINT_16]*3, unit=self.__modbus_id)
imported, exported = self.peak_filter.check_values(power, imported, exported)
frequency = self.__tcp_client.read_holding_registers(
Comment thread
benderl marked this conversation as resolved.
0x001A, ModbusDataType.UINT_16, unit=self.__modbus_id) / 100
imported, exported = self.peak_filter.check_values(power, imported, exported)
counter_state = CounterState(
currents=currents,
imported=imported,
exported=exported,
power=power
)
if 'powers' in locals():
counter_state.powers = powers
if 'voltages' in locals():
counter_state.voltages = voltages
if 'frequency' in locals():
counter_state.frequency = frequency
currents = self.__tcp_client.read_holding_registers(
0x0017, [ModbusDataType.INT_16]*3, unit=self.__modbus_id)
powers = self.__tcp_client.read_holding_registers(
0x001b, [ModbusDataType.INT_32]*3, unit=self.__modbus_id)
currents = scale_currents(currents, powers)
counter_state = CounterState(
Comment thread
benderl marked this conversation as resolved.
currents=currents,
imported=imported,
exported=exported,
power=power,
powers=powers,
frequency=frequency
)
self.store.set(counter_state)


def scale_currents(currents, powers):
factors = [-1000, -100, -10, 10, 100, 1000]
scaled_currents = []
for c, p in zip(currents, powers):
if p == 0:
scaled_currents.append(0)
else:
factor = c * 230 / p
rounded_factor = min(factors, key=lambda z: abs(factor - z))
Copy link

Copilot AI Apr 8, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

scale_currents() selects the nearest scaling factor via min(factors, key=...). In exact tie cases (e.g. computed factor exactly midway between 10 and 100), min(...) will pick the earlier entry in factors, which currently biases toward the smaller magnitude and can mis-scale currents by 10×. Consider making the selection deterministic for ties (e.g. prefer the larger magnitude) or switching to threshold-based rounding on abs(factor) so boundary cases don’t produce large errors.

Suggested change
rounded_factor = min(factors, key=lambda z: abs(factor - z))
rounded_factor = min(factors, key=lambda z: (abs(factor - z), -abs(z)))

Copilot uses AI. Check for mistakes.
scaled_currents.append(c / rounded_factor)

Comment thread
benderl marked this conversation as resolved.
return scaled_currents


component_descriptor = ComponentDescriptor(configuration_factory=AlphaEssCounterSetup)
Loading