Issue with some calculations when Sensors will be updated at different times

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

You need to control your updates then.
I think you can set the update frequency to a value in HA or run it manually, but I am not sure if it’s per sensor or globally.

Personally I have moved these things to Node-Red, so I can decide when to do the calculations or if the sensor supports it when to update the values.

thanks.
Doing this in node Red might be an idea - also to shorten the size of my file…
But even in NodeRed I would not know at the top of my head how to check when the calculation would be “valid”… hm… Need to dig a bit deeper into that … it’s not an urgent topic basically, it is working - and since I know the reason of this, I could live with it if it does not work out.

There are several options.
If the sensor updates can be polled, then you can make sure to poll them at the same time.
You can also store the value of one poll and check if the value has changed, which just require a high enough resolution of the sensor to make sure the likelihood of two values after each other are the same will be so low that it would probably never happen.
If the updates are with long enough intervals, then you can also schedule your calculations to be in the middle of the interval to make sure the updates are done and finished.
Sometimes the sensors have a timestamp, which makes it even easier and there are probably more solutions.

1 Like