@@ -810,14 +810,58 @@ def _sum_l123(phase_info: dict) -> float:
810810 metric_unit = phase_info .get (om .unit .value )
811811
812812 if metric_unit == DEFAULT_POWER_UNIT :
813- self . _metrics [( target_cid , metric )]. value = metric_value / 1000
814- self . _metrics [( target_cid , metric )]. unit = HA_POWER_UNIT
813+ final_value = metric_value / 1000
814+ final_unit = HA_POWER_UNIT
815815 elif metric_unit == DEFAULT_ENERGY_UNIT :
816- self . _metrics [( target_cid , metric )]. value = metric_value / 1000
817- self . _metrics [( target_cid , metric )]. unit = HA_ENERGY_UNIT
816+ final_value = metric_value / 1000
817+ final_unit = HA_ENERGY_UNIT
818818 else :
819- self ._metrics [(target_cid , metric )].value = metric_value
820- self ._metrics [(target_cid , metric )].unit = metric_unit
819+ final_value = metric_value
820+ final_unit = metric_unit
821+
822+ self ._metrics [(target_cid , metric )].value = final_value
823+ self ._metrics [(target_cid , metric )].unit = final_unit
824+
825+ # Amend session energy based on incoming Energy.Active.Import.Register values if the charger does not report session energy directly.
826+ if metric == DEFAULT_MEASURAND and not getattr (
827+ self , "_charger_reports_session_energy" , False
828+ ):
829+ # Verify we are in an active transaction
830+ tx_metric = self ._metrics .get (
831+ (
832+ target_cid ,
833+ csess .transaction_id .value ,
834+ )
835+ )
836+
837+ if tx_metric and tx_metric .value :
838+ # Get meter start and session energy metrics
839+ ms_metric = self ._metrics .get (
840+ (
841+ target_cid ,
842+ csess .meter_start .value ,
843+ )
844+ )
845+ se_metric = self ._metrics .get (
846+ (
847+ target_cid ,
848+ csess .session_energy .value ,
849+ )
850+ )
851+
852+ if ms_metric and se_metric :
853+ # Initialize baseline if missing
854+ if ms_metric .value is None :
855+ ms_metric .value = final_value
856+ ms_metric .unit = final_unit
857+ se_metric .value = 0.0
858+ se_metric .unit = final_unit
859+ # Session Energy Math: Current Total - Start Total
860+ elif ms_metric .unit == final_unit :
861+ se_metric .value = (
862+ round (1000 * (final_value - ms_metric .value )) / 1000
863+ )
864+ se_metric .unit = final_unit
821865
822866 @staticmethod
823867 def get_energy_kwh (measurand_value : MeasurandValue ) -> float :
@@ -982,7 +1026,9 @@ def process_measurands(
9821026 ].value
9831027 else :
9841028 # Initialize baseline on first tx-bound EAIR; then derive Session = EAIR - meter_start.
985- ms_metric = self ._metrics [(target_cid , csess .meter_start )]
1029+ ms_metric = self ._metrics [
1030+ (target_cid , csess .meter_start .value )
1031+ ]
9861032 if ms_metric .value is None :
9871033 ms_metric .value = value
9881034 ms_metric .unit = unit
@@ -1000,7 +1046,15 @@ def process_measurands(
10001046 (target_cid , csess .session_energy .value )
10011047 ].unit = unit
10021048 else :
1003- unprocessed .append (sampled_value )
1049+ normalized_value = MeasurandValue (
1050+ measurand = measurand ,
1051+ value = value ,
1052+ phase = phase ,
1053+ unit = unit ,
1054+ context = context ,
1055+ location = location ,
1056+ )
1057+ unprocessed .append (normalized_value )
10041058
10051059 try :
10061060 self .process_phases (unprocessed , connector_id )
0 commit comments