Ignore sensor values outside of a certain acceptable range

Occasionally I’ll get really off values from my sensors. My temperature sensor sometimes will report some value over 15,000 which is obviously incorrect. I have the same issue with an Aeonlabs Home Energy Monitor sometimes reporting negative values.

As far as I can tell, neither of these issues are home-assistant’s fault, but I’d like to see an attribute set using customize to limit the range of acceptable values(min/max) for a sensor.

For now you could use a template sensor to limit the output.

1 Like

I had the same problem literally yesterday. I wonder what would be a good place to add this. Cap all temperature values in a sensor to a certain range ? Or should it be per platform / device (lots of repetition when it gets to temperature?)

Originally I was thinking per device, but you have a good point that there’d be a lot of repetition. There could be a global max/min temp setting.

What about a min/max per unit of measurement? That’d also fix my other issue with a power sensor if I could set a min/max for w/kwh as well as F/C.

A more complicated solution could be a way to apply a template to all sensors (after their individual templates have been processed) so that someone could write their own logic to do something like this.

I have these problems also from time to to. I think there should be a min/max option for every sensor.
Also contributors should be encouraged to do some basic range checking where it is possible (e.g. % values are usually between 0 and 100). This will make the system much more robust.

I have this problem, too. I have five miflora sensors and all off them report poriodically values at about 5000°C.
I would really like to fix this.

Why not use the statistics sensor to do some averaging? Not ideal, but should smooth out incorrect readings…

Smoothing is not really an option here, because the strange readings are so far off.
Usual values are between 17°C and 22°C and then there is one with 4980°C.
I would really like to just ignore everything below -10°C and + 60°C

2 Likes

There is at least three components that can be used but don’t quite work together currently.

Individual exceptional measurements can be smoothed with statistics and a median of 5 readings would be OK to filter our suddent exceptional changes.

Persisting unlogical input as when my ESIC 433MHz shows temperature below -1000’C in certain conditions (humidity) is another issue. Condition like this could go on longer than reasonable sampling window for smoothing window, for example a couple of hours. Median and statistics then fail for this case.

There should be a way to convert the out of range values to missing or N/A. While value template can be used to change -1000 to 0, this is not OK for statistics.

Using value template to return None currently messes sensor history as HA treats the states discrete after seeing None values.

2 Likes

anyone a solution for this

I also tray to solve this problem, my pressure sensor sometime give wron value. So I need to filter the value.
I try this:

  • platform: template
    sensors:
    mb_template:
    friendly_name: Pressure Template
    value_template: >-
    {%- if states.sensor.pressione_barometrica.state < 900 > 1000-%}
    nan
    {%- else -%}
    states.sensor.pressione_barometrica.state
    {%- endif %}

In this way I want to filter only the values between 900 and 1000. But It doesn’t work…
Any idea?

2 Likes

@mr-varga - I stumbled onto this thread after my humidity sensor was reporting -999 Anyway I was monkeying around today and I believe you need to cast your state to a float. This works for me:

{%- if states.sensor.pws_relative_humidity.state|float >= 0 and states.sensor.pws_relative_humidity.state|float <= 100 -%}
{{(states.sensor.pws_relative_humidity.state)}}
{%- else -%}
nan
{%- endif -%}

Cheers!

5 Likes

Thanks danodemano :smiley:

where exactly would I set this? Directly on the value_template of the sensor? Or do I need an extra sensor to reprocess the original value?

@Benjamin_De_Smet - you create a new template sensor then use that sensor in place of the actual one.

Thanks =).

I ended up getting the same result like this:

  - platform: mqtt  
    state_topic: "mymqtt/sensornode1"  
    name: "SN1 Temperature"  
    unit_of_measurement: "°F"  
    value_template: >-
      {%- if value_json.temperature|float >= 40 and value_json.temperature|float <= 120 -%}
      {{ value_json.temperature | round(1) }}
      {%- else -%}
      nan
      {%- endif -%}
5 Likes

Filter sensor might be exactly what you need. Particulary outlier filter filters out any value outside a range.

4 Likes

its not exactly what you need, as you cannot define a range of permissable values in the outlier filter. Its close to the “range” filter, but range just floors and caps the values, not ignore them. :slight_smile:

This works! However… Isnt it problematic that it returns a nan value? Wouldnt it be better to just return the last “proper value”?

1 Like

Would a filter work?