Energy sensor that reads meter and adds its value before midnight

Hi,
I have been searching for a long time, but I can’t find any similar questions as mine:
I have a energy sensor (bathroom thermostat) that accumulates its energy consumption during the day, but then resets around midnight.
I would like to make a sensor that accumulates this value every night, so that I am able to use Utility Meters to get weekly, monthly and annual metering from this one.
I have made an attempt below, but realize that I need some if/then in there to trigger it for instance at time 23:59:59 every day.

Would some kind souls here be willing to give me a little push in the right direction?

sensor:
  - platform: template
    sensors:
      varmekabel_bad_akkumulert:
        friendly_name: "Akkumulert effektforbruk varmekabler bad"
        unit_of_measurement: "kWh"
        icon_template: mdi:heating-coil
        value_template: "{{ (states('sensor.bad_energy) + sensor.varmekabel_bad_akkumulert }}"

It seems what you are looking is the utility meter functionality in HA… Here is an example from my config:

The entity source for the utility_meter configuration is sensor.lave_vaisselle_kwh with the following characteristics:

state_class: total
source: sensor.lave_vaisselle_w
unit_of_measurement: kWh
device_class: energy
icon: mdi:chart-histogram
friendly_name: lave_vaisselle_kwh

Configuration in utility_meter.yaml (I will have three new entities, giving daily, monthly and yearly consumption of this equipment):

lave_vaisselle_consumption_daily:
    source: sensor.lave_vaisselle_kwh
    name: Lave Vaisselle (kWh/jour)
    cycle: daily 

lave_vaisselle_consumption_monthly:
    source: sensor.lave_vaisselle_kwh
    name: Lave Vaisselle (kWh/mois)
    cycle: monthly 

lave_vaisselle_consumption_yearly:
    source: sensor.lave_vaisselle_kwh
    name: Lave Vaisselle (kWh/an)
    cycle: yearly

Just in case this gives another idea.
I had a similar sensor that reset itself, but I wanted to record the average of the daily maximum values. I ended up creating an input_number.max_daily_charge, then an automation to record the max into this before the reset.

service: input_number.set_value
data:
  value: "{{ states('sensor.battery_charge_meter')|float}}"
target:
  entity_id: input_number.max_daily_charge
1 Like

Thanks for your reply, I have some experience in using those, but the issue here is that the sensor I am reading (for my bathroom heating) accumulates during each day and resets at midnight.
So before I can use it with Utility Meters, I need to make a sensor that accumulates the consumption and adds the value each midnight (before it resets).

Please see the attached screenshot of the History for the sensor I want to accumulate. (Edit: Added better history presentation)

Well, the simple solution was there right before my eyes. Utility Meter seems to do this too. I was taking the advanced route for a simple problem, and for some reason was not listening to the people guiding me in the right direction. Sorry about that. :laughing: