Integrations

History stats

Spook adds an action to the history stats sensor that changes the period it measures, so an automation can restart the count without a trip to the helper's options.

The history stats helper measures how long something was in a state over a period, or how often it got there: how many hours the game console was on today, how often the back door opened this week, what share of the evening the heat pump ran. The period has a start, an end and a duration, and the sensor takes two of those three: the third follows from them.

The period is set in the helper's options, and only there. An automation cannot change it: not a console time that counts from the moment somebody presses reset, instead of from midnight.

Spook adds an action for that: set period.

Actions

Spook adds one action to the history stats integration. You find it wherever you add an action, marked with a 👻.

Set period

Changes the period one or more history stats sensors measure, like the options of the helper do: when it starts, when it ends, and how long it lasts. What you leave out stays as it is: the entity it watches, the states it counts, what it measures, and the minimum state duration of its additional settings.

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 sensor, as it does after you save its options. The action returns once the sensor is reloaded, so the next step of your automation already sees the new period. A sensor that already has the period you give is left alone, without a reload.

  • Start: when the period starts, as a template.
  • End: when the period ends, as a template.
  • Duration: how long the period lasts.

A sensor has exactly two of the three. To take one away, give it null in YAML: end: null with a duration makes a sensor of a period that starts at its start and lasts that long. A period with all three, or with only one, is refused, with an error that says how many it would have.

Templates in an automation are rendered first. Your automation or script renders every template in the data of an action before the action sees it. So start: "{{ now() }}" does not reach the sensor as a template: it arrives as the time it rendered to, like 2026-12-03 21:15:00+01:00, and the sensor counts from that moment on, for good. That is often exactly what you want, like when a button resets a count. When the sensor has to keep the template, so it moves along by itself the way {{ today_at() }} moves to the next midnight, write it inside raw tags: start: "{% raw %}{{ today_at() }}{% endraw %}". Your automation then hands the template over as written.

The options of the helper check that a template is a template, not what it renders to. Spook does the same. A start or an end that does not render to a time, like soon, is taken, and the sensor then cannot set up: it stays unavailable, and Home Assistant keeps trying.

A sensor with the same name, entity and settings as another history stats sensor is refused by Home Assistant, the same as when you make it twice. The error says so, and names the sensor. It stays as it was.

Every entity is checked before the first sensor changes. An entity that is not a history stats sensor, one set up in YAML, or one that would end up without exactly two of start, end and duration, is refused with an error, and nothing changes. The sensors change one at a time, in the order you give them. When Home Assistant refuses the new period of one, like when it matches another sensor, the action stops with an error that names that sensor. The sensors 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 period, and select Add action.
  2. Search for set period, and select History stats: Set period 👻.
  3. Choose the history stats sensors.
  4. Turn on the parts of the period you want to change, and fill them in.
  5. Select Save.

To take a part of the period 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.