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;.
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.
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”…
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.