Add support for helper sensor for statistics when no entity available

I would like to use a helper sensor for historical statistics. I use the Duke Energy integration which pulls meter data from Duke Energy.

Because the data can be delayed up to 24-48 hours the integration does not provide an entity. It is an integration which purely provides statistics.

For example, statistic:

duke_energy:electric_<device>_energy_consumption

I would like to create a statistics sensor which provides statistic values from 2 days ago as the sensor. This would allow me to do additional math such as billing data from published price per kilowatt pricing based on my region.

Currently, helper sensors (including history and statistic sensors) require an entity ID.

What else I have tried

I tried using a template sensor but in the documentation, there’s lots of examples for entity states but none that I could find regarding statistics (or date ranges of statistics).

As of core version 2025.6.0 you can use this action in a template sensor and use the response variable to manipulate the data returned:

image

2 Likes

Amazing! I will try it and report back. I am on the latest release.

Reporting back: I was not able to figure this out. I even went to the original pull request on GitHub which introduced this statistic change for the recorder.

I found no way to use this in templating (I’m not very experienced at templating so could be I don’t know where to look).

https://www.home-assistant.io/integrations/recorder/#action-get_statistics

I saw these docs I should have linked them with my reply. I don’t see how the YAML examples provided there relate to template sensors.

For example, I can’t get it to work in Developer Tools > Template using the Template editor. That’s the same syntax template sensors use when I configure them under Settings> Devices > Helpers, Template sensor.

The template editor does not run actions, it only evaluates templates. Do this: Calendar.get_events response variable in developer-tools > template - #7 by Troon

Noting that Developer Tools / Service is now called Developer Tools / Actions

Circling back. I appreciate the cross linking. Ultimately I couldn’t figure out now to do relative times from “now” to previous days in the helper device.

I gave up trying and may try to tackle it again with renewed interest in the future. For now, probably a fine feature request to make this kind of feature more accessible to helper devices.

Side note: am going away for a few weeks so may be slow or no response.

Here is an example that should get the five minute average one day ago for my energy cost sensor:

template:
  - triggers: # can only use actions in triggered template sensors
      - trigger: state
        entity_id: sensor.total_cost_today # trigger on any valid change of cost
        not_to:
          - unknown
          - unavailable
    action: # get yesterday's 5 minute mean for the same time
      - action: recorder.get_statistics
        data:
          start_time: "{{ now() - timedelta(days = 1) }}"
          end_time: "{{ now() + timedelta(minutes = 5, days = -1) }}"
          period: 5minute
          statistic_ids:
            - sensor.total_cost_today
          types:
            - mean
        response_variable: cost_yesterday
    sensor:
      - name: "Yesterday's cost"
        state: "{{ cost_yesterday.statistics['sensor.total_cost_today'][0].mean }}"
        unit_of_measurement: "$"
        availability: "{{ cost_yesterday.statistics['sensor.total_cost_today'][0].mean | is_number }}"

Only tested in the template editor.

Edit: As confirmation I actually use an SQL sensor to get the state data yesterday and this is what is says:

image

One cent off the 5 minute average. That seems reasonable as the cost would be increasing for the rest of the 5 minute period.

Oh nice I will try that; I am traveling now but will try first thing when back. Thanks!