Resistance Sensor to Binary Sensor for flood detection: how to treat nan like 0?

My basement flooded this morning from hurricane Helene. This happened years ago and I added Zwave sensors to alert me, but none of them reported anything (even though they report OK). Sigh.

I’m making up some ESPHome sensors instead but hitting a problem.

Basically I have some analog water sensors, and using an ADC sensor feeding a resistance sensor, then watching for an ohm threshold to trigger a binary_sensor.

Problem is that when the sensor is dry, it returns nan (infinity resistance). This basically means the binary will trigger on when the sensor gets wet, but will not turn back off when resistance switches to nan.

How can I get this to work? Seems like I need to somehow tell things to treat nan as 0, but there is probably some other trick.

Would love to have a ESPHome binary sensor but if I cannot get this to work I’ll just do something in HA. Thanks!

My code:

sensor:

  - platform: adc
    id: source_sensor
    pin: A0
    filters:
      - multiply: 3.3

  - platform: resistance
    id: crawlspace_esp_flood_sensor_resistance
    sensor: source_sensor
    configuration: DOWNSTREAM
    resistor: 10.0kOhm
    name: Flood Resistance Sensor


binary_sensor:
  - platform: analog_threshold
    name: "Crawlspace ESP Flood Sensor"
    sensor_id: crawlspace_esp_flood_sensor_resistance
    threshold: 25000
    

Try the template binary sensor instead of analog threshold.

You don’t need that resistance sensor, it’s useless in your case.
Just analog sensor and template binary sensor.