Filter out 'unknown' sensor values in ESPHome itself

Hi,

I’m reading temperatures using an assortment of sensors (DHT/AM/Dallas). One of those sensors has an HA automation associated with it. That sensor has the tendency to spit out an unknown value every once in a while. If it does this when the automation requests the value, the automation fails. Of course this can be fixed by tweaking some things within home assistant itself but I’d much rather want ESPHome to ignore unknown values. I’ve tried some things using filters (mainly clamp) but that doesn’t work. Anyone know if it’s possible to ignore unknown values within ESPHome? Would also be handy for sensors that need some time to deliver values after booting (like some particle sensors). Ideally ESPHome should report the last ‘known’ value.

Below is the esphome code for the sensor:

sensor:
  - platform: dht
    pin: 21
    model: am2302
    temperature:
      name: "Temperature"
      filters:
      - offset: -2.0
      - clamp:
          min_value: -10
          max_value: 50
          ignore_out_of_range: true
    humidity:
      name: "Humidity"
    update_interval: 60s

Here’s some of the values reported in HA:

sensor.esp_test_temperature,13.8,2024-10-30T06:38:25.580Z
sensor.esp_test_temperature,13.7,2024-10-30T06:39:25.573Z
sensor.esp_test_temperature,unknown,2024-10-30T06:40:25.573Z
sensor.esp_test_temperature,13.7,2024-10-30T06:41:25.579Z
sensor.esp_test_temperature,13.8,2024-10-30T06:43:25.570Z
sensor.esp_test_temperature,13.7,2024-10-30T06:52:25.563Z
sensor.esp_test_temperature,13.8,2024-10-30T06:53:25.567Z
sensor.esp_test_temperature,13.7,2024-10-30T06:54:25.569Z
sensor.esp_test_temperature,13.8,2024-10-30T06:55:25.580Z
sensor.esp_test_temperature,13.7,2024-10-30T07:01:25.558Z
sensor.esp_test_temperature,13.8,2024-10-30T07:02:25.561Z
sensor.esp_test_temperature,13.7,2024-10-30T07:03:25.566Z
sensor.esp_test_temperature,13.8,2024-10-30T07:04:25.558Z
sensor.esp_test_temperature,unknown,2024-10-30T07:25:25.536Z
sensor.esp_test_temperature,13.8,2024-10-30T07:26:25.540Z
sensor.esp_test_temperature,13.9,2024-10-30T07:52:25.520Z
sensor.esp_test_temperature,13.8,2024-10-30T07:53:25.517Z
sensor.esp_test_temperature,13.9,2024-10-30T07:54:25.518Z
sensor.esp_test_temperature,13.8,2024-10-30T07:55:25.520Z
sensor.esp_test_temperature,13.9,2024-10-30T07:57:25.515Z
sensor.esp_test_temperature,13.8,2024-10-30T08:01:25.511Z
sensor.esp_test_temperature,13.9,2024-10-30T08:02:25.516Z

Try this one:

  filters:
    - filter_out: NAN

Thanks for thinking along…
I think I read a comment by someone on reddit who tried that without success but I’ll try it myself and see what happens…

OK, that filter_out solution absolutely worked! :smiley: Haven’t seen a single unknown value since. Thanks for that! Gonna try and find that reddit post now and share my experience.