How to return a NAN value?

Dear all,

I would like to return a NAN value as result of a calculation if the values do not make any sense (in my context).

Example:


binary_sensor:
  - platform: template
    name: "Status Car Presence"
    device_class: presence
    lambda: |-
      if (id(sensor_us).state < 0.20) {
        return NAN
      } else if (id(sensor_us).state > 0.2 and id(sensor_us).state < 1.5) {
        return true;
      } else {
        return false;
      }

US is a ultrasonic distance sensor. If the value is below 0.2, NAN should be return. Unfortunately, the code above does not work there seems to be no difference between return true; and return NAN;.

Any ideas?

Many thanks

You can’t have binary sensor report three stats.
It’s binary, two.

You need to make it a regular sensor

Do the id(sensor_us).state < 20 calculation in an availability sensor to have your sensor be either true or false (on/off) when the value makes sense and to set the sensor to “unavailable” if the value does not make sense.

@ Hellis81

Thanks,

Certainl binary means that only two values are availble (true/false or 0/1). But I thought that a kind of “undefined” or “null” is available / supported as a return value as well to indicate that no valid data (status) is available.

I tried “regular sensor” but this does not work together with the device class “presence”…

Use return unavailable insteed of return NAN.

@Pipo31416:

Actually sounds good, but if I use the following definition

binary_sensor:
  - platform: template
    name: "Status Car Presence"
    device_class: presence
    lambda: |-
      if (id(sensor_us).state < 0.20) {
        return unavailable;
      } else if (id(sensor_us).state > 0.2 and id(sensor_us).state < 1.5) {
        return true;
      } else {
        return false;
      }

I receive the error

esp_garageControl.yaml:84:16: error: 'unavailable' was not declared in this scope
*** [.pioenvs\garagecontrol\src\main.cpp.o] Error 1

@atlflyer

sounds good. But how to change the availability attribute of a sensor?

If I add to directly to the definiton (just give it a try/see the effect) I receive the error that it is only avaialbe for MQTT:

binary_sensor:
  - platform: template
    name: "Status Car Presence"
    id:   "car"
    device_class: presence
    availability: false
    lambda: |-
      if (id(sensor_us).state < 0.20) {
        return false;
      } else if (id(sensor_us).state > 0.2 and id(sensor_us).state < 1.5) {
        return true;
      } else {
        return false;
      }

Error:

Failed config

binary_sensor.template: [source esp_garageControl.yaml:79]
  platform: template
  name: Status Car Presence
  id: car
  device_class: presence

  This option requires component mqtt.
  availability: False
  lambda: |-
    if (id(sensor_us).state < 0.20) {
      return false;
    } else if (id(sensor_us).state > 0.2 and id(sensor_us).state < 1.5) {
      return true;
    } else {
      return false;

Sorry, it’s my bad.
I forgot that’s not work, and it’s the reason I use the python script “set_state” in an automation to set unavailable an entity when their value is out of my needs.

Sorry, that setting only works for HA template sensors, not ESPHome sensors. There’s a discussion here on how to accomplish what you’re looking for, but it seems like perhaps it’s not possible until some new change is made.