How to reset the value of a sensor every night?

Hi,
I’m rather new to HA but was able to realize many ideas.
Unfortunately, I’m now struggling with an probably very easy question: How Can I reset the value of an sensor every night?

Some background: I want to calculate the efficieny of my solar panels in the format kWh/kWp.
For this I have created a sensor called “NO_Effizienzmultiplikator” that gives me exactly this value. The example given is just for one of the panels.

    - name: NO Effizienzmultiplikator
      unique_id: NO_Effizienzmultiplikator
      state: >-
        {{ ((states('sensor.no_erzeugung_kwh')|float) / 12.96)|round(2)|float }}
      unit_of_measurement: "kWh/kWp"
      device_class: power_factor
      state_class: measurement

So far so good.

The problem is that sensor.no_erzeugung_kwh was created as a Riemann sum integral (left) helper. Unfortunately it does not start from zero every day but just adds the yield from the next day on top.

Therefore I thought I could use an automation that resets the sensor value every night. But this piece of code doesn’t work.

alias: NO & SO Erzeugungszähler auf Null setzen um Mitternacht.
description: ""
trigger:
  - platform: time
    at: "00:00:00"
condition: []
action:
  sensor:
    name: "NO Erzeugung kWh"
    state: "{{ 0 }}"

As soon as I click the Save button I get the following error message:
Message malformed: Unable to determine action @ data[‘action’][0]

It’s probabaly a stupid coding error? Please advise.

PS: I also tried to store the current value of the helper in a variable at midnight and then to subtract it the next day from the helper value which continues to increase. But here I was also struggling. Didn’t manage it to store the value…

Anyways, thanks for your support in advance.

You have to create side sensors that will give you daily, monthly, hourly data as you like.

utility_meter:
  NO_Effizienzmultiplikator_täglich:
    source: sensor.NO_Effizienzmultiplikator
    cycle: daily

I never used utility_meter with other things than kwh sensors, but give it a try.

Merci beaucoup.
I will try it!

Can you tell if it worked?

sorry, I haven’t seen your question.
No, unfortunately it didn’t work.

It is working, I’m doing that for my fridge
First, the Riemann helper (sorry, no code, defined with the UI

Then, utilities

  frigo_energy_monthly:
    source: sensor.frigo_cr_328_azd_conso
    cron: 0 0 1 * *
  frigo_energy_daily:
    source: sensor.frigo_cr_328_azd_conso
    cron: 0 0 * * *
  frigo_energy_weekly:
    source: sensor.frigo_cr_328_azd_conso
    cron: 0 0 * * 1

I did them with cron, as I got some issues of no reset sometimes with the default daily/weekly/monthly cycles.

And here are some data
Riemann sensor


And utilities

My monitoring plug is disconnected right now, it was making my breaker to off.

1 Like

Hi Olivier,

Thanks for your clear explanation. As a nearly newbie with HA, I am missing your sentence “Then, utilities” just before your code with the cron time.
Which part of HA can I include this? Do I have to create a section “utilities.yaml” and insert the code in there?
Thanks for any light you could help me with.

I did that directly in my configuration.yaml as I don’t have that many

# Track power consumption over day/week/month    
utility_meter:
  frigo_energy_monthly:
    source: sensor.frigo_cr_328_azd_conso
    cron: 0 0 1 * *
  frigo_energy_daily:
    source: sensor.frigo_cr_328_azd_conso
    cron: 0 0 * * *
  frigo_energy_weekly:
    source: sensor.frigo_cr_328_azd_conso
    cron: 0 0 * * 1

If you have a lot, I recommend to use the include directive and create a file (eg.: utility_meters.yaml) to define them.

Like so

utility_meter: !include utility_meters.yaml
1 Like

Thanks a lot for your swift answer. I’ll implement the way you are showing.
What I don’t get is why am I having the 11 kWh vertical front when my swimming pool pump is starting (it is a 1.2 HP)

This data is coming from a Riemann sensor (see below) fed by the instant power delivered to the pump (through a zigbee double current sensor). The Riemann sensor is not reset to zero every day which is causing the data to be collected wrongly.

Screenshot 2024-03-15 at 19.23.46

I sincerely appreciate your help.

Regards from Cannes.

The Riemann sensor is an integral sensor.
It will always increase, so the need for the utility_meter.

To make it simple:

  1. The utility_meter will always take the difference between the last reset and now. So it will be 0 at midnight and increase “slowly” during the day to go back to 0 at midnight again (for a daily)

As an exemple, my fridge_daily

  1. The Riemann sensor is doing the integral under the curve of a instantaneous sensor
    a. The sensor used by the Riemann sensor is supposed to be something going up and back to 0. You can “visualize” it like generating a continuous histogram.
    b. The Riemann sensor will then sum those histograms to give a total of consumption, growing over time, forever. It will never go back to 0.

The sensor used for the Riemann integral

And the Riemann sensor for the same period

Hope that helps (from Liège, Belgium)

1 Like

Very clear now.
Setup done. I will see the result tomorrow.

Many thanks for your help Olivier.