Energy dashboard - high and missing values on energy usage

Hi,

I am seeing really high energy usage values and also missing values in my energy dashboard.

For instance compare what I got on this day

And this is what really happened (reported my Shelly 3 EM)

I am running

Home Assistant 2023.8.2
Supervisor 2023.08.1
Operating System 10.4
Frontend 20230802.0 - latest
MariaDB 10.6.12

My template for grid consumption is the following

      - name: "Energia entrante"           
        unique_id: "sensor.energia_entrante"
        state: '{{ (states("sensor.tablero_fase_a_energy") | float(0) +
                    states("sensor.tablero_fase_b_energy") | float(0)  +
                    states("sensor.tablero_fase_c_energy") | float(0)) | round(0) 
                }}'
        unit_of_measurement: 'kWh'
        device_class: energy    
        state_class: total_increasing

      - name: "Energia saliente"
        unique_id: "sensor.energia_saliente"
        state:  '{{ (states("sensor.tablero_fase_a_energy_returned") | float(0) +
                            states("sensor.tablero_fase_b_energy_returned") | float(0)  +
                            states("sensor.tablero_fase_c_energy_returned") | float(0))  
                            | round(0) 
                        }}'
        unit_of_measurement: 'kWh'
        device_class: energy    
        state_class: total_increasing

and my dashboard configuration as follows:

I already tried starting on an empty database several again but the behavior happens randomly on some days. I also have the same configuration on another HA with SqlLite and I haven’t seen this behavior. Also in my MariaDb configuration this used to work in the past and at some point it started with this behavior and I haven’t changed the database version either.

Any thoughts ?

Thanks
Claudio.

Why is your total energy decreasing?

It should only increase. Are they hourly totals?

That won’t work with the energy dashboard.

Also please don’t post pictures of config text. Post the actual text and format it correctly for the forum. That way we can copy and edit it to help you.

The Shelly 3 EM graph is correct, because it is a daily graph that shows power consumption at each hour, that same values should be shown in the daily energy dashboard if it were working correctly.

Updated the post with the template code instead of the picture

Thanks

Energy per hour is not the correct sensor to feed the energy dashboard.

Yes that is what the energy dashboard displays in the daily chart, but it calculates this from being fed a total energy sensor. It can also display energy per day, week, or month depending on the view selected. All calculated from a total energy sensor, not an hourly one.

@tom_l I am not using an energy per hour sensor, I showed the Shelly 3EM daily graph just to tell what the dashboard should show on that same day but it doesn’t.

My sensor is based on the template I showed in my first post that as you can see is a total energy sensor anyway here I post the graph of it

also if you read my post I have that same configuration working on another HA installation (SqlLite based) for more than 1 year without any issue, so probably this is a bug that has something to do with the target database MariaDb (that for the record was working without issues for months also).

Now I cleaned everything and started from scratch against SqlLite on this installation.

Thoughts?

Thanks again,
Claudio.

Can you show that same graph for around 9pm on August 10th?

Yes, it seems to be messed up for that day, but not sure why, that’s why I am thinking is a database issue.

No ist is because you have no availability template in your template sensors.

The 3 EM device became unavailable for whatever reason.

Your template substituted zero (from the float filter defaults). On return of the sensor the entire total was added to your energy dashboard, because it went from 0 → total.

If the sensor had gone total → unavailable → total everything would have been ok.

So in summary:

Total → 0 → total: add the entire total to the energy dashboard
Total → unavailable → total: continue counting from the previous total.

You can protect against your template returning zero when the source sensors are unavailable with an availability template.

      - name: "Energia entrante"           
        unique_id: "sensor.energia_entrante"
        state: >
         {{ (states("sensor.tablero_fase_a_energy") | float(0) +
             states("sensor.tablero_fase_b_energy") | float(0)  +
             states("sensor.tablero_fase_c_energy") | float(0)) | round(0) 
         }}
        availability: >
          {{ states("sensor.tablero_fase_a_energy") | is_number and
             states("sensor.tablero_fase_b_energy") | is_number and
             states("sensor.tablero_fase_c_energy") | is_number
          }}
        unit_of_measurement: 'kWh'
        device_class: energy    
        state_class: total_increasing

      - name: "Energia saliente"
        unique_id: "sensor.energia_saliente"
        state:  >
          {{ (states("sensor.tablero_fase_a_energy_returned") | float(0) +
              states("sensor.tablero_fase_b_energy_returned") | float(0)  +
              states("sensor.tablero_fase_c_energy_returned") | float(0))  | round(0) 
          }}
        availability:  
          {{ states("sensor.tablero_fase_a_energy_returned") | is_number and
             states("sensor.tablero_fase_b_energy_returned") | is_number and
             states("sensor.tablero_fase_c_energy_returned") | is_number
          }}
        unit_of_measurement: 'kWh'
        device_class: energy    
        state_class: total_increasing

Also you are not using multi line templates correctly. I have corrected them above.

Oh, I didn’t know the existance of the availability template.
I will try your suggestion and let you know.

Thanks!