---
title: "Recorder"
description: "Spook adds an action to import long-term statistics into the recorder, and tells you when statistics belong to no entity anymore."
canonical_url: "https://spook.boo/recorder"
---
# Recorder

> Spook adds an action to import long-term statistics into the recorder, and tells you when statistics belong to no entity anymore.

The recorder integration in Home Assistant stores everything that happens in your home. It is the backbone of the history and the logbook. It also keeps long-term statistics, which power the energy dashboard and other graphs you see in Home Assistant.

## Actions

Spook adds one action for the recorder. You find it wherever you add an action, marked with a 👻.

### Import statistics

Imports long-term statistics into the recorder database of Home Assistant.

Your energy history does not start the day you installed Home Assistant. Your energy provider has years of it, often as a CSV file you can download, and a previous system may have its own. This action writes that history into the long-term statistics, so it shows up in the energy dashboard and the statistics graphs.

**How to use it**

1. Open the script that should do the import, and select **Add action**.
2. Search for **import statistics**, and select **Recorder: Import statistics 👻**.
3. Fill in **Statistics ID**, usually the entity ID of the sensor, and **Source**, usually `recorder`.
4. Turn **Has a mean** and **Has a sum** on or off to match the kind of sensor: a meter that counts up has a sum, a temperature has a mean.
5. Optionally, fill in **Name** and **Unit of measurement**.
6. Fill in **Statistics**, the list of values to import, one per hour. This one is easiest in YAML, see the example below.
7. Select **Save**, and run the script once.

Just want to see it work? [Try this action](https://my.home-assistant.io/redirect/developer_call_service/?service=recorder.import_statistics) opens it in your Home Assistant, ready to run once.

> **Warning:** Importing writes straight into your long-term statistics, and there is no undo. Wrong numbers end up in your graphs and the energy dashboard. Try it on a statistic you can afford to lose first.

![The Actions tool with Import statistics 👻 picked and filled in with an example.](https://spook.boo/images/screenshots/en/light/action-recorder-import-statistics.webp)

What **Try this action** opens, filled in with an example.

**Technical details**

| Action properties |
| --- |
| Action | Recorder: Import statistics 👻 |
| Action name | `recorder.import_statistics` |
| Action targets | No targets |
| Action response | No response |
| Spook's influence | Newly added action |
| Tools | [Try this action](https://my.home-assistant.io/redirect/developer_call_service/?service=recorder.import_statistics) [![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=recorder.import_statistics) |

| Action data parameters |
| --- |
| Attribute | Type | Required | Default / Example |
| `has_mean` | boolean | Yes |  |
| `has_sum` | boolean | Yes |  |
| `name` | string | No | `None` |
| `source` | string | Yes |  |
| `statistic_id` | string | Yes |  |
| `unit_of_measurement` | string | No | `None` |
| `stats` | mapping | Yes |  |

| `stats` attribute mapping |
| --- |
| Attribute | Type | Required | Default / Example |
| `start` | datetime string | Yes |  |
| `mean` | float | No |  |
| `min` | float | No |  |
| `max` | float | Yes |  |
| `last_reset` | datetime string | No | `None` |
| `state` | float | No |  |
| `sum` | float | No |  |

More information about the mapping/meaning of fields in long-term statistics can be found on the [Home Assistant data portal](https://data.home-assistant.io/docs/statistics).

**Example actions in YAML**

One hour of energy use for a meter that counts up:

```yaml
action: recorder.import_statistics
data:
  has_mean: false
  has_sum: true
  statistic_id: sensor.some_energy_sensor
  source: recorder
  unit_of_measurement: kWh
  stats:
    - start: "2023-07-03 21:00:00+02:00"
      sum: 123123
```

## Repairs

Spook looks through the recorder for long-term statistics that no entity belongs to anymore, and tells you in the repairs dashboard.

This one Spook can fix for you, straight from the repair. New to repairs? Follow [Fix your first repair](https://spook.boo/fix-your-first-repair).

### Orphaned long-term statistics

The recorder keeps long-term statistics for sensors that no longer exist, usually because a sensor was removed and its history was kept. Not harmful, but they take up database space, and keep showing up in pickers and graphs long after the sensor is gone.

![The Orphaned long-term statistics repair, listing the statistic of a meter that is gone, with the choice to clear it.](https://spook.boo/images/screenshots/en/light/repair-orphaned-statistics.webp)

The repair lists every statistic it would clear, before you choose.

**How to fix it:** Open the repair, and choose **Clear these statistics** to have Spook throw that history away, for good. Or choose **Let me do it myself**, which points you at **Settings** > Tools > **Statistics**. Want to keep them? **Keep them, stop telling me** leaves them alone and stops the repair coming back. The repair disappears by itself once it is fixed.

**How Spook checks this**

The recorder keeps long-term statistics separately from the states it records, and it keeps them by statistic ID, not by entity. So they outlive the entity they were collected for. Spook compares the statistics in the database against the entities that still exist, and raises a repair listing the ones with no entity behind them anymore.

Spook looks again before it clears anything, and only removes what the list you read and that fresh look agree on. Anything that has an entity behind it again by the time you press the button is left where it is, without being named. The repair comes back for whatever is still orphaned. If none of them still need clearing, Spook says so.

A statistic has to keep looking abandoned for a quarter of an hour before Spook mentions it at all. A sensor can be gone for a moment, when an integration reloads or while Home Assistant is still starting. That is no reason to delete its history.

## Use cases

What people use this for:

- Import data into the recorder yourself. For example, history from a previous system, or the CSV file with your energy usage from your energy provider.


## Sitemap

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