Skip to content

Commit bb23ee7

Browse files
authored
min delta should be 0.0, use total if negative (#2993)
1 parent 8d9f3a7 commit bb23ee7

2 files changed

Lines changed: 3 additions & 2 deletions

File tree

drivers/SmartThings/matter-switch/src/switch_utils/utils.lua

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -402,12 +402,13 @@ function utils.report_power_consumption_to_st_energy(device, endpoint_id, total_
402402

403403
local previous_imported_report = utils.get_latest_state_for_endpoint(device, endpoint_id, capabilities.powerConsumptionReport.ID,
404404
capabilities.powerConsumptionReport.powerConsumption.NAME, { energy = total_imported_energy_wh }) -- default value if nil
405+
local delta_energy = total_imported_energy_wh - previous_imported_report.energy
405406
-- Report the energy consumed during the time interval. The unit of these values should be 'Wh'
406407
local epoch_to_iso8601 = function(time) return os.date("!%Y-%m-%dT%H:%M:%SZ", time) end -- Return an ISO-8061 timestamp from UTC
407408
device:emit_event_for_endpoint(endpoint_id, capabilities.powerConsumptionReport.powerConsumption({
408409
start = epoch_to_iso8601(last_time),
409410
["end"] = epoch_to_iso8601(current_time - 1),
410-
deltaEnergy = total_imported_energy_wh - previous_imported_report.energy,
411+
deltaEnergy = delta_energy >= 0.0 and delta_energy or total_imported_energy_wh, -- clarifying assumption: a negative delta means the meter was reset
411412
energy = total_imported_energy_wh
412413
}))
413414
end

drivers/SmartThings/matter-switch/src/test/test_electrical_sensor_set.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -612,7 +612,7 @@ test.register_coroutine_test(
612612
mock_device_periodic:generate_test_message("main", capabilities.powerConsumptionReport.powerConsumption({
613613
start = "1970-01-01T00:15:01Z",
614614
["end"] = "1970-01-01T00:48:20Z",
615-
deltaEnergy = -4.0,
615+
deltaEnergy = 19.0,
616616
energy = 19.0
617617
}))
618618
)

0 commit comments

Comments
 (0)