Basics · Energy management
Home Assistant as an energy manager
Installation type, hardware, storage and backup – the foundation the data sits on.
Read the article →Technical article · Home Assistant · Data
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.
state_class). Without one, nothing is left after 10 days. Switches and
binary sensors have none at all.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.
| Tier | Table | Content | Retention |
|---|---|---|---|
| Raw states | states | every state change, exactly as it happened | purge_keep_days, default 10 days |
| Short-term statistics | statistics_short_term | 5-minute summary | same as raw states |
| Long-term statistics | statistics | hourly summary | unlimited |
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 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.
state_class | For | Statistics store |
|---|---|---|
measurement | instantaneous values: power in W, temperature, state of charge | mean, minimum, maximum |
total | meter readings that are never reset or can also decrease, such as a net meter | reading and sum |
total_increasing | meter readings that only rise and occasionally restart from zero | reading 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.
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.
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:
total_increasing, a drop
in the meter reading of more than 10 % counts as a new meter cycle. If a device briefly
reports 0 instead of unavailable after a restart, and then its real reading
again, the statistics add the entire meter reading as consumption. The value can be
corrected in the Statistics tab (“Correct a wrong value, for example, a spike in your energy
use”).kWh
instead of KWh, or a self-made sensor without a device class gets a new unit.
If Home Assistant does not know how the old and new unit relate, it suspends the
statistics until you decide: update the old unit or delete the old statistics.exclude has neither history nor statistics.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.
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:
exclude in the recorder
configuration, by entity, domain or pattern such as sensor.weather_*. What is
never needed never has to be purged.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.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.
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.
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.
| Goal | Enough for it |
|---|---|
| Monthly and yearly consumption, Energy dashboard | long-term statistics, built in |
| Temperature or power history as hourly values over years | long-term statistics, built in |
| Run times of immersion heater, pump, heat pump | History stats helper with a state class |
| Raw values at full resolution for a few weeks | increase purge_keep_days |
| Raw values at full resolution for months or years | InfluxDB |
| Analysis outside Home Assistant, for example with Grafana | InfluxDB |
| A one-off analysis in a spreadsheet | CSV 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.
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.
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.
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.
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.
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.
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.
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.
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.