How to not show sensor status Not available or Unavailable

I have a problem with the binary door open sensor. I received a binary sensor through Tuya Local integration. When the magnet touches the sensor, it sends a status for 2 seconds, then turns off. Home Assistant accepts the sensor’s state, but then translates it to Unavailabe. I don’t understand how I can configure a template or sensor so that there are only two values ​​On or Off, and the rest are ignored.
I tried to do this through a template in configuration.yaml but in this case I only have the value Off. Please help me.

My template

binary_sensor:
  - platform: template
    sensors:
      door_status:
        friendly_name: "Door lock"
        value_template: >
          {% if is_state('binary_sensor.door_lock_door_lock', 'on') %}
            Open
          {% else %}
            Closed
          {% endif %}

try this? (warning free handed it)

template:
  - binary_sensor:
      - name: Door Lock
        state: "{{  states('binary_sensor.door_lock_door_lock')  ==  'on'   }}"

note that i’ve converted to the new format.
you could just steal the template and put it in your legacy format… but i’d recommend you convert as eventually the legacy will go away.

I found this option on the Internet. Unfortunately, it doesn’t work the way I want either. It goes into the enabled state and then into the unavailable state. State off no. I want to see only two states without Unavailable, Closed or Open.

Door Lock is turned off, starts as Door lock Door Lock changes state to "Unavailable"
11:54:22 - 15 seconds ago
Door Lock is activated is triggered by Door lock state Door Lock is unlocked
11:54:04 - 33 seconds ago
Door Lock turns off
11:52:48 - 2 minutes ago
Door Lock is turned off, starts as Door lock Door Lock changes state to "Unavailable"
11:49:54 - 5 minutes ago
Door Lock is activated is triggered by Door lock state Door Lock is unlocked
11:49:36 - 5 minutes ago
Door Lock is turned off, starts as Door lock Door Lock changes state to "Unavailable"
11:47:35 - 7 minutes ago

is this happening from the code I gave you? or some other code you found on the Internet? I presume it’s from something else completely as mine never says unavailable… can answer what’s going wrong with this other code you found on the Internet without seeing it…

maybe try the code I gave you?

You need to understand that a binary sensor has two active states: on and off. The “Open” and “Closed” are front-end mappings from being declared as a door. They are not the actual state of the sensor.

That’s one of the reasons why your first template didn’t work: it’s trying to set invalid states.

Try @armedad’s template which should work, or alternatively:

{{ is_state('binary_sensor.door_lock_door_lock', 'on') }}
1 Like

I tried your code. The log that I sent is the log of your code.

Were you reloading your templates during the time you took that log from? Templates will always go to “unavailable” during a restart or template reload.

Other than that, there is no other way a template sensor configured exactly with only the code armedad provided can resolve to “unavailable”. What does it show in the history (not the logbook)?

If you want to show “Open” and “Closed” in the front end, give you sensor a device class.

template:
  - binary_sensor:
      - name: Door Lock
        device_class: door
        state: "{{ is_state('binary_sensor.door_lock_door_lock', 'on') }}"

That looks like the log from an automation called “Door Lock is activated”. Please post the YAML of that automation.

So this is log from the original door sensor

May 10, 2024
Changes the status to "Unavailable"
10:49:06 - 10 seconds ago
Open
10:48:48 - 28 seconds ago
Changes the status to "Unavailable"
10:48:20 - 1 minute ago
Closed
10:48:02 - 1 minute ago
Changes the status to "Unavailable"
10:44:36 - 5 minutes ago
Open
10:44:33 - 5 minutes ago
Changes the status to "Unavailable"
10:40:29 - 9 minutes ago
Open
10:40:11 - 9 minutes ago

this is log from the my new template, with your code, binary sensor.

May 10, 2024
Closed starts as Door Lock status changes to "Unavailable"
10:49:06 - 2 minutes ago
Open is triggered by Door Lock Open state
10:48:48 - 2 minutes ago
Closed starts as Door Lock status changes to "Unavailable"
10:44:36 - 6 minutes ago
Open is triggered by Door Lock Open state
10:44:33 - 6 minutes ago
Closed starts as Door Lock status changes to "Unavailable"
10:40:29 - 11 minutes ago
Open is triggered by Door Lock Open state
10:40:11 - 11 minutes ago

this is not a automation. Last state the door is open, template sensor say me about that, but then he say door unavailable and closed. I want to see only open (on) or closed (off) state. In some programming language I would do this. I would take a variable, write the current state of the sensor there, then check it, if it is true or elif false, then show it in the state, other state ignore.

Oh, I think I see what you mean. Try this:

template:
  - binary_sensor:
      - name: Door Lock
        device_class: door
        state: >
          {% if states('binary_sensor.door_lock_door_lock') in ('on', 'off') %}
            {{ states('binary_sensor.door_lock_door_lock') }}
          {% else %}
            {{ this.state }}
          {% endif %}

That should retain the on or off state until a new on or off is received, ignoring any unavailable states in the meantime.

1 Like

Thank you so much. You are the best. I found similar examples and didn’t understand how it should work because in else I wanted to write my own sensor. Please tell me what this.state means? This is a form that does nothing, if I need to skip an action, can I use this? Why i can’t use this script without else?

The template needs to output a new value. The this variable is the state object of this sensor — so the template outputs the new value if it is on or off; otherwise it outputs the existing state value again.

What if it’s a sensor instead of a binary sensor?

For example, I have a script in proxmox that sends the temperature to HA, but the sensor does not exist at startup.
I would like the sensors to exist at startup with the last temperature

EDIT

It kind of worked in the developer tools.
I’ll try:

          {% if states('sensor.ryzen_7_5700u') not in ['unknown', 'unavailable','none'] %}
            {{ states('sensor.ryzen_7_5700u') }}
          {% else %}
            {{ this.state }}
          {% endif %}