Math operation average returns unreliable results

Hello,

I have two sensors and calculate their average. However, the average time to time returns apparently wrong number. Any idea where the problem could lie?

Code:

livingroom_temperature:
  friendly_name: Living room Temperature
  unit_of_measurement: '°C'
  value_template: "{{((states('sensor.livingroom_valve_temperature')) | float + (states('sensor.livingroom_sensor_temperature')) | float) / 2}}"

Hello I would use the “average sensor” add-on… You can install it using HACS and it is easy to get average values on specified time range…

1 Like

I have tried your template and it is giving me the correct value of average. It could not be an issue with the template.

The template in configuration.yaml is certainly correct. HACS sensor wont make difference. The problem is not the return of the value but sudden accidental wrong return as you can see on the graph. The yellow line drop.
image

It could be an issue with the updating of the sensor values that is used to calculate average and the template sensor itself. can you check what are the parent sensor values at the time of such deviation. Is it nominal?

When a sensor value is unknown filtering that sensor with |float returns 0.

Use an availability template to ensure both your sensors are available.

You will have to scroll up a bit as the current security warning is messing with the link location: Template - Home Assistant

This is the easiest way to do it assuming your sensors never get to 0 degrees:

availability_template: >
  {{ 0 not in [ states('sensor.livingroom_sensor_temperature')|float, states('sensor.livingroom_valve_temperature')|float ] }}

If either of the sensors could report 0 degrees, use this instead:

availability_template: >
  {{ states('sensor.livingroom_sensor_temperature') not in ['unavailable', 'unknown', 'none' ] and states('sensor.livingroom_valve_temperature') not in ['unavailable', 'unknown', 'none' ] }}
1 Like

@sheminasalam the other sensors are visible in the first graph I posted.
@tom_l sounds like the problem and solution! I will try it. Thx.

So I should use availability_template instead of value_template?

No you need both.

value_template to calculate your average.
availability_template to ensure the sensor states used in the value_template are valid.

So firstly I make the virtual availability_template out of the sensor provided by the hw, then I create another layer that calculates the average? So my current sensor never calucalte with zeros?

Just add one of the availability templates I wrote to your existing template sensor. Scroll up to my first post. (I edited my post).

I am glad that you are patient with somebody who makes living out from social sciences instead of programming, is this final solution as you mean it?

      livingroom_temperature:
        friendly_name: Living room Temperature
        unit_of_measurement: '°C'
        value_template: "{{((states('sensor.livingroom_valve_temperature')) | float + (states('sensor.livingroom_sensor_temperature')) | float) / 2}}"
        availability_template: "{{ 0 not in [ states('sensor.livingroom_sensor_temperature')|float, states('sensor.livingroom_valve_temperature')|float ] }}"

Yep. That’s it. Do the same (adjusting the sensor names in the availability template) for your other average sensors.

and add your second row that deals with the problem of both sensors = zero.

THANKS!

You might want to look into why your sensors are becoming unavailable. Could be from restarting HA. Not much you can do about that.

or netatmo integration? Maybe cgtobi is playing with my valves as I provided my home as a testbed, haha, no you are correct. I am playing with it all the time and this can be the cause. If I let the HA for half day alone, the error does not surface

2 Likes

Tom, just a note. It looks that it does not return zeros now, but I have three warnings in the log:

2021-01-19 15:26:08 WARNING (SyncWorker_0) [homeassistant.util.yaml.loader] YAML file /config/configuration.yaml contains duplicate key “availability_template”.

Is there a better syntax to avoid it or I should ignore it?

      livingroom_temperature:
        friendly_name: Living room Temperature
        unit_of_measurement: '°C'
        value_template: "{{((states('sensor.livingroom_valve_temperature')) | float + (states('sensor.livingroom_sensor_temperature')) | float) / 2}}"
        availability_template: "{{ 0 not in [ states('sensor.livingroom_sensor_temperature')|float, states('sensor.livingroom_valve_temperature')|float ] }}"
        availability_template: "{{ states('sensor.livingroom_sensor_temperature') not in ['unavailable', 'unknown', 'none' ] and states('sensor.livingroom_valve_temperature') not in ['unavailable', 'unknown', 'none' ] }}"

You can only have one availability template per sensor. Delete this one:

availability_template: "{{ 0 not in [ states('sensor.livingroom_sensor_temperature')|float, states('sensor.livingroom_valve_temperature')|float ] }}"

The remaining one will cover all situations.

I said:

Not, use this as well.

1 Like