Force STATE_CLASS_TOTAL_INCREASING to reset to 0 (#54751)

* Force STATE_CLASS_TOTAL_INCREASING to reset to 0

* Tweak

* Correct detection of new cycle

* Fix typing
This commit is contained in:
Erik Montnemery 2021-08-17 23:05:31 +02:00 committed by GitHub
parent 8eec949835
commit 3a78f1fce6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 8 additions and 4 deletions

View file

@ -308,7 +308,7 @@ def compile_statistics(
elif old_state is None and last_reset is None: elif old_state is None and last_reset is None:
reset = True reset = True
elif state_class == STATE_CLASS_TOTAL_INCREASING and ( elif state_class == STATE_CLASS_TOTAL_INCREASING and (
old_state is None or fstate < old_state old_state is None or (new_state is not None and fstate < new_state)
): ):
reset = True reset = True
@ -319,7 +319,11 @@ def compile_statistics(
# ..and update the starting point # ..and update the starting point
new_state = fstate new_state = fstate
old_last_reset = last_reset old_last_reset = last_reset
old_state = new_state # Force a new cycle for STATE_CLASS_TOTAL_INCREASING to start at 0
if state_class == STATE_CLASS_TOTAL_INCREASING and old_state:
old_state = 0
else:
old_state = new_state
else: else:
new_state = fstate new_state = fstate

View file

@ -383,7 +383,7 @@ def test_compile_hourly_sum_statistics_total_increasing(
"min": None, "min": None,
"last_reset": None, "last_reset": None,
"state": approx(factor * seq[5]), "state": approx(factor * seq[5]),
"sum": approx(factor * 40.0), "sum": approx(factor * 50.0),
}, },
{ {
"statistic_id": "sensor.test1", "statistic_id": "sensor.test1",
@ -393,7 +393,7 @@ def test_compile_hourly_sum_statistics_total_increasing(
"min": None, "min": None,
"last_reset": None, "last_reset": None,
"state": approx(factor * seq[8]), "state": approx(factor * seq[8]),
"sum": approx(factor * 70.0), "sum": approx(factor * 80.0),
}, },
] ]
} }