Need Help - Creating a Sensor that averages daily energy consumption over X Days!

Hi there!

I use Tibber prices to “smart” charge my solar battery in winter when I don’t get enough solar power.
So far I used a static value for my home cunsumption (10kWh) which worked reasonably well.
However, we added a Heat Pump to our home and it will soon start using quite a bit of electricity every day once the heating period starts.

Now my idea is this: Instead of using a static value to know how much I need to charge my battery when electricity is cheap, I want to creat a “3 Day Average” from my daily energy consumption and use that value as the new basis for my charging calculation.
I thought this would be easy, but unfortunately it has turned out to be impossible for me…

I have a Sensor (a Utility Meter with dayly reset) that gives me my energy consumption in kWh for each day. Yesterday’s value is stored in the “last_period” attribute. That is as far as I have managed to get so far.
I would like my new Sensor to collect data for a few days (let’s say 3 Days) and always give me the average of those three days. If I try to do that right now, I get a far too low value, because it takes the “last period” and combines it with the incomplete data for today, resulting in a far too low value.

I hope I managed to explain what I am trying to achieve.

Thanks!

One way to do it is with a pair of template sensors:

template:
  - trigger:
      - platform: state
        entity_id: sensor.YOUR_DAILY_UTILITY_METER
        to: 
          - '0.0'
          - '0'
        not_from:
          - unknown
          - unavailable
    sensor:
      - name: Consumption History 3 Days
        state: "OK"
        attributes:
          history: >
            {% set current = this.attributes.get('history', []) %}
            {% set new = [trigger.from_state.state|float(0)] %}
            {{ (new + current)[:3] }}
            
  - sensor:
      - name: Consumption Average 3 Days
        state: "{{ average(state_attr('sensor.consumption_history_3_days', 'history')) }}"
        state_class: measurement
1 Like

Thank you. I will try whether this works!

I added it to my Configuration.yaml and the new Sensors show up, currently with status “Unkown” and “unavailable”.
I will keep them running and see if they populate over the next couple of days or if something is not working.

So…after leaving it running for several days I unfortunately have to report that the Sensor never populated any Data for me. The values always return as “unknown” and “unavailable”.

Unfortunately I don’t really know how to attempt fixing this since I don’t understand what the Sensors above are supposed to ve doing.

Thanks anyway for your help…I will keep looking for a solution.

Edit: I found a partial solution for now using the Average Sensor. The value is not quite as expected for some reason, but pretty close. I will keep monitroing it and see how it behaves.

Here is the code:

- platform: average
    name: 'Average Consumption'
    duration:
     days: 3
    entities:
     - sensor.hausverbrauch_tagesende

That means the trigger never fired. Make sure you have entered the correct value for entity_id in the trigger.

The trigger-based sensor listens when your daily consumption sensor resets to zero, then it adds the value it changed from to a list stored in the history attribute. The last value in the list is dropped and the list is saved.

The state-based sensor just watches that list of three values and provides an average.

Thanks. The entity ID is correct, but it still didn’t fire. I will check it again and see what happens

Hello, here’s another 2 sensor approach by using statistics:

  - platform: statistics
    name: Energy Consumed Last 3 Days
    entity_id: sensor.<consumed_energy>
    state_characteristic: change
    max_age:
      days: 3
    precision: 1
    unique_id: e_consumed_3_days

This sensor provides the delta of the consumed energy from now until 3 days in the past. Not exactly what you were looking for though so we need another sensor:

- trigger:
  - platform: time_pattern
    hours: 0
    minutes: 0
  sensor:
  - name: "Avg Energy Consumption per Day"
    state: "{{ (states('sensor.energy_consumed_last_3_days')|float / 3)|round(1) }}"

This triggered template sensor is updated at midnight and just devides the consumption of the last 3 days by 3.

For testing you can alter the trime pattern or deifne an additional trigger.

1 Like

Thanks. I have managed to get a (more or less) working solution with the “Average Sensor” integration. It isn’t perfect, but appears to give me workable data. I will try your suggestion when I have a little time on my hands and see if it is better or worse than my current approach :+1: