-
-
Notifications
You must be signed in to change notification settings - Fork 32.5k
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
Weight sensor average statistics by state durations #51150
Conversation
This is actually implemented in the filter sensor https://www.home-assistant.io/integrations/filter/ https://github.com/home-assistant/core/blob/dev/homeassistant/components/filter/sensor.py#L573 |
@dgomes Right, but the |
The filter sensor also uses history, I propose we do some refactoring in order for both to use the same code/algorithm. |
Right, I missed that it does that in an Priming of the filters from history is done like this: # Replay history through the filter chain
for state in history_list:
if state.state not in [STATE_UNKNOWN, STATE_UNAVAILABLE, None]:
self._update_filter_sensor_state(state, False) The complexity of this is O(n^2) for the What we could do is perhaps to add a helper which takes as input an iterable of some state abstraction, and only does the actual calculation: moving_sum = 0
start = new_state.timestamp - self._time_window
prev_state = self.last_leak or self.queue[0]
for state in self.queue:
moving_sum += (state.timestamp - start).total_seconds() * prev_state.state
start = state.timestamp
prev_state = state
new_state.state = moving_sum / self._time_window.total_seconds() |
I suggest we might need to get this refactoring out of the critical path. |
That's the way forward I believe |
In a follow-up PR for another time. |
* Weight sensor average statistics by state durations * Fix test
Proposed change
Weight sensor average statistics by state durations.
This makes the average calculation more accurate when there are successive occurrences of the same state and for sensors which updates states on an irregular basis.
Type of change
Additional information
Checklist
black --fast homeassistant tests
)If user exposed functionality or configuration variables are added/changed:
If the code communicates with devices, web services, or third-party tools:
Updated and included derived files by running:
python3 -m script.hassfest
.requirements_all.txt
.Updated by running
python3 -m script.gen_requirements_all
..coveragerc
.The integration reached or maintains the following Integration Quality Scale:
To help with the load of incoming pull requests: