---
title: "History stats"
description: "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."
canonical_url: "https://spook.boo/history-stats"
---
# 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`.

**Technical details**

| Action properties |
| --- |
| Action | History stats: Set period 👻 |
| Action name | `history_stats.set_period` |
| Action targets | No targets |
| Action response | No |
| Spook's influence | Newly added action |
| Tools | [Try this action](https://my.home-assistant.io/redirect/developer_call_service/?service=history_stats.set_period) [![Open your Home Assistant instance and show the Actions tool with a specific action selected.](https://my.home-assistant.io/badges/developer_call_service.svg)](https://my.home-assistant.io/redirect/developer_call_service/?service=history_stats.set_period) |

| Action data parameters |
| --- |
| Attribute | Type | Required | Default / Example |
| `entity_id` | string or list | Yes | `sensor.console_time` |
| `start` | template or `null` | No | `"{% raw %}{{ today_at() }}{% endraw %}"` |
| `end` | template or `null` | No | `"{% raw %}{{ now() }}{% endraw %}"` |
| `duration` | time period or `null` | No | `"24:00:00"` |

Give at least one of `start`, `end` and `duration`, and leave the sensor with exactly two of them. A start or an end renders to a date and time, or to a timestamp. The duration is a duration like `"24:00:00"`, or days, hours, minutes and seconds like `{"days": 7}`. `null` takes one of the three away.

**Example actions in YAML**

Count from midnight again, as a template the sensor keeps:

```yaml
action: history_stats.set_period
data:
  entity_id: sensor.console_time
  start: "{% raw %}{{ today_at() }}{% endraw %}"
```

The last seven days, up to now:

```yaml
action: history_stats.set_period
data:
  entity_id: sensor.console_time
  start: null
  end: "{% raw %}{{ now() }}{% endraw %}"
  duration:
    days: 7
```

The console time measures how long the game console was on, from a start to `{{ now() }}`. A button on the dashboard resets it: pressing it starts the period at that moment. Here the start template is rendered on purpose, so the sensor keeps counting from the press, until the next one:

```yaml
alias: Reset the console time
triggers:
  - trigger: state
    entity_id: input_button.reset_console_time
    not_from: unavailable
actions:
  - action: history_stats.set_period
    data:
      entity_id: sensor.console_time
      start: "{{ now() }}"
```

The end stays the `{{ now() }}` template the sensor has.

**Known limitations**

- Your automation renders templates before the action sees them. A template the sensor has to keep goes inside `{% raw %}` and `{% endraw %}`.
- Every change reloads the sensor. Do not drive the period from something that changes every few seconds: change it when it matters, like when somebody presses a button.
- A reload reads the period back from the recorder, so the sensor measures the whole new period, not only what came after the change. What the recorder no longer keeps, or leaves out, is not counted.
- While the sensor reloads, its state is `unavailable` for a moment.
- A start or an end that does not render to a time is taken, as it is in the options of the helper. The sensor then stays `unavailable`.
- A sensor with the same name, entity and settings as another one is refused, as it is in the options of the helper.
- When Home Assistant refuses the change of one sensor, the sensors before it in the list keep their new period.
- History stats sensors set up in YAML are refused. Their settings are in your configuration: change them there.


## Sitemap

See the full [sitemap](https://spook.boo/sitemap.md) for all pages.
