Technical article · Home Assistant · Data

Long-term data in Home Assistant: what the recorder keeps and what it purges

Look at the history after a year and you find either a clean curve or nothing at all. The difference is rarely the database. It comes down to a single property of the sensor and to which of the three data tiers you actually mean. For most long-term data, Home Assistant needs no additional software.

Raw states
10 days by default (purge_keep_days)
5-minute statistics
as long as the raw states
Hourly values
unlimited, never purged
Requirement
state_class on the sensor
Status
October 2026 · primary sources below, with the date checked
Author
Julian Haag, state-certified electrical engineering technician

The essentials in one minute

What the recorder keeps, and for how long Three bars over the age of the data. Raw values in the states table, every state change, and short-term statistics in statistics_short_term, per 5 minutes, end at purge_keep_days, 10 days by default. Long-term statistics in the statistics table, per hour mean, minimum and maximum or the sum, reach the end of the axis: they are never purged, but only exist for sensors with a state_class. purge_keep_days, default 10 Raw values states each change Short-term stats statistics_short_term per 5 min Long-term stats statistics per hour: mean/min/max or sum only for sensors with a state_class age of data now 10 days never purged
Based on the recorder integration documentation: “The long-term statistics table is never purged.” What is missing from the history after ten days is usually a sensor without a state class, see below.

Three tiers: what the recorder stores

Everything Home Assistant records is written by the Recorder integration into a database. Without further configuration this is an SQLite file named home-assistant_v2.db in the configuration directory. History, Activity (formerly Logbook), dashboard graphs and the Energy dashboard all read from this one file. Inside it, though, the data sits in three tiers, and each is treated differently.

The recorder's three data tiers
TierTableContentRetention
Raw statesstatesevery state change, exactly as it happenedpurge_keep_days, default 10 days
Short-term statisticsstatistics_short_term5-minute summarysame as raw states
Long-term statisticsstatisticshourly summaryunlimited

The documentation puts it without qualification:

“The long-term statistics table is never purged. Because it stores an hourly summary, only 24 entries are created per day.”

What an hourly row contains depends on the kind of sensor:

That determines what is still there after a year and what is not. Last winter's power curve still exists, but as hourly averages. A short load peak survives in the maximum of its hour; exactly when within the hour it happened is gone. For consumption analysis that is almost always enough. For debugging a control loop that runs every second it is not.

Purging runs every night at 04:12 local time (auto_purge). On the second Sunday of each month the recorder then rewrites the file (auto_repack); otherwise it would not shrink despite the purge.

The requirement: a state class

The long-term statistics are not an archive of all entities. They are only created for sensors whose state_class property is set to one of three values. This is where long-term data gets lost in practice, and silently: for the first ten days the history looks perfectly normal because it comes from the raw states. Beyond ten days it takes its data from the long-term statistics, and where there are none, the graph stays empty.

Which state class for what
state_classForStatistics store
measurementinstantaneous values: power in W, temperature, state of chargemean, minimum, maximum
totalmeter readings that are never reset or can also decrease, such as a net meterreading and sum
total_increasingmeter readings that only rise and occasionally restart from zeroreading and sum

You can check this under Settings › Tools › States (in older versions: Developer tools): if the attributes show no state_class, the sensor gets no long-term statistics. If you create sensors yourself, you set it yourself: in ESPHome with the state_class option, and the same for template sensors. The template documentation says so explicitly: to include the sensor in long-term statistics, set this key. For the ESPHome rain gauge, exactly this made the difference between a number on the dashboard and usable precipitation statistics.

The same applies to the Energy dashboard, only more strictly. An energy meter needs device_class: energy and state_class: total or total_increasing, a power sensor device_class: power and measurement. If anything is missing, the sensor does not even appear in the selection list. The dashboard reads exclusively from the long-term statistics.

Switches, contacts and run times

Among entities, only sensors produce statistics. Whether the immersion heater was on in January, how often the circulation pump ran, when the window was open: those are switches and binary sensors, and their history is gone after ten days. To turn them into a long-term quantity, create a History stats helper that counts the run time or the number of activations. For a period that restarts every day, set the state class to total_increasing, and the long-term statistics keep a running sum – just as for an energy meter.

Typical mistakes and where to fix them

The central place for everything that goes wrong with long-term data is Settings › Tools › Statistics. It lists every statistic with its ID, unit, source and, if there is one, the issue. The most common:

Some fixes delete an entity's statistics. The documentation is clear about it: deleted statistics can only be restored from a backup. So make one first.

How large the database gets

The long-term statistics are not the problem: 24 rows per sensor per day, about 8,760 a year. What makes the database grow is the raw states table. Every state change of every entity is a row, and a power meter that reports a new value every second produces more rows than the rest of the house combined.

