How to check if a sensor from home assistant has value "unknown"

Hi,
I would like to check if the status returned by a sensor implemented in home assistant is “unknown”
I’ve the following configuration

  • The sensor is updated through native API
  • The sensor is created in ESPHome using the platform home assistant
sensor:
  - platform: homeassistant
    name: "sleeptime"
    entity_id: sensor.sleeptime 
    id: sleeptime

to check the value I’ve implemented the following code:

    - lambda: |-  
       if(id(sleeptime).state == "unknown")
        {

            id(deep_sleep_23).set_sleep_duration(1000);
        }
       else
        {

          id(deep_sleep_23).set_sleep_duration(id(sleeptime).state*1000);
        }

but during the compiling I get the error shown in the image

I know that the problem is due to the compare because is not possible to compare a float variable with a const char.
Do you know how could check if the status value is different from “unknown”?

I solved with function isnan in this way:

    - lambda: |-  
       if(isnan(id(sleeptime).state))  
        {
          ...
        }
       else
        {
            ....
        }
5 Likes