Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Infer a limited history chart from state object #19176

Merged
merged 6 commits into from
Feb 28, 2024

Conversation

karwosts
Copy link
Contributor

Proposed change

If we have a very stable sensor that rarely changes, eventually its last recorded datapoint in the states database may be purged, at which point there is no recorded information for that entity.

If user has a history graph of that entity, at this point it will no longer render, the entity will just silently disappear from the chart, or if that is the only entity, it will display the message "No state history found".

This is suboptimal as it leaves users to think their entity has somehow broken or gone unavailable.

To improve this situation, I believe we can use the information from the entity state object to infer a limited history for an entity which can be displayed in the history chart. Given that we still know the entity's state, and the last updated timestamp, we can show an accurate chart for that entity instead of letting it disappear.

Type of change

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

Example configuration

Additional information

Checklist

  • The code change is tested and works locally.
  • There is no commented out code in this PR.
  • Tests have been added to verify that the new code works.

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

@karwosts karwosts marked this pull request as draft December 29, 2023 00:58
@karwosts karwosts marked this pull request as ready for review December 29, 2023 02:08
@@ -114,7 +114,7 @@ export class StateHistoryChartTimeline extends LitElement {
config: this.hass.config,
},
},
suggestedMin: this.startTime,
min: this.startTime.getTime(),
Copy link
Member

Choose a reason for hiding this comment

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

Why do we need getTime here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Struggling to remember exactly, I think maybe at one time the custom timeline controller didn't handle Date correctly, but I might have fixed that at some point. I'll see if it works just as well with removing it.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Couldn't find any issue, removed it 🤷

{
s: hass.states[entity].state,
a: hass.states[entity].attributes,
lu: new Date(hass.states[entity].last_updated).getTime() / 1000,
Copy link
Member

Choose a reason for hiding this comment

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

I think this should be last_changed and not last_updated?

Copy link
Contributor Author

@karwosts karwosts Feb 28, 2024

Choose a reason for hiding this comment

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

I think I convinced myself that last_updated would be more correct, in case we were displaying a chart with attributes (like climate).

If we use last_changed, then we would render that the attributes were constant all the way back to the time of the last state change, but that would be possibly inaccurate, as the attributes may have been changing after that point.

If we cutoff the graph at the time of last attribute change, then we display potentially less data, but guarantee that what is shown is actually correct.

];
}
});
Object.keys(stateHistory).forEach((entityId) => {
Copy link
Member

Choose a reason for hiding this comment

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

Can we combine Object.keys(stateHistory) and entityIds at the top so we dont have to go over stateHistory again?

@bramkragten bramkragten enabled auto-merge (squash) February 28, 2024 15:13
@bramkragten bramkragten merged commit dd98ec7 into home-assistant:dev Feb 28, 2024
9 checks passed
@karwosts karwosts deleted the stateObj-chart branch February 28, 2024 15:25
@ildar170975
Copy link
Contributor

Could this PR cause this behaviour? #20008

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

History Graph Card not showing entities if they have been constant for too long
3 participants