The current size is shown under Settings › System › Repairs, three-dot menu, System information, section Recorder: “Estimated Database Size (MiB)”. The levers, ordered by effect:

  1. Exclude chatty entities. With exclude in the recorder configuration, by entity, domain or pattern such as sensor.weather_*. What is never needed never has to be purged.
  2. Choose retention deliberately. purge_keep_days applies to all raw states. To keep individual entities for a shorter time, call the recorder.purge_entities action from an automation with its own keep_days.
  3. Write less often. commit_interval defaults to 5 seconds. On an SD card the documentation recommends a higher value, such as 30 seconds. Better still, as described in the article on setting it up as an energy manager: no SD card at all.

A rule that is easily overlooked: the storage must always have at least as much free space as the database is large. Upgrades, repairs and repacks make a copy while they run. A 1.5 GiB database therefore needs at least 1.5 GiB free. If SQLite detects a corrupted file, Home Assistant moves it aside and starts a new one; for that case the documentation gives 2.5 times the database size as free space.

SQLite, MariaDB or PostgreSQL?

Supported are MariaDB from 10.3, MySQL from 8.0, PostgreSQL from 12 and SQLite from 3.40.1. The documentation's recommendation is nevertheless unambiguous: SQLite is the default, the recommended option and the most tested; newer versions of Home Assistant are specifically optimised for it. Anyone choosing something else should be comfortable in the role of database administrator, explicitly including backups of the external database.

On top of that comes one sentence that settles the order of decisions: changing the database may lose your existing history, and migrating data is not supported. So if you want to switch, do it at the start, not after three years of energy data.

My assessment: for energy management in a single-family home there is no reason to move away from SQLite. An argument for MariaDB or PostgreSQL is a database server that is already running and backed up. If you choose MariaDB, check the version: before 10.5.17, 10.6.9, 10.7.5 and 10.8.4 there was a performance regression when querying history and purging.

When InfluxDB pays off

The InfluxDB integration sends all state changes to an external InfluxDB; versions 1.x, 2.x and 3 are supported. The key point in the documentation: it runs in parallel to the Home Assistant database and does not replace it. History and the Energy dashboard keep reading from the recorder.

What you want to keep – and what is enough for it
GoalEnough for it
Monthly and yearly consumption, Energy dashboardlong-term statistics, built in
Temperature or power history as hourly values over yearslong-term statistics, built in
Run times of immersion heater, pump, heat pumpHistory stats helper with a state class
Raw values at full resolution for a few weeksincrease purge_keep_days
Raw values at full resolution for months or yearsInfluxDB
Analysis outside Home Assistant, for example with GrafanaInfluxDB
A one-off analysis in a spreadsheetCSV export from the history (“Download data”)

The price of InfluxDB is a second system that has to be maintained, updated and backed up. Under Home Assistant OS, InfluxDB and Grafana run as apps from the Home Assistant Community Add-ons collection, which makes the setup easy but not the upkeep. Collecting per-second values just because you can mostly collects volume.

Backup

With SQLite the database sits in the configuration directory, which is part of every full backup. The automatic backup settings have a separate item for it, History, described as “Historical data of your sensors, including your energy dashboard”. Deselecting it to make the backup smaller means the long-term data is not backed up.

With an external database, whether MariaDB on another server or InfluxDB, the backup is your job. And for every variant, what the article on setting it up as an energy manager says applies: a backup is only useful if it lives off the device and a restore has been tried once.

Frequently asked questions

How long does Home Assistant keep data?

It depends on the tier. Raw states and 5-minute statistics stay for as long as purge_keep_days in the recorder says, 10 days by default. The hourly long-term statistics are never purged. They are only created for sensors that have a state class, though.

Why does my history show nothing older than 10 days?

For time ranges beyond 10 days, the history takes its data from the long-term statistics. Those only exist for sensors with state_class measurement, total or total_increasing. Without a state class, nothing is left after 10 days. Switches and binary sensors have no long-term statistics at all.

Do I need InfluxDB for long-term data?

Not for hourly values, monthly and yearly consumption or the Energy dashboard; the built-in long-term statistics cover those. InfluxDB pays off if raw values at full resolution should be kept for months or years, or analysed outside Home Assistant. It runs in parallel to the Home Assistant database and does not replace it.

Can I simply increase purge_keep_days?

Yes, raw states are then kept correspondingly longer, and the database grows with them. The documentation requires at least as much free space as the database size at all times, because upgrades and repairs may make a copy.

What happens to the statistics when I rename an entity?

Nothing is lost: when the entity ID changes, the statistic ID is updated automatically. If a device is removed, however, its old statistics remain and can be deleted under Settings, Tools, Statistics.

Is the history kept when I switch from SQLite to MariaDB?

Not automatically. The documentation warns that changing the database may lose your existing history, and migrating data is not supported. The decision therefore belongs at the start, not at the end.

Sources

All statements come from the official documentation of Home Assistant and ESPHome, each with the date it was checked.

Home Assistant changes every month. What counts is the project's current documentation.

Your Energy dashboard shows spikes, gaps or nothing at all – and you want to know whether the data can still be saved? I can take a look at your system – the first conversation is free.