Ignore sensor values outside of a certain acceptable range

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
    friendly_name: Pressure Template
    value_template: >-
    {%- if states.sensor.pressione_barometrica.state < 900 > 1000-%}
    {%- else -%}
    {%- endif %}

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


@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 -%}
{%- else -%}
{%- endif -%}



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 -%}
      {%- endif -%}

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


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?

Brilliant! I love it. The only thing is that I’d much prefer if the “else” would just return the last valid reading. Any idea if that would be possible?

ok, but which filter with what attributes gives result as below:

  • if new sensor value is out of given range show last correct value
  • if not just show it


you could use the “variable” component, to always save the last valid value from the sensor, as soon as it’s read. That way you have a ‘saved state’ sort of solution.
I’m sure there are more elegant solutions. Though I can think of none right now ^^.

If you’re new to the variable component, this helped me a lot:

When I start HA, Zwave devices get their initial values from cfg***.xml
My energy usage meter has cumulative values, but when HA starts, HA shows the value from file.
Every time I restart, it shows in dip in cumulative energy usage.

Wolud it be possible to create a filter that simply ignores values that are out of range. The Filter- component does not seem to ignore values but interpolates new values. It would be better to have an option just to drop the wrong values. Not recorded anywhere and not propagating value changes to objects depending on it?

I solved this by assigning template sensor its own value back.
Beware that if you do this you need to define entity_id, otherwise automatic detection will start infinite update loop.

1 Like

Hi @Korl, I’m in the process of try to do something smiler, whoud you be able to share your temolate sensor? I have a sensor where I want it to avoid any “0” or “100” values, as when the sensor get this value it’s usually a mistak and not a real value.

Here’s one of mine adapt it as you need : -

        friendly_name: Heating Sensor
        value_template: >
          {% set val = states('sensor.qubino_qt1_temp') | float %}
          {% if val and val != 0 % and 0.1 < val < 62.6 }
            {{ '%.2f' | format(val) }}
          {% else %}
            {{ states('sensor.s_heat_thermostat_temp') }}
          {% endif %}
        icon_template: "{{ 'mdi:thermometer' }}"
        unit_of_measurement: '°C'
1 Like

having the same issue with a swimming pool thermometer (sonoff th16).
get the odd wild -2031c.
Really struggling with filtering out the rogue values.

temps are between 10 and 38c all year round. Daily variances are between 2-3c max so graphs are smooth but all out of shape with the odd values.

Another example would be appreciated.


great thread, just implemented a template sensor using these examples to split - and + values from a grid power monitoring sensor

I have to bump that thread up again, sorry for that :slight_smile:

I mostlikely have the same issue, but I’m not able to limit the values to a specific range, because the sensor I’m using is serving data to my energy meters, and is a total_increasing one.


As you can see, this is the sensor grabbing data from my smart meter about the power from my photovoltaic system, injected back to the grid.

Due to the hiccups of the sensor, my energy meters for daily, weekly, monthy and so on, gets confused and this is messing up the historical data over the recording period.

Any chance I can filter the sensors value to get it sorted out, when it’s totaly different from the previous one that has been recorded?