[Utility Meter] Daily sensors are not calculated correctly

Hello everyone,

I have created some sensors to display the generated PV power, the self-consumed PV power, the fed-in PV and the energy drawn from the grid.

my calculated total sensors work correctly.

The Energy Sensors, which have a daily cycle, do strange things…


template:
 - sensor:
   - name: "Energy Netzbezug"
     state: >
        {% if states('sensor.total_energy') == 'unavailable' %}
          {{ states('sensor.total_energy') }}
        {% else %}
          {{ (states('sensor.total_energy')) | float }}
        {% endif %} 
     state_class: 'total_increasing'
     device_class: 'energy'
     unit_of_measurement: 'kWh'


   - name: "Energy Einspeisung"
     state: >
        {% if is_number(states('sensor.energy_export')) %}
          {{ (states('sensor.energy_export')) | float }}
        {% else %}
          None
        {% endif %}
     state_class: 'total_increasing'
     device_class: 'energy'
     unit_of_measurement: 'kWh'

   - name: "Hausverbrauch Strom kwh"
     state: >
        {% if is_number(states('sensor.solaredge_ac_energy_kwh')) and is_number(states('sensor.total_energy')) and is_number(states('sensor.energy_export')) %}
          {{ '%0.1f' | format(states('sensor.solaredge_ac_energy_kwh') | float + 
                              states('sensor.total_energy') | float - 
                              states('sensor.energy_export') | float) }}
        {% else %}
          None
        {% endif %}
     unit_of_measurement: 'kWh'
     device_class: 'energy'
     state_class: 'total_increasing'

   - name: "Eigenverbrauch PV kwh"
     state: >
        {% if is_number(states('sensor.solaredge_ac_energy_kwh'))  and is_number(states('sensor.energy_export')) %}
          {{ '%0.1f' | format(states('sensor.solaredge_ac_energy_kwh') | float - 
                              states('sensor.energy_export') | float) }}
        {% else %}
          None
        {% endif %}
     unit_of_measurement: 'kWh'
     device_class: 'energy'
     state_class: 'total_increasing'
 

image

At 00:01:17 the total sensor shows 17,986 kWh.
Now it is at 17,990 kwh
So the consumption for today is 4kwh.

utility_meter:
(..)
  energy_house_consumption_daily:
    source: sensor.hausverbrauch_strom_kwh
    name: Energy House Consumption Daily
    cycle: daily


However, the sensor from the utility meter shows 7.8 kWh.

Can someone explain this or am I doing something wrong?

I took a closer look:
these are all my sensors:

the first three are read out (Modbus Inverter & ESPhome electricity meter)

  • solaredge (AC Energy kWh) | sensor.solaredge_ac_energy_kwh
    This is the total power generated in kwh that my photovoltaic system has generated so far.
  • total Energy | sensor.total_energy
    This is the amount of total energy that I drew from the grid
  • Energy Export | sensot.energy_export
    This is the amount of total energy I put into the grid

The next two are just converted sensors to use the Energy Dashboard.
template:

 - sensor:
   - name: "Energy Netzbezug"
     state: >
        {% if states('sensor.total_energy') == 'unavailable' %}
          {{ states('sensor.total_energy') }}
        {% else %}
          {{ (states('sensor.total_energy')) | float }}
        {% endif %} 
     state_class: 'total_increasing'
     device_class: 'energy'
     unit_of_measurement: 'kWh'


   - name: "Energy Einspeisung"
     state: >
        {% if is_number(states('sensor.energy_export')) %}
          {{ (states('sensor.energy_export')) | float }}
        {% else %}
          None
        {% endif %}
     state_class: 'total_increasing'
     device_class: 'energy'
     unit_of_measurement: 'kWh'

The next two are calculated.

  • House consumption electricity (Hausverbrauch Strom)
   - name: "Hausverbrauch Strom kwh"
     state: >
        {% if is_number(states('sensor.solaredge_ac_energy_kwh')) and is_number(states('sensor.total_energy')) and is_number(states('sensor.energy_export')) %}
          {{ '%0.1f' | format(states('sensor.solaredge_ac_energy_kwh') | float + 
                              states('sensor.total_energy') | float - 
                              states('sensor.energy_export') | float) }}
        {% else %}
          None
        {% endif %}
     unit_of_measurement: 'kWh'
     device_class: 'energy'
     state_class: 'total_increasing'
  • self-consumption PV (Eigenverbrauch PV)

   - name: "Eigenverbrauch PV kwh"
     state: >
        {% if is_number(states('sensor.solaredge_ac_energy_kwh'))  and is_number(states('sensor.energy_export')) %}
          {{ '%0.1f' | format(states('sensor.solaredge_ac_energy_kwh') | float - 
                              states('sensor.energy_export') | float) }}
        {% else %}
          None
        {% endif %}
     unit_of_measurement: 'kWh'

