Ignore out of range values

Is there way to ignore received values that out of range? Ideally the range is something that I would define.

An example application for this is with mysensors. in mysensors, nodes need to send an initial value for each sensor to HA so that HA can learn of the existence of each sensor. If, for example, a default value of zero is sent for temperature sensors, then anything else using that value (perhaps in an automation) may act inappropriately if the real temperature is 30. If however, I could define a valid of range of -50 to 50, then I could send an initial value of 51. This value would be ignored (perhaps generating a log entry), but HA will still have learned that the sensor exists. The entity would have an unknown value until a valid value arrives later.

Never used it myself, but filter sensor with range seems like what you are looking for.
filter sensor - range

Yeah, looks like the filter sensor might do the trick. Or you could create a template sensor, which could either provide the upper or lower bound when the value is out of range (like the filter sensor), or it could be ‘unknown’ in that case, like you said. Assuming you’d like the latter:

sensor:
  - platform: template
    sensors:
      my_limited_sensor:
        value_template: >
          {% set x = states('sensor.my_sensor')|float %}
          {% if -50 <= x <= 50 %}
            {{ x }}
          {% else %}
            unknown
          {% endif %}

If the sensor is an integer value, just replace |float with |int.

Thank you! I appreciate that you not only mentioned the method but also provided a clear example. I now feel like I have a new pair of scissors to run with.

2 Likes

I haven’t encountered filter_sensor before. A value_template is a quick solution, but filter_sensor is something I am going to explore. It may be a better long-term solution.

Thanks!

1 Like