Get average of last 7 days


i want to build a script that changes the depth of discharge of my battery.
For that i want to use the average of the energy feed into the grid and charged into the battery over the last 7 days.
I have following sensors available:
Battery Charge Today
Battery Charge Yesterday
Total Battery Charge

Grid Export Today
Meter Grid Export Total

The today sensors are rising during the day and reset on midnight
Total sensors are the same without the difference, that they dont get reset.

How can i get the average from both of the last 7 days, that i can calculate the discharge limit every day?

Use a statistics sensor

But i want only the average of the 7 values at the end of the day
But when i use the normal average it will be alot less because its rising over the day

There are likely several ways to accomplish it.

What I’d do first is create a template sensor with a Time Trigger that captures the value from that sensor at 11:59 PM (or 1 minute before it resets), This new sensor will update once a day with the max value from the current day (or pretty close to)

Then with that sensor recorded the statistics sensor should be able to calculate the average.

Can you give me a short explanation how to do that?
Create a empty sensor with no state, correct?
Then create an automation that triggers every day at midnight.
But what do I have to choose in the automation to copy the value over?

Short on time, may be able to post a full example later.

  1. Create a template sensor
  1. For the template sensor create a Time Trigger that triggers at 23:59:00

  2. For the template sensor State expressions use

{{ states("sensor.the_sensor") }}

Where you replace the_sensor with the name of that sensor that increases and then resets at midnight.

Thank you.
I thought i have to do this with an automation.

So this should work right?

  - trigger:
      - platform: time_pattern
        # This will update every night
        hours: 23
        minutes: 55
      - name: "Überschuss Bat + Netz"
        unit_of_measurement: "kWh"
        state: >
          {{ (states('sensor.solis_battery_charge_today') | float) + 
             (states('sensor.solis_grid_export_today') | float) }}

  - platform: statistics
    name: "Überschuss Bat + Netz 7 Tage Durchschnitt"
    entity_id: sensor.uberschuss_bat_netz
    state_characteristic: average_linear
    sampling_size: 7

And like every day at 00:00 i can calculate the depth of discharge and set it with an automation right?

1 Like