Basically this seems to work correctly.
Total energy produced (50.7) + energy purchased from grid (7.2) - energy sold to grid (40.0)
=> 17.9 home consumption
Total Energy Generated (50.7) - Energy Sold to Grid (40.0)
=> 10.7 self-consumption PV

The daily sensors look like this:

utility_meter:
  solar_panel_production_daily:
    source: sensor.solaredge_ac_energy_kwh
    name: Solar Panel Production Daily
    cycle: daily
  solar_house_consumption_daily:
    source: sensor.eigenverbrauch_pv_kwh
    name: Solar House Consumption Daily
    cycle: daily 
  energy_house_consumption_daily:
    source: sensor.hausverbrauch_strom_kwh
    name: Energy House Consumption Daily
    cycle: daily
  energy_imported_power_daily:
    source: sensor.energy_netzbezug
    name: Energy Imported Power Daily
    cycle: daily 
  solar_exported_power_daily:
    source: sensor.energy_einspeisung
    name: Solar Exported Power Daily
    cycle: daily

What is striking?

  • solar_panel_production_daily = correct

  • solar_exported_power_daily = correct

  • solar_house_consumption_daily = incorrect it should be 9,x

  • energy_house consumption daily = incorrect, the energy fed into the grid is apparently not deducted here

  • energy_imported_power_daily = correct

I can’t understand the error, why some values ​​in the daily cycle are correct but others are not. The overall values ​​that serve as a basis all seem to be correct?

I found other cases in the forum that have the same problem:

There is also an entry for this in Git.

OK so this dump clears things.

if you look into the source.csv file you will see that some times (154 times to be precise) the next value is smaller then the previous value.

The utility_meter is sensible to these negative changes (because it tracks the changes not the absolute value)

Someone an idea how I can solve this problem? :frowning:

I had similar issues with the calculation of such values.
Usually - I observed this especially in the following cases:

  1. Sensor Solaredge (api response) will be updated aprox every 15 minutes.
  2. other sensors might be updated more frequently.

now, your calculations will be done whenever one of the sensor states will change - so it might be, that one of the sensor has already updated, but the sensor from solaredge not.
This will lead to a slightly wrong value, considered the actual time…

When your Utility Meter is now reading these values, this can also cause wrong calculations in the utility meter…

btw: are you using the solaredge Api integration or the modbus integration?

Hello Chris,

Thanks for your answer.

I use the Solaredge Modbus interface.

I am currently testing the addition to the Utility Meter
net_consumption: true

if I understand the documentation correctly, should small values ​​be processed correctly?

I will monitor the values ​​tomorrow to see if things are better.

ok :slight_smile:

So mabye as an information:
The Sensors from the “binsentu/solaredge-modbus” integration can be used directly in the energy dashboard for quite a while now.
I think at some point last year after the energy dashboard was integrated, this has been fixed by the devs :slight_smile:
So there is no need to create any custom sensor anymore.

Despite of that, I would recommend to add the “availability” option to your sensor - in case of restarting HA or something like that, your sensor could show incorrect data.

I am also not quite sure about your “state” configuration:

If - Total Energy is Unavailable → Use Total Energy.
Else - Show Total Energy as Float.

the configuration should probably something like this:

  - sensor:
    - name: "Energy Netzbezug"
      unique_id: '"Energy_Netzbezug"'
      unit_of_measurement: kWh
      device_class: energy
      state_class: total_increasing
      state: {{ states('sensor.total_energy') }}
      availability: >
          {{ states('sensor.total_energy') not in ['unknown', 'unavailable', 'None'] }}

Hi Chris,

oh little misunderstanding. My Solardedge only measures the generated PV power.

I have to read the fed-in PV power with an ESPhome with D0 sensor, just like the power that I drew from the grid.

I have to convert the values ​​from the ESPhome into Energy Sensors. I already use the sensor from the Solaredge directly.

I created sensors just before 12 hourly to test it with netConsumption:
It looks very good!

you can see how the value regularly jumps back and forth. Without NetConsumption, this will have triggered +0.1 each time.
The value also agrees with the PV self-consumption value from the Energy Dashboard for the last hour :slight_smile:

(the value is so high because my e-car is charging )

Ah okey :slight_smile:
I am using solaredge with a solaredge meter - so I could get imported and exported power directly from the solaredge integration - which would allow similar calculations - but in my case, the sensors would update at the same time…

Anyway - the Solaredge sensors (production) can be used directly in the energy dashboard - so for this, there’s no need to creat a custom sensor anymore.

For Netzbezug & so on, I am still not sure if your “state” is really correct… because in my eyes, the if / else given there seems not to make sense … (but i might be wrong)… :slight_smile:

If the “net_consumption” which you have added does solve your initial issue, then that’s good :slight_smile:

Yes it is now working as it should.
image

“net_consumption” solves the problem if the data also regularly gets smaller or jumps back and forth due to different reading times.

2 Likes