Net energy consumed in 15 minute blocks

Hello. Hoping that someone might have some knowledge to help me.

I have a PV system returning excess production to the grid and my energy utility bills me by calculating the net energy consumption in 15 minute blocks. This means that, every 15 minutes, they take the consumed and returned energy, calculate the net amount and use this value as the amount consumed in those 15 minutes. They then bill me monthly by summing all these 15 minute net blocks.

I’m trying to replicate these calculations in HA but I’m having a hard time. The only information I have available is the total consumed and returned energy values as reported by a Shelly EM. I tried to create two quarterly hour utility meters, for consumed and returned energy and then have a template that nets it, but this is not exactly what I need since the template will update with every state change. I would need a way to make the calculation every 15 minutes and only then zero the quarterly hour meters.

Any ideas?

This is a tricky one. My solution (there’s probably a better way):

Create your two quarter-hourly cycle utility meters, lets call them energy_15_minute_in and energy_15_minute_out. The use a triggered template sensor to calculate the net energy just before the end of each quarter hour (just before the utility meters reset) and add it to itself (so it keeps a running total).

template:
  - trigger:
      - platform: time_pattern
        hours: "*"
        minutes: 14
        seconds: 59
      - platform: time_pattern
        hours: "*"
        minutes: 29
        seconds: 59
      - platform: time_pattern
        hours: "*"
        minutes: 44
        seconds: 59
      - platform: time_pattern
        hours: "*"
        minutes: 59
        seconds: 59
    sensor:
      - name: "Total 15 Minute Energy"
        state: >
          {% set net_15 = states('sensor.energy_15_minute_in')|float(0) - states('sensor.energy_15_minute_out')|float(0) %}
          {{ this.state|float(0) + [0, net_15]|max }}
        availability: "{{ states('sensor.energy_15_minute_in')|is_number and states('sensor.energy_15_minute_out')|is_number }}"
        state_class: total_increasing
        device_class: energy

sensor.total_15_minute_energy will keep counting up forever but you can add it to your energy dashboard to keep track of daily/monthly/yearly energy and cost.

1 Like

Thank for your reply and time to think of a solution.

I will try that. I didn’t notice you could trigger template values at specific times like an automation. This opens a whole new world, thanks!

It’s not very elegant but I think it will get the job done. Let me test later and I’ll tag your answer as solution.

Hi Diogo,

I made something like you want (i think!). Please look at here.

Pedro.