Backfilling energy statistics for dashboard

Somewhat related @frenck has a new custom integration of “things/features, that will never end up in Home Assistant itself”, as part of that there is a service recorder.import_statistics:

The description in the README.md is only “Advanced service to directly inject historical statistics data into the recorder long-term stats database.”

Without installing it to see the dialog from Developer tools → Services, the services.yaml gives some clues about parameters and how to use it:

recorder_import_statistics:
  name: Import statistics 👻
  description: >-
    Import long-term statistics.
  fields:
    statistics_id:
      name: Statistics ID
      description: The statistics ID (entity ID) to import for.
      required: true
      selector:
        entity:
    name:
      name: Name
      description: The name of the statistics.
      required: false
      selector:
        text:
    source:
      name: Source
      description: The source of the statistics data.
      required: true
      selector:
        text:
    unit_of_measurement:
      name: Unit of measurement
      description: The unit of measurement of the statistics.
      required: false
      selector:
        text:
    has_mean:
      name: Has a mean
      description: If the statistics has a mean value.
      required: true
      selector:
        boolean:
    has_sum:
      name: Has a sum
      description: If the statistics has a sum value.
      required: true
      selector:
        boolean:
    stats:
      name: Statistics
      description: >-
        A list of mappings/dictionaries with statistics to import.
        The dictionaries must contain a "start" key with a datetime string
        other valid options are "mean", "sum", "min", "max", "last_reset", and
        "state". All of those are optional and either an integer or a float,
        except for "last_reset" which is a datetime string.
      required: true
      selector:
        object:

The code is here:

The code looks very straightforward/minimal and uses the recorder integration so it should be database agnostic.

There is this community thread for the Spook integration, but so far no clarifying details usage of import statistics:

1 Like