Hi all,
I’ve noticed a small issue with some of my statistic-calculations which I’ve done in my configuration.
I have created some template-sensors, which should calculate the percentage of my energy export / import / autarcy ratio and so on - but the calculation is causing some invalid values sometimes, when one of the sensors will be updated earlier than the other one.
For example, here’s my energy export ratio:
The basic calculation is simple:
exported_kwh / produced_kwh * 100 = x%
My Template is like this:
solaredge_calculated_energy_export_monthly:
friendly_name: 'SolarEdge Netzeinspeisung (Monta)'
unit_of_measurement: "%"
#state_class: measurement
value_template: >-
{% if is_state('sensor.solaredge_energy_this_month_kwh', '0.0') or is_state('sensor.solaredge_monthly_kwh_export', '0.0') %}
0.0
{% else %}
{% set production = states('sensor.solaredge_energy_this_month_kwh') | float(default=0) %}
{% set export = states('sensor.solaredge_monthly_kwh_export') | float(default=0) %}
{{ ((export / production) * 100) | float(default=0) | round(2) }}
{% endif %}
Now, the sensor was updated at 07:48:13
at this time, the exported_kwh sensor was updated to 0,14 kwh.
The producted_kwh sensor was updated at 07:48:14 - to a value of 0,18 kwh.
until the update, the production_kwh had a value of 0,06 kwh.
this caused the ratio to jump to 247%
Is there a way how I can change the calculation / template to prevent those peaks?
This also happens for other similar calculations - so I cannot really just simply check if production is equal or higher than export…
for example, the Usage-Ratio of my solar installation is similar:
solaredge_calculated_solar_usage_monthly:
friendly_name: 'Nutzungsgrad (Monat)'
unit_of_measurement: "%"
#state_class: measurement
value_template: >-
{% if is_state('sensor.solaredge_energy_this_month_kwh', '0.0') or is_state('sensor.solaredge_monthly_kwh_selfconsumption', '0.0') %}
0.0
{% else %}
{% set production = states('sensor.solaredge_energy_this_month_kwh') | float(default=0) %}
{% set consumption = states('sensor.solaredge_monthly_kwh_selfconsumption') | float(default=0) %}
{{ ((consumption / production) * 100) | float(default=0) | round(2) }}
{% endif %}
Thanks and br,
chris