Skip to content
Merged
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 74 additions & 0 deletions blog/2021-08-12-state_class_total.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
---
author: Erik Montnémery
authorURL: https://github.com/emontnemery
title: "New sensor state classes: total and total_increasing"
---

Two new state classes, `STATE_CLASS_TOTAL` and `STATE_CLASS_TOTAL_INCREASING` have been
added. In addition, it is no longer mandatory to set the `last_reset` attribute in order
for `sum` statistics to be generated. The driver for the changes is to make it easier to
integrate with utility meters etc.
Comment thread
emontnemery marked this conversation as resolved.
Outdated

### State classes

There are now 3 defined state classes:
- `STATE_CLASS_MEASUREMENT`, the state represents a measurement in present time, for
example a temperature, electric power etc.
Comment thread
emontnemery marked this conversation as resolved.
Outdated
- `STATE_CLASS_TOTAL`, the state represents a total amount which can both increase and
Comment thread
emontnemery marked this conversation as resolved.
Outdated
decrease, e.g. the value of a stock portfolio
- `STATE_CLASS_TOTAL_INCREASING`, a monotonically increasing total, e.g. an amount of
consumed gas, water or energy

#### STATE_CLASS_TOTAL
If the sensor's state class is `STATE_CLASS_TOTAL`, the last_reset attribute can
Comment thread
emontnemery marked this conversation as resolved.
Outdated
optionally be set to gain manual control of meter cycles; each time last_reset changes
the corresponding value is used as the zero-point when calculating `sum` statistics.
If last_reset is not set, the sensor's value when it was first added is used as the

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"first added" -> when looking at the examples it looks like it uses the last state as the zero-point, not the first state.

zero-point when calculating `sum` statistics.

Example of `STATE_CLASS_TOTAL` without last_reset:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are great examples and shouldn't be in a blog post but in the docs instead.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think they can be both here and in the docs?


| t | state | sum |
| ---: | -----: | -----: |
| 0 | 1000 | 0 |
| 1 | 1010 | 10 |
| 2 | 0 | -1000 |
Comment thread
Danielhiversen marked this conversation as resolved.
Outdated
| 3 | 5 | -995 |

Example of `STATE_CLASS_TOTAL` with last_reset:

| t | state | last_reset | sum |
| ---: | -----: | ------------------- | -----: |
| 0 | 1000 | 2021-08-01T13:30:00 | 0 |
| 1 | 1010 | 2021-08-01T13:30:00 | 10 |
| 2 | 1005 | 2021-08-01T13:30:00 | 5 |
| 3 | 0 | 2021-09-01T13:30:00 | 5 |
| 4 | 5 | 2021-09-01T13:30:00 | 10 |
Comment thread
frenck marked this conversation as resolved.
Outdated


#### STATE_CLASS_TOTAL_INCREASING
Comment thread
emontnemery marked this conversation as resolved.
Outdated
If the sensor's state class is `STATE_CLASS_TOTAL_INCREASING`, a decreasing value is
interpreted as the start of a new meter cycle or the replacement of the meter. It is
Comment thread
emontnemery marked this conversation as resolved.
Outdated
important that the integration ensures that the value cannot erroneously decrease in
the case of calculating a value from a sensor with measurement noise present. The
last_reset attribute will be ignored when compiling statistics. This state class is
useful for gas meters, electricity meters, water meters etc. The value when the sensor
reading decreases will be used as zero-point when calculating `sum` statistics.

Example of `STATE_CLASS_TOTAL_INCREASING`:

| t | state | sum |
| ---: | -----: | ---: |
| 0 | 1000 | 0 |
| 1 | 1010 | 10 |
| 2 | 0 | 10 |
| 3 | 5 | 15 |

Example of `STATE_CLASS_TOTAL_INCREASING` where the sensor does not reset to 0:

| t | state | sum |
| ---: | -----: | ---: |
| 0 | 1000 | 0 |
| 1 | 1010 | 10 |
| 2 | 5 | 10 |
| 3 | 10 | 15 |