Skip to content

Restore state_class to derivative sensor#163557

Merged
emontnemery merged 1 commit into
home-assistant:devfrom
karwosts:derivative_statistics
Apr 28, 2026
Merged

Restore state_class to derivative sensor#163557
emontnemery merged 1 commit into
home-assistant:devfrom
karwosts:derivative_statistics

Conversation

@karwosts
Copy link
Copy Markdown
Member

Proposed change

Restore statistics to derivative sensors.
Was previously removed in #88952

Previous issues:

Derivative sensor's restore state is not restoring the native_value
Derivative sensor's restore state does not restore the unit (this is why the recorder complains in the linked issue)
Derivate sensor doesn't set its state to unknown or unavailable when the input sensor is unknown or unavailable, the state just stops updating

Derivative now restores native_value and unit, as well as correctly handles unavailable during times when source is unavailable.
Related PRs:
#147468
#91071

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)
  • Deprecation (breaking change to happen in the future)
  • Breaking change (fix/feature causing existing functionality to break)
  • Code quality improvements to existing code or addition of tests

Additional information

Checklist

  • I understand the code I am submitting and can explain how it works.
  • 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
  • I have followed the perfect PR recommendations
  • The code has been formatted using Ruff (ruff format homeassistant tests)
  • Tests have been added to verify that the new code works.
  • Any generated code has been carefully reviewed for correctness and compliance with project standards.

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.
  • For the updated dependencies a diff between library versions and ideally a link to the changelog/release notes is added to the PR description.

To help with the load of incoming pull requests:

@home-assistant
Copy link
Copy Markdown
Contributor

Hey there @afaucogney, mind taking a look at this pull request as it has been labeled with an integration (derivative) you are listed as a code owner for? Thanks!

Code owner commands

Code owners of derivative can trigger bot actions by commenting:

  • @home-assistant close Closes the pull request.
  • @home-assistant rename Awesome new title Renames the pull request.
  • @home-assistant reopen Reopen the pull request.
  • @home-assistant unassign derivative Removes the current integration label and assignees on the pull request, add the integration domain after the command.
  • @home-assistant add-label needs-more-information Add a label (needs-more-information, problem in dependency, problem in custom component, problem in config, problem in device, feature-request) to the pull request.
  • @home-assistant remove-label needs-more-information Remove a label (needs-more-information, problem in dependency, problem in custom component, problem in config, problem in device, feature-request) on the pull request.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR restores the state_class = MEASUREMENT attribute to the derivative sensor, which was previously removed in PR #88952 due to state restoration issues. The underlying problems that caused the revert have been fixed in related PRs #147468 (handling unavailability) and #91071 (using RestoreSensor for proper state restoration). This change enables statistics collection for derivative sensors.

Changes:

  • Add _attr_state_class = SensorStateClass.MEASUREMENT to the DerivativeSensor class
  • Add test assertion to verify state_class attribute is present in sensor states

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
homeassistant/components/derivative/sensor.py Adds _attr_state_class = SensorStateClass.MEASUREMENT class attribute to enable statistics for derivative sensor
tests/components/derivative/test_sensor.py Adds assertion in setup_tests helper to verify state_class attribute is set correctly

Copy link
Copy Markdown
Member

@justanotherariel justanotherariel left a comment

Choose a reason for hiding this comment

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

Looks good to me. CC @emontnemery as you reverted this change previously - do you see any other reason that blocks this?

@Eagle3386
Copy link
Copy Markdown
Contributor

@emontnemery / @justanotherariel Can we proceed with this or is there anything @karwosts and/or I can do?
Because I just found myself desperately requiring this for my energy dashboard's gas meter where I need to use a derivative helper function for Gas Flow Rate (analog gas meter only provides a counter that's queried via reed switch, i.e., 1 pulse = 0.01 m³).

Copy link
Copy Markdown
Contributor

@emontnemery emontnemery left a comment

Choose a reason for hiding this comment

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

LGTM

@emontnemery emontnemery merged commit 2f3a624 into home-assistant:dev Apr 28, 2026
39 checks passed
@karwosts karwosts deleted the derivative_statistics branch April 28, 2026 12:24
@github-actions github-actions Bot locked and limited conversation to collaborators Apr 29, 2026
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.

5 participants