From 76dc190513d76b21f613dbed997616e8c84ea117 Mon Sep 17 00:00:00 2001 From: Sanghee Kim Date: Fri, 10 Jul 2026 11:54:00 +0900 Subject: [PATCH 1/3] Matter Energy: Fix powerConsumptionReport delta to use correct component state The energy delta calculation in `report_power_consumption_to_st_energy` was incorrectly reading the previous energy state from the "main" component regardless of whether the report was for imported or exported energy. This caused incorrect deltaEnergy values when multiple energy components were involved. Also update test_battery_storage.lua expectations to verify non-zero delta values (100 Wh) now that the correct component state is used. --- drivers/SmartThings/matter-energy/src/init.lua | 2 +- .../matter-energy/src/test/test_battery_storage.lua | 10 +++++----- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/drivers/SmartThings/matter-energy/src/init.lua b/drivers/SmartThings/matter-energy/src/init.lua index c6e44008d6..bbaf3c61c3 100644 --- a/drivers/SmartThings/matter-energy/src/init.lua +++ b/drivers/SmartThings/matter-energy/src/init.lua @@ -474,7 +474,7 @@ local function report_power_consumption_to_st_energy(device, component, latest_t -- Calculate the energy delta between reports local energy_delta_wh = 0.0 - local previous_imported_report = device:get_latest_state("main", capabilities.powerConsumptionReport.ID, + local previous_imported_report = device:get_latest_state(component.id, capabilities.powerConsumptionReport.ID, capabilities.powerConsumptionReport.powerConsumption.NAME) if previous_imported_report and previous_imported_report.energy then energy_delta_wh = math.max(latest_total_imported_energy_wh - previous_imported_report.energy, 0.0) diff --git a/drivers/SmartThings/matter-energy/src/test/test_battery_storage.lua b/drivers/SmartThings/matter-energy/src/test/test_battery_storage.lua index 329daa6bd7..4f077189a0 100644 --- a/drivers/SmartThings/matter-energy/src/test/test_battery_storage.lua +++ b/drivers/SmartThings/matter-energy/src/test/test_battery_storage.lua @@ -269,7 +269,7 @@ test.register_coroutine_test( mock_device:generate_test_message("importedEnergy", capabilities.powerConsumptionReport.powerConsumption({ energy = 200, - deltaEnergy = 0.0, + deltaEnergy = 100, start = "1970-01-01T00:15:01Z", ["end"] = "1970-01-01T00:48:20Z" })) @@ -278,20 +278,20 @@ test.register_coroutine_test( test.socket.matter:__queue_receive({ mock_device.id, clusters.ElectricalEnergyMeasurement.attributes .CumulativeEnergyExported:build_test_report_data(mock_device, BATTERY_STORAGE_EP, - clusters.ElectricalEnergyMeasurement.types.EnergyMeasurementStruct({ energy = 400000, start_timestamp = 0, end_timestamp = 0, start_systime = 0, end_systime = 0, apparent_energy = 0, reactive_energy = 0 })) }) --400Wh + clusters.ElectricalEnergyMeasurement.types.EnergyMeasurementStruct({ energy = 500000, start_timestamp = 0, end_timestamp = 0, start_systime = 0, end_systime = 0, apparent_energy = 0, reactive_energy = 0 })) }) --500Wh test.socket.capability:__expect_send( mock_device:generate_test_message("exportedEnergy", capabilities.energyMeter.energy({ - value = 400, unit = "Wh" + value = 500, unit = "Wh" })) ) test.socket.capability:__expect_send( mock_device:generate_test_message("exportedEnergy", capabilities.powerConsumptionReport.powerConsumption({ - energy = 400, - deltaEnergy = 0.0, + energy = 500, + deltaEnergy = 100, start = "1970-01-01T00:15:01Z", ["end"] = "1970-01-01T00:48:20Z" })) From a6b44574500ea35b34d514c1932fd38a3f3eb5cf Mon Sep 17 00:00:00 2001 From: Sanghee Kim Date: Fri, 10 Jul 2026 11:43:06 +0900 Subject: [PATCH 2/3] Matter Energy: Rename energy parameter to reflect both import/export usage The parameter `latest_total_imported_energy_wh` in the `report_power_consumption_to_st_energy` function was misleadingly named since the function handles both imported and exported energy reporting (determined by the component id). --- drivers/SmartThings/matter-energy/src/init.lua | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/drivers/SmartThings/matter-energy/src/init.lua b/drivers/SmartThings/matter-energy/src/init.lua index bbaf3c61c3..43e7367812 100644 --- a/drivers/SmartThings/matter-energy/src/init.lua +++ b/drivers/SmartThings/matter-energy/src/init.lua @@ -461,7 +461,7 @@ local function device_energy_mgmt_mode_attr_handler(driver, device, ib, response end end -local function report_power_consumption_to_st_energy(device, component, latest_total_imported_energy_wh) +local function report_power_consumption_to_st_energy(device, component, latest_total_energy_wh) local current_time = os.time() local last_report_timestamp_field = component.id == "exportedEnergy" and LAST_EXPORTED_REPORT_TIMESTAMP or LAST_IMPORTED_REPORT_TIMESTAMP @@ -477,7 +477,7 @@ local function report_power_consumption_to_st_energy(device, component, latest_t local previous_imported_report = device:get_latest_state(component.id, capabilities.powerConsumptionReport.ID, capabilities.powerConsumptionReport.powerConsumption.NAME) if previous_imported_report and previous_imported_report.energy then - energy_delta_wh = math.max(latest_total_imported_energy_wh - previous_imported_report.energy, 0.0) + energy_delta_wh = math.max(latest_total_energy_wh - previous_imported_report.energy, 0.0) end -- Report the energy consumed during the time interval. The unit of these values should be 'Wh' @@ -485,7 +485,7 @@ local function report_power_consumption_to_st_energy(device, component, latest_t start = epoch_to_iso8601(last_time), ["end"] = epoch_to_iso8601(current_time - 1), deltaEnergy = energy_delta_wh, - energy = latest_total_imported_energy_wh + energy = latest_total_energy_wh })) end From 162e25e8272b692a6c3a48fa715f5a5077b42474 Mon Sep 17 00:00:00 2001 From: Sanghee Kim Date: Fri, 10 Jul 2026 12:23:18 +0900 Subject: [PATCH 3/3] Matter Energy: Fix typo endopint_id to endpoint_id in evse_supply_state_handler Fix a typo in the `evse_supply_state_handler` function where `ib.endopint_id` was used instead of `ib.endpoint_id` when calling `device:endpoint_to_component()`. This typo would cause the handler to fail when looking up the correct component for the EVSE supply state. --- drivers/SmartThings/matter-energy/src/init.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/drivers/SmartThings/matter-energy/src/init.lua b/drivers/SmartThings/matter-energy/src/init.lua index 43e7367812..6ee3eeac7a 100644 --- a/drivers/SmartThings/matter-energy/src/init.lua +++ b/drivers/SmartThings/matter-energy/src/init.lua @@ -327,7 +327,7 @@ local function evse_supply_state_handler(driver, device, ib, response) local evse_supply_state = ib.data.value local latest_evse_state = device:get_latest_state( - device:endpoint_to_component(ib.endopint_id), + device:endpoint_to_component(ib.endpoint_id), capabilities.evseState.ID, capabilities.evseState.state.NAME )