A Home Assistant custom component to import historical sensor data from one entity into another.
Built for migrating sensor data between integrations — for example, when replacing an Ecowitt integration with a different one and you want the new sensors to carry the old sensors' history.
- Sidebar panel with a simple UI: select source/destination pairs, click Import
- Imports both states and long-term statistics (hourly aggregates for energy dashboard / long-term graphs)
- Atomic transactions: either all states are imported or none are — no partial imports that leave gaps
- Idempotent: safe to re-run; a successful import shifts the cutoff so nothing is re-imported, and a failed import is fully rolled back
- Entity filter to quickly find sensors by keyword
- Also available as a service (
merge_sensor_history.import_history) for use in automations or Developer Tools - HACS compatible
- Reads all historical states from the source entity via the recorder API
- Queries the destination entity's oldest existing entry (inside the same DB transaction)
- Imports only source states that are strictly older than that oldest entry — this prevents any overlap or duplication
- Imports long-term statistics (hourly mean/min/max/sum) via the official
async_import_statisticsAPI, which is inherently deduplicated by the database schema - Commits everything in a single transaction — if anything fails, the entire import is rolled back and you can safely retry
| Data | Source | Granularity | Retention |
|---|---|---|---|
| States | states table |
Every state change | Limited by recorder purge (default ~10 days) |
| Statistics | statistics table |
Hourly aggregates | Kept indefinitely |
HA does not regenerate statistics from states retroactively. Both are imported separately to ensure complete history coverage — including long-term statistics from periods whose raw states have already been purged.
- Open HACS in your Home Assistant instance
- Click the three dots in the top right corner, select Custom repositories
- Add
https://github.com/mayerwin/HA-Merge-Sensor-Historyas an Integration - Search for "Merge Sensor History" and install it
- Restart Home Assistant
- Copy the
custom_components/merge_sensor_historyfolder into your Home Assistantconfig/custom_components/directory - Restart Home Assistant
- Go to Settings > Devices & Services > Add Integration
- Search for Merge Sensor History
- Click Submit — this enables the integration and adds the sidebar panel
- Click Merge History in the sidebar
- Select a source entity (the old sensor with historical data)
- Select a destination entity (the new sensor you want the history imported into)
- Use + Add Pair to queue multiple imports at once, or Bulk add pairs to paste a whole list of
source, destinationlines in one go - Use the filter field to narrow down entities by keyword (e.g.,
ecowitt,temperature). Uncheck Same filter for both to filter the source and destination lists separately — handy when only a serial number differs between the old and new sensors (filter source on the old serial, destination on the new one) - If the sensors record the same quantity in different units, enable Adjust imported values under Options. Multiply by covers simple factors (e.g.
1000for kWh → Wh,0.001for Wh → kWh); Custom function takes a math formula ofvfor anything else (e.g.v * 9/5 + 32for °C → °F), with a live preview of sample values. Formulas are parsed as pure math (numbers,+ - * / % ^ ( ),abs round floor ceil sqrt log log10 exp min max pow pi e) and are never executed as code. Every numeric source value (states and statistics) is converted before import, and energy totals are spliced after conversion; for cumulative energy sensors keep the formula linear (a*v + b) so hourly deltas stay correct - Click Preview to see exactly what would be imported (including the per-row debug JSON) without writing anything to the database
- Click Import History
- Review the results — each pair shows how many states and statistics were imported
Energy & cost are separate sensors. In the Energy dashboard, a sensor's consumption and its cost are tracked by two different entities. If you migrate only the energy sensor, the new sensor's past cost will show
0. To bring the cost history across too, add a second pair for the cost sensors (old cost → new cost). The same applies to any other derived sensor (e.g. compensated/return energy).
You can also call the service directly from Developer Tools or automations:
service: merge_sensor_history.import_history
data:
source_entity_id: sensor.ecowitt_outdoor_temperature
destination_entity_id: sensor.outdoor_temperature- Back up your database before importing. The integration writes directly to the recorder database.
- Only data still in the recorder can be imported. States are purged by default after ~10 days. Long-term statistics (hourly) are kept indefinitely.
- After importing, the new history will appear in the History panel. You may need to refresh the page or wait for the next recorder cycle.
- The import is a one-time operation, not a continuous sync. Run it once after setting up your new sensors.
- Cost is a separate sensor from energy. Pair the cost sensors too if you want their history (see the note under Usage) — migrating only the energy sensor leaves past cost at
0. - Energy sensors are stitched into one continuous series automatically. When you import older energy history in front of a destination that already has statistics, the two cumulative
sumseries (old and new) start from different baselines. The integration lifts the destination's running total, its existing and future statistics, by a constant so the imported history and the existing data join seamlessly, using Home Assistant's own statistics-adjustment mechanism. The result is correct hourly, daily and lifetime energy totals, with no spike and no manual first-hour correction. Per-hour and per-day consumption values are unchanged by the lift (it is a constant offset), and the lift is preserved across restarts (Home Assistant re-reads it from the database). The import stays safe to re-run: a second run detects the series is already aligned and does nothing. - Spikes that reappear after a restart are a separate sensor issue, not this integration. Some energy sensors (solar inverters especially) briefly report
0while Home Assistant restarts, for example during a HAOS or core update. Home Assistant then counts the jump from0back up to the real reading as an hour of consumption, which shows as a spike. This happens on every restart, with or without this integration, and no statistics adjustment can prevent it (the realignment above neither causes nor fixes it). The durable fix is at the source: make the sensor reportunavailable(which Home Assistant ignores) instead of0during restarts, usually with a template sensor that has anavailabilitycondition, then point the Energy dashboard at that clean sensor. See the community write-ups on energy dashboard spikes.
- Home Assistant 2024.1.0 or newer
- The source entity must still have history in the recorder database
Use this integration at your own risk. Always create a full backup of your Home Assistant instance (including the database) before using this tool.
This integration manipulates internal Home Assistant recorder data (states and statistics tables) using internal APIs that are not part of Home Assistant's public API surface. These internals may change without notice in future Home Assistant releases, which could cause this integration to malfunction or produce unexpected results. The authors are not responsible for any data loss, corruption, or other issues arising from its use.
MIT — see LICENSE