Skip to content

Add support for total and total_increasing sensor state classes#54523

Merged
emontnemery merged 8 commits intohome-assistant:devfrom
emontnemery:state_class_meter
Aug 13, 2021
Merged

Add support for total and total_increasing sensor state classes#54523
emontnemery merged 8 commits intohome-assistant:devfrom
emontnemery:state_class_meter

Conversation

@emontnemery
Copy link
Copy Markdown
Contributor

@emontnemery emontnemery commented Aug 12, 2021

Breaking change

Although not a breaking change from a user's perspective, this is a significant change from a developer's perspective.
It should be noted that the state class total was removed.

Details here: home-assistant/developers.home-assistant#1028

Proposed change

Add support for total and total_increasing sensor state classes:

# The state represents a measurement in present time
STATE_CLASS_MEASUREMENT: Final = "measurement"
# The state represents a total amount, e.g. a value of a stock portfolio
STATE_CLASS_TOTAL: Final = "total"
# The state represents a monotonically increasing total, e.g. an amount of consumed gas
STATE_CLASS_TOTAL_INCREASING: Final = "total_increasing"

If the sensor's state class is STATE_CLASS_TOTAL, the last_reset attribute can optionally be set to indicate a new meter cycle.

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. The last_reset attribute will be ignored when compiling statistics. This state class is useful for gas meters, electricity meters, water meters etc.

Type of change

  • Dependency upgrade
  • Bugfix (non-breaking change which fixes an issue)
  • New integration (thank you!)
  • New feature (which adds functionality to an existing integration)
  • Breaking change (fix/feature causing existing functionality to break)
  • Code quality improvements to existing code or addition of tests

Additional information

Checklist

  • The code change is tested and works locally.
  • Local tests pass. Your PR cannot be merged unless tests pass
  • There is no commented out code in this PR.
  • I have followed the development checklist
  • The code has been formatted using Black (black --fast homeassistant tests)
  • Tests have been added to verify that the new code works.

If user exposed functionality or configuration variables are added/changed:

If the code communicates with devices, web services, or third-party tools:

  • The manifest file has all fields filled out correctly.
    Updated and included derived files by running: python3 -m script.hassfest.
  • New or updated dependencies have been added to requirements_all.txt.
    Updated by running python3 -m script.gen_requirements_all.
  • Untested files have been added to .coveragerc.

The integration reached or maintains the following Integration Quality Scale:

  • No score or internal
  • 🥈 Silver
  • 🥇 Gold
  • 🏆 Platinum

To help with the load of incoming pull requests:

@emontnemery emontnemery changed the title Add support for amount and meter sensor state classes Add support for total and total_increasing sensor state classes Aug 12, 2021
elif old_state is None and last_reset is None:
reset = True
elif state_class == STATE_CLASS_TOTAL_INCREASING and (
old_state is None or fstate < old_state
Copy link
Copy Markdown
Member

@TomBrien TomBrien Aug 12, 2021

Choose a reason for hiding this comment

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

Might be worth usings omething like fstate < 0.9 * old_state? I'm just thinking of cases where a slightly noisy energy sensor is being used in an integration to calculate power. If the sensor is measuring 0 +/- some noise it is possible for the value to creep down a small amount without being a true reset

Copy link
Copy Markdown
Contributor Author

@emontnemery emontnemery Aug 12, 2021

Choose a reason for hiding this comment

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

I think that needs to be handled by the integration if it claims STATE_CLASS_TOTAL_INCREASING.
In the example with integrating a noisy power sensor (I think that's what you meant?) it could perhaps be handled by ignoring negative power?

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.

Agreed. I think it's best that the integration claiming that the sensor will only increase ensures that is true. I thought of some others where a less than could be an issue but in all cases the integration should know what it's doing better

@emontnemery emontnemery merged commit 029873a into home-assistant:dev Aug 13, 2021
"""Return state attributes."""
if last_reset := self.last_reset:
if (
last_reset is not None
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.

This is not needed, L162 will not continue if this is None

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.

Thanks, added to #54624

@frenck
Copy link
Copy Markdown
Member

frenck commented Aug 25, 2021

Removed the breaking change label, as it never went to stable.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants