Skip to content

mayerwin/HA-Merge-Sensor-History

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

33 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

HA Merge Sensor History

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.

Features

  • 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

How it works

  1. Reads all historical states from the source entity via the recorder API
  2. Queries the destination entity's oldest existing entry (inside the same DB transaction)
  3. Imports only source states that are strictly older than that oldest entry — this prevents any overlap or duplication
  4. Imports long-term statistics (hourly mean/min/max/sum) via the official async_import_statistics API, which is inherently deduplicated by the database schema
  5. Commits everything in a single transaction — if anything fails, the entire import is rolled back and you can safely retry

What gets imported

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.

Installation

HACS (recommended)

  1. Open HACS in your Home Assistant instance
  2. Click the three dots in the top right corner, select Custom repositories
  3. Add https://github.com/mayerwin/HA-Merge-Sensor-History as an Integration
  4. Search for "Merge Sensor History" and install it
  5. Restart Home Assistant

Manual

  1. Copy the custom_components/merge_sensor_history folder into your Home Assistant config/custom_components/ directory
  2. Restart Home Assistant

Setup

  1. Go to Settings > Devices & Services > Add Integration
  2. Search for Merge Sensor History
  3. Click Submit — this enables the integration and adds the sidebar panel

Usage

Sidebar panel

  1. Click Merge History in the sidebar
  2. Select a source entity (the old sensor with historical data)
  3. Select a destination entity (the new sensor you want the history imported into)
  4. Use + Add Pair to queue multiple imports at once, or Bulk add pairs to paste a whole list of source, destination lines in one go
  5. 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)
  6. If the sensors record the same quantity in different units, enable Adjust imported values under Options. Multiply by covers simple factors (e.g. 1000 for kWh → Wh, 0.001 for Wh → kWh); Custom function takes a math formula of v for anything else (e.g. v * 9/5 + 32 for °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
  7. Click Preview to see exactly what would be imported (including the per-row debug JSON) without writing anything to the database
  8. Click Import History
  9. 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).

Service call

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

Important notes

  • 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 sum series (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 0 while Home Assistant restarts, for example during a HAOS or core update. Home Assistant then counts the jump from 0 back 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 report unavailable (which Home Assistant ignores) instead of 0 during restarts, usually with a template sensor that has an availability condition, then point the Energy dashboard at that clean sensor. See the community write-ups on energy dashboard spikes.

Requirements

  • Home Assistant 2024.1.0 or newer
  • The source entity must still have history in the recorder database

Disclaimer

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.

License

MIT — see LICENSE

About

Home Assistant custom component to import historical sensor data from one entity into another. Atomic, idempotent, HACS compatible.

Topics

Resources

License

Stars

36 stars

Watchers

1 watching

Forks

Packages

 
 
 

Contributors