Skip to content

Make derivative sensor unavailable when source sensor is unavailable#147468

Merged
frenck merged 2 commits into
home-assistant:devfrom
karwosts:derivative_unknown
Jul 4, 2025
Merged

Make derivative sensor unavailable when source sensor is unavailable#147468
frenck merged 2 commits into
home-assistant:devfrom
karwosts:derivative_unknown

Conversation

@karwosts
Copy link
Copy Markdown
Member

Proposed change

Make the derivative sensor unknown when its source sensor is non-numeric. The previous behavior just persists the previous value from the most recent time before the sensor had a bad value.

While the derivative sensor state is Unknown during the period its source sensor is non-numeric, as soon as the source sensor becomes available again, it calculates the derivative as if there was no gap in the source sensor, allowing brief periods of unavailability to not distort longer term derivative calculations.

Based on this revert PR this feature is one of the last missing pieces to re-enable statistics for Derivative sensor, which was previously reverted partially for this reason.

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

  • This PR fixes or closes issue: fixes #
  • This PR is related to issue:
  • Link to documentation pull request:
  • Link to developer documentation pull request:
  • Link to frontend pull request:

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
  • 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.

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 link to the changelog, or at minimum a diff between library versions 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) to the pull request.
  • @home-assistant remove-label needs-more-information Remove a label (needs-more-information, problem in dependency, problem in custom component) on the pull request.

Decimal(restored_data.native_value), # type: ignore[arg-type]
self._round_digits,
)
except SyntaxError as err:
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I can't figure out why this was here or what would ever thrown this Error.

@karwosts
Copy link
Copy Markdown
Member Author

karwosts commented Jun 25, 2025

I initially started out with Unavailable instead of Unknown, but that didn't seem to work as well with the RestoreSensor.

For some reason when I shutdown HA and the sensor was Unavailable, when I booted it up again it would always restore to it's last-known-good-state, which doesn't seem like the right thing to do as the source sensor was still unavailable on boot, and it didn't seem right to put a valid-looking value there.

When I shutdown HA and the value is Unknown, then it correctly restores as Unknown, which I think is what I want.

Copy link
Copy Markdown
Member

@frenck frenck left a comment

Choose a reason for hiding this comment

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

This is not the correct behavior IMHO, I think we should make this sensor unavailable when the source sensor is unavailable as well.

@home-assistant home-assistant Bot marked this pull request as draft June 27, 2025 18:33
@home-assistant
Copy link
Copy Markdown
Contributor

Please take a look at the requested changes, and use the Ready for review button when you are done, thanks 👍

Learn more about our pull request process.

@karwosts
Copy link
Copy Markdown
Member Author

karwosts commented Jun 28, 2025

Ok, changed to make it unavailable instead of unknown.

Behavior is something like this:

  • When added to hass, if source sensor is not setup, unavailable, or unknown, derivative is unavailable.

  • When source sensor becomes available for the first time, derivative sensor becomes available and equal to the RestoreSensor state.

  • When already added and source sensor becomes unavailable, derivative becomes unavailable.

  • When already added and source sensor becomes unknown or non-numeric, derivative becomes unknown.

I think that's my best idea of how the rules should work, but let me know if I've misunderstood anything.

@karwosts karwosts marked this pull request as ready for review June 28, 2025 19:26
@home-assistant home-assistant Bot requested a review from frenck June 28, 2025 19:26
@karwosts karwosts changed the title Make derivative sensor unknown when source sensor is unavailable Make derivative sensor unavailable when source sensor is unavailable Jul 1, 2025
@frenck frenck added the smash Indicator this PR is close to finish for merging or closing label Jul 3, 2025
Copy link
Copy Markdown
Member

@frenck frenck left a comment

Choose a reason for hiding this comment

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

Thanks, @karwosts 👍

../Frenck

@frenck frenck merged commit 22e46d9 into home-assistant:dev Jul 4, 2025
30 checks passed
@karwosts karwosts deleted the derivative_unknown branch July 4, 2025 20:52
@github-actions github-actions Bot locked and limited conversation to collaborators Jul 5, 2025
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.

2 participants