Make new cycles for sensor sum statistics start with 0 as zero-point (#55473)

This commit is contained in:
Erik Montnemery 2021-08-31 10:45:17 +02:00 committed by GitHub
parent 88a08fdf57
commit f9225bad5f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 12 additions and 15 deletions

View file

@ -403,11 +403,8 @@ def compile_statistics(
# ..and update the starting point
new_state = fstate
old_last_reset = last_reset
# Force a new cycle for STATE_CLASS_TOTAL_INCREASING to start at 0
if (
state_class == STATE_CLASS_TOTAL_INCREASING
and old_state is not None
):
# Force a new cycle for an existing sensor to start at 0
if old_state is not None:
old_state = 0.0
else:
old_state = new_state

View file

@ -258,7 +258,7 @@ def test_compile_hourly_sum_statistics_amount(
"min": None,
"last_reset": process_timestamp_to_utc_isoformat(four),
"state": approx(factor * seq[5]),
"sum": approx(factor * 10.0),
"sum": approx(factor * 40.0),
},
{
"statistic_id": "sensor.test1",
@ -268,7 +268,7 @@ def test_compile_hourly_sum_statistics_amount(
"min": None,
"last_reset": process_timestamp_to_utc_isoformat(four),
"state": approx(factor * seq[8]),
"sum": approx(factor * 40.0),
"sum": approx(factor * 70.0),
},
]
}
@ -512,7 +512,7 @@ def test_compile_hourly_energy_statistics_unsupported(hass_recorder, caplog):
"min": None,
"last_reset": process_timestamp_to_utc_isoformat(four),
"state": approx(40.0),
"sum": approx(10.0),
"sum": approx(40.0),
},
{
"statistic_id": "sensor.test1",
@ -522,7 +522,7 @@ def test_compile_hourly_energy_statistics_unsupported(hass_recorder, caplog):
"min": None,
"last_reset": process_timestamp_to_utc_isoformat(four),
"state": approx(70.0),
"sum": approx(40.0),
"sum": approx(70.0),
},
]
}
@ -595,7 +595,7 @@ def test_compile_hourly_energy_statistics_multiple(hass_recorder, caplog):
"min": None,
"last_reset": process_timestamp_to_utc_isoformat(four),
"state": approx(40.0),
"sum": approx(10.0),
"sum": approx(40.0),
},
{
"statistic_id": "sensor.test1",
@ -605,7 +605,7 @@ def test_compile_hourly_energy_statistics_multiple(hass_recorder, caplog):
"min": None,
"last_reset": process_timestamp_to_utc_isoformat(four),
"state": approx(70.0),
"sum": approx(40.0),
"sum": approx(70.0),
},
],
"sensor.test2": [
@ -627,7 +627,7 @@ def test_compile_hourly_energy_statistics_multiple(hass_recorder, caplog):
"min": None,
"last_reset": process_timestamp_to_utc_isoformat(four),
"state": approx(45.0),
"sum": approx(-95.0),
"sum": approx(-65.0),
},
{
"statistic_id": "sensor.test2",
@ -637,7 +637,7 @@ def test_compile_hourly_energy_statistics_multiple(hass_recorder, caplog):
"min": None,
"last_reset": process_timestamp_to_utc_isoformat(four),
"state": approx(75.0),
"sum": approx(-65.0),
"sum": approx(-35.0),
},
],
"sensor.test3": [
@ -659,7 +659,7 @@ def test_compile_hourly_energy_statistics_multiple(hass_recorder, caplog):
"min": None,
"last_reset": process_timestamp_to_utc_isoformat(four),
"state": approx(50.0 / 1000),
"sum": approx(30.0 / 1000),
"sum": approx(60.0 / 1000),
},
{
"statistic_id": "sensor.test3",
@ -669,7 +669,7 @@ def test_compile_hourly_energy_statistics_multiple(hass_recorder, caplog):
"min": None,
"last_reset": process_timestamp_to_utc_isoformat(four),
"state": approx(90.0 / 1000),
"sum": approx(70.0 / 1000),
"sum": approx(100.0 / 1000),
},
],
}