File tree Expand file tree Collapse file tree 1 file changed +19
-7
lines changed
custom_components/apsystems_ecu_reader Expand file tree Collapse file tree 1 file changed +19
-7
lines changed Original file line number Diff line number Diff line change @@ -665,16 +665,28 @@ def native_value(self):
665665 if (
666666 self ._last_update_time is not None
667667 and self ._last_power_value is not None
668- and self ._last_power_value < 0
669- ): # Only accumulate when exporting (negative power)
670-
668+ and current_power
669+ != self ._last_power_value # Only calculate when power changed (new ECU data)
670+ and self ._last_power_value
671+ < 0 # Only accumulate when previously exporting
672+ ):
671673 time_delta_hours = (
672674 current_time - self ._last_update_time
673675 ).total_seconds () / 3600
674- # Use absolute value of negative power for export energy calculation
675- energy_increment_kwh = (
676- abs (self ._last_power_value ) * time_delta_hours / 1000
677- )
676+
677+ # Use trapezoidal integration if both values are exporting (negative)
678+ if current_power < 0 :
679+ # Both exporting - use trapezoidal (average of both values)
680+ avg_export_power = (
681+ abs (self ._last_power_value ) + abs (current_power )
682+ ) / 2
683+ energy_increment_kwh = avg_export_power * time_delta_hours / 1000
684+ else :
685+ # Transition from export to import/zero - use rectangular with last value
686+ energy_increment_kwh = (
687+ abs (self ._last_power_value ) * time_delta_hours / 1000
688+ )
689+
678690 self ._state += energy_increment_kwh
679691
680692 # Update tracking variables
You can’t perform that action at this time.
0 commit comments