Integrations

Statistics

Spook adds an action to the statistics helper that changes which samples it keeps, so an automation can shorten or lengthen its window without a trip to the helper's options.

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

  1. Open the automation or script that should change the sampling, and select Add action.
  2. Search for set sampling, and select Statistics: Set sampling 👻.
  3. Choose the statistics helpers.
  4. Turn on the settings you want to change, and fill in the new values.
  5. 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.

Missing something here?

An action, an entity or a repair this integration should have? Say so. Stuck, or found a bug? There is help.