Create a binary sensor that can return 'unknown'

I’d like the following (which works fine) to instead be a binary_sensor where “floor” is off/away and “Car” is on/home and otherwise the sensor would return unknown. But I can’t figure out the syntax that might send me the unknown bit.

  - sensor:
      name: "Car in garage"
      unique_id: jhkjhkjhkjhkj
      state: >
        {% if not is_number(states('sensor.garage_door_vl53l0x_distance')) %}
          Floor
        {% elif states('sensor.garage_door_vl53l0x_distance')|float(0) > 1100
           and states('sensor.garage_door_vl53l0x_distance')|float(0) < 1130 %}
          Car
        {% else %}
          Dunno
        {% endif %}

Thoughts?

Thanks! :slight_smile:

A binary sensor does not fit this situation.

You can’t have one test for on and a completely different test for off.

The state will be on if the first test is true and off if the first test is not true.

If you can work out how to have one test for on/off then you can use any test for an availability template that marks the sensor unavailable if it returns false.

1 Like

Thank you, @tom_l! I was unaware of the availability attribute. Is there a reference for that kind of thing? I did search a bunch in the docs but didn’t see it… ended up finding examples with just general searching.

Ended up with something like this… does it look reasonable?

  - binary_sensor:
      name: "Car in garage 2"
      unique_id: wuyrtwuyrtwu
      availability: >
        {{ is_number(states('sensor.garage_door_vl53l0x_distance'))
           and states('sensor.garage_door_vl53l0x_distance')|int(0) < 1200 }}
      state: >
        {{ states('sensor.garage_door_vl53l0x_distance')|float(0) > 1100
           and states('sensor.garage_door_vl53l0x_distance')|float(0) < 1130 }}

https://www.home-assistant.io/integrations/template/#availability

Ah! I didn’t see the “[all sensor, binary sensor…” section. Thank you!