Statistics
The statistics helper works out a number over the recent values of a sensor: the average humidity of the last half hour, the highest temperature of the day, how much the power changed over the last twenty readings. It keeps a set of samples, and drops them when there are too many or when they get too old.
How many samples, and how old they may get, is set in the helper's options, and only there. An automation cannot change it: not a shorter window during a shower, when an average over the last half hour lags far behind.
Spook adds an action for that: set sampling.
Actions
Spook adds one action to the statistics integration. You find it wherever you add an action, marked with a 👻.
Set sampling
Changes which samples one or more statistics helpers keep, like the options of the helper do: the sampling size, the maximum age, whether it keeps the last sample, and the percentile. What you leave out stays as it is, the sensor it follows and what it works out too.
The change goes through the helper's own options, so Home Assistant checks it the same way it checks what you fill in there. Then Home Assistant reloads the helper, as it does after you save its options. The action returns once the helper is reloaded, so the next step of your automation already sees it with its new settings. A helper that already has the settings you give is left alone, without a reload.
- Sampling size: the most samples the helper keeps.
- Maximum age: how old a sample may get before the helper drops it. With a sampling size too, the helper drops a sample at whichever of the two it reaches first.
- Keep last sample: keep the last sample past the maximum age, so the helper always has one.
- Percentile: the percentile the helper works out, when that is what it measures.
A statistics helper can be without a sampling size or without a maximum age. To take one away, give it null in YAML: sampling_size: null leaves the helper with only its maximum age. Taking away both is refused: the helper would keep every sample forever. So is keeping the last sample without a maximum age, since there is no age to keep it past. A maximum age of 0 is refused too: the helper takes that as no maximum age at all, so say that with null.
A helper with the same name, sensor and settings as another statistics helper is refused by Home Assistant, the same as when you make it twice. The error says so, and names the helper. It stays as it was.
Every entity is checked before the first helper changes. An entity that is not a statistics helper, or one set up in YAML, is refused with an error, and nothing changes. The helpers change one at a time, in the order you give them. When Home Assistant refuses the new settings of one, like taking away both its sampling size and its maximum age, the action stops with an error that names that helper. The helpers before it in the list did change; the ones after it did not.
Run by hand, the action takes an administrator, like the options of a helper do. Your automations can always run it. So can your scripts, unless a user who is not an administrator starts one.
How to use it
- Open the automation or script that should change the sampling, and select Add action.
- Search for set sampling, and select Statistics: Set sampling 👻.
- Choose the statistics helpers.
- Turn on the settings you want to change, and fill in the new values.
- Select Save.
To take the sampling size or the maximum age away, switch the action to YAML with the three dots menu, and set it to null.
| Action properties | |
|---|---|
| Action | Statistics: Set sampling 👻 |
| Action name | statistics.set_sampling |
| Action targets | No targets |
| Action response | No |
| Spook's influence | Newly added action |
| Tools | Try this action |
| Action data parameters | |||
|---|---|---|---|
| Attribute | Type | Required | Default / Example |
entity_id | string or list | Yes | sensor.bathroom_humidity_average |
sampling_size | integer or null | No | 20 |
max_age | time period or null | No | "00:05:00" |
keep_last_sample | boolean | No | true |
percentile | integer | No | 90 |
Give at least one of sampling_size, max_age, keep_last_sample and percentile. The sampling size is at least 1, the percentile from 1 to 99. The maximum age is a duration like "00:05:00", or hours, minutes and seconds like {"minutes": 5}, and longer than nothing. null takes the sampling size or the maximum age away, not both.
Keep the samples of the last five minutes:
action: statistics.set_sampling
data:
entity_id: sensor.bathroom_humidity_average
max_age: "00:05:00"
Take the sampling size away, so only the maximum age counts:
action: statistics.set_sampling
data:
entity_id: sensor.bathroom_humidity_average
sampling_size: null
The average bathroom humidity of the last half hour is the baseline the fan compares with. During a shower, that average lags far behind, and the fan keeps running long after the mirror is clear. While the shower runs, and for fifteen minutes after, the average covers the last five minutes:
alias: Bathroom humidity average quicker during a shower
triggers:
- trigger: state
entity_id: binary_sensor.bathroom_shower
from: "off"
to: "on"
id: shower
- trigger: state
entity_id: binary_sensor.bathroom_shower
from: "on"
to: "off"
for: "00:15:00"
id: done
actions:
- action: statistics.set_sampling
data:
entity_id: sensor.bathroom_humidity_average
max_age: "{{ '00:05:00' if trigger.id == 'shower' else '00:30:00' }}"
- Every change reloads the helper. Do not drive the sampling from something that changes every few seconds: change it when it matters, like when the shower starts.
- A reload empties the samples. The helper reads them back from the recorder, by its new settings: with a maximum age of five minutes, the samples of the last five minutes. Without any to read back, like for a sensor that did not change in that time or one the recorder leaves out, the helper is
unavailableuntil the sensor changes, and works with what came after the reload only. - While the helper reloads, its state is
unavailablefor a moment. - A helper with the same name, sensor and settings as another one is refused, as it is in the options of the helper.
- When Home Assistant refuses the change of one helper, the helpers before it in the list keep their new settings.
- Statistics helpers set up in YAML are refused. Their settings are in your configuration: change them there.
Missing something here?
An action, an entity or a repair this integration should have? Say so. Stuck, or found a bug? There is help.
Generic hygrostat
Spook adds an action to the generic hygrostat helper that changes its dry and wet tolerance, so an automation can widen or narrow them without a trip to the helper's options.
Derivative
Spook adds an action to the derivative sensor that changes its time window, so an automation can smooth it more or less without a trip to the helper's options.
