How to create a custom sensor that provides a 'since start of day' value from a 'lifetime value' sensor?

I have an existing sensor (from an integration) that provides an (always increasing) energy value (kWh). I’d like to create a new virtual sensor (maybe a template sensor?) that shows how much has been added to this sensor since the start of ‘today’. Logic something like:

  • At 00:00:00 save the value of the lifetime sensor.
  • Return the current value of the lifetime sensor minus the current saved value.

Is this possible? Any guidance on how to implement this (specifically the saving the value at 00:00:00 so it can be used later)? Or would a different technique be better?

Any guidance gratefully received as I’m still on a steep learning curve regarding HA.

Look at
History stats - Home Assistant (home-assistant.io)

Does not answer all your questions though

@vingerha Sadly that doesn’t provide what I need (though it has some of the necessary characteristics). The type for this is limited to time, ratio and count none of which are what I need (I need a floating point accumulator). Also, it tracks a ‘state’ not a numeric value.

In your configuration YAML, either the main file or a template included file (without the first line):

template:
  - trigger:
      - platform: time
        at: "00:00"
    sensor:
      - name: "Energy at start of day"
        state: "{{ states('sensor.YOUR_SENSOR') }}"
        unit_of_measurement: 'kWh'
        device_class: energy

  - sensor:
      - name: "Energy since start of day"
        state: >
          {{ states('sensor.YOUR_SENSOR')|float(0) - 
             states('sensor.energy_at_start_of_day')|float(0) }}
        unit_of_measurement: 'kWh'
        device_class: energy

My mistake… try the utility meter on daily …or Troon’s

The UM will grow during the day and then reset … I have a few based on my gas or elec meter which are total-increasing

Utility Meter - Home Assistant (home-assistant.io)

@Troon Thanks for that. it looks like it should do what I want but I added the following to my configuration.yaml (I did not already have any ‘template:’ sections):

template:
  - trigger:
      - platform: time
        at: "00:00"
    sensor:
      - name: "Solar export at start of day"
        unique_id: solar_export_at_start_of_day
        value_template: "{{ states('sensor.jenkins_family_solar_export') }}"
        unit_of_measurement: 'kWh'
        device_class: energy

  - sensor:
      - name: "Solar export since start of day"
        unique_id: solar_export_since_start_of_day
        value_template: >
          {{ states('sensor.jenkins_family_solar_export')|float(0) -
             states('sensor.solar_export_at_start_of_day')|float(0) }}
        unit_of_measurement: 'kWh'
        device_class: energy

and when I try to reload the configuration it throws a bunch of errors:

Logger: homeassistant.config
Source: config.py:622
First occurred: 14:52:02 (8 occurrences)
Last logged: 14:52:06

  • Invalid config for ‘template’ at configuration.yaml, line 51: required key ‘state’ not provided Invalid config for ‘template’ at configuration.yaml, line 53: ‘value_template’ is an invalid option for ‘template’, check: sensor->0->value_template
  • Invalid config for ‘template’ at configuration.yaml, line 58: required key ‘state’ not provided Invalid config for ‘template’ at configuration.yaml, line 60: ‘value_template’ is an invalid option for ‘template’, check: sensor->0->value_template

Any ideas please?

Sorry, got mixed up between legacy and modern configuration for template sensors. state not value_template, corrected above.

Be aware that the unique_id doesn’t have any influence on the name or entity ID of the sensor. If you’re providing it to allow for assigning areas etc., I’d recommend using a random UUID via a tool like this.

@vingerha Thank you! Utility Meter (daily) did the trick and is exactly what I needed.

@Troon thanks! I went with Utility Meter since it does exactly what I need ‘out of the box’.