Calculated power usage wrong, because of data age

Ever since I have PV panels on my house I lost insight into our actual power usage during the time our PV panels produce energy. To overcome that I decided to create a template sensor with the following template:

{{((( states.sensor.solaredge_local_current_power.state | float ) - (states.sensor.power_production.state | float * 1000)) + (states.sensor.power_consumption.state | float * 1000 )) | round(0)}}

In which solaredge_local is the local reading of my Solaredge inverter and the power_production and power_consumption are local readings of my smartmeter.
This works like a charm for most of the day but sometimes I get negative values for power usage followed by a unusual high spike.
I think this is due to the age of the data of two sources. After all, the solaredge_local and my smartmeter readings are from different sources which are not retrieved at exactly the same time.
The solaredge data is updated every 30 seconds, while my smartmeter updates approximately every 10 seconds.
Does any of you smart guys have a good solution for this problem?

For the data spikes themselves, the filter sensor should help:

You’re able to decrease the scan interval by adding scan_interval: 10 to the sensor configuration.

Thanks a lot. Would you advise me to add this to the template sensor only or to the both specific entities the template is using to calculate the actual usage?

I thought a template sensor get’s updated when the sensor data changes. If the template uses 2 different entities the template is updated when either of the entity is changed.

So I would suggest to add the scan_interval on the source. I think what would be sufficient.

I have added this to my config, but unfortunately it still gives me spikes :frowning:
I have no idea how to debug this from this point.

I’m not using the power outputs of my smart meter because of the same problems you’re having. I went for the total kWh values from both the smart meter as the solar_edge.

I’ve made the following calculation to determine the internal (house) usage (all in kWh):

(solaredge_lifetime_energy - total_exported_powerproduction) + power_consumption

total_exported and power_consumption are build using the following.

total_exported_powerproduction = power_production_low + _normal

I’m using EmonCMS to log all this btw. I’ve still some gaps in the data:

Screenshot-20190823084058-442x363

It’s always in the morning between 5 and 7, and in the afternoon between 16:00 and 18:00. Not sure what to make of this.