Energy dashboard bars / costs

Energy dashboard does not work well. My setup:
I use a Shelly pro3em to measure all three phase of grid.

Then I have a Balcony power plant (Balkonkraftwerk) which is putting in Energy but on one phase only. Now the gist:

If we have a little sun, there is energy less than on that phase consumed from the house, that means we use Power from all three phases. But when the sun is strong and powerful shining, then we have returned energy on phase 1, but even if we return energy on phase 1, we still consume energy on phases 2 and 3.

Formula:
For energy, grid consumption: (Power plant is attached to phase a)

{% if states(“sensor.shellypro3em_a0dd6ca1972c_phase_a_active_power”) | float(0) > 0 %}
{{states(“sensor.shellypro3em_a0dd6ca1972c_total_active_energy”) | float(0) | round (4) }}
{% else %}
{{(states(“sensor.shellypro3em_a0dd6ca1972c_phase_b_total_active_energy”) | float(0)) + (states(“sensor.shellypro3em_a0dd6ca1972c_phase_c_total_active_energy”) | float (0)) | round(4) }}
{% endif %}
Now I have created myself a problem: In the energy dashboard we see some large energy returned values.

If these massive value changes negative and positive both are within the hour, then it is just fine. But if it is negative and then a full hour goes by, then the value is displayed as a massive negative stripe. And so is the returned energy value. That corrects itself until the day ends. But I had several cases where the cost value did not. Thats not good.

The reason for this is to be found in template above.

Every time the value of the phase 1 changes from positive to negative, the energy value changes. An example: No sun, Shelly total active energy is lets say 450kWh. And then the sun comes out, the value of phase 1 gets negative and then it states the addition of phase b and c total active energy which is lets say 200kWh. That makes a jump in the statistics which explain these massive jumps in the strips in the energy dashboard.

I did a Feature request to have it implemented in HA for future but as long as it is not implemented, I will have this problem.

Maybe anyone has a hint on how to prevent this bars from doing wrong to the cost part.

I know how to correct such values within the statistics but I don’t like to do this every second day because its just so many values to correct, specially on some clouds days as the sun appears and disappears every 5 minutes or so and that creates a massive amount of statistics energy and costs to correct.

Now I am a new user and so I need to split this pics in several posts. I am doing 1 to 4 in Order:

Here is the stripes and below the picture with the cost that recovered
1

2

3
And now another day with stripes and below the costs that did not recover:

4

Can someone help me with that topic?

Thank you.

A screenshot of the history of your sensors would be more helpful, but you shouldnt need a template sensor. The shelly should be able to add the phases up and report the correct data (Saldierender Zähler).
Put total_active_energy in grid consumption, total_active_returned_energy in return to grid and the Balkonkraftwerk as Solar Production.

And for your feature request: use an utility meter with tarrifs to get the right values.

Thank you for the hints. I tried utility meter but I did not get it to work. Had difficulties with the Tariff field, could not figure it out with google. I just could not figure out how to add a Tariff list. So I tried to work around it.

I found a solution. I just added another riemann left and fed it from this helper in the formula above. Now it works flawless. I think the cost values wont be a problem either because I don’t have these jumps anymore. Since everything is new, it ist just this years first quarter which is messed up. From now on I will have clean values.