Sensor "nan" value as trigger

Hi,

I’m trying to make a relay turn on if the value of a sensor is “NAN”. I tried multiple things and nothing works.

interval:
  - interval: 5s
    then:
      - script.execute: run_pump

script:
  - id: run_pump
    then: 
       - lambda:
          if (id(sensor_temp).state == NAN) {id(relay).turn_on();} 
          else {id(relay).turn_off();}

Try “NAN”. Havent tried myself as I am noob

No, it gives an error.

           if (id(sensor_temp).state == "NAN") {id(relay).turn_on();}  
                                  ^
/config/esphome/solar-controller.yaml:135:34: error: invalid operands of types 'float' and 'const char [4]' to binary 'operator=='
*** [/data/solar-controller/.pioenvs/solar-controller/src/main.cpp.o] Error 1

i found the solution.

       - lambda:
          if (isnan(id(sensor_temp).state)) {id(relay).turn_on();} 
4 Likes