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.
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…
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:
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)?
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.
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.
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 %}
@Troon I’m wondering where should this be added in 2k25
I tried adding it to the configuration.yaml file, restarted home assistant but I still get unavailable values.
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 %}
You will at first, as it has to “bootstrap” itself with a valid value from your source sensor (binary_sensor.door_lock_door_lock). After that, this template sensor (binary_sensor.door_lock if you haven’t changed it in the UI) should mirror the valid values out of the source.
To be clear: do you even see this new template sensor? If not, please post your full configuration.yaml here with any private bits redacted.
I can see my sensor changing values in the UI between closed → opened and if I leave it for some time it becomes unavailable.
My configuration.yaml is quite simple. Is it something that I’m missing?
# Loads default set of integrations. Do not remove.
default_config:
template:
- binary_sensor:
- name: Door Lock
device_class: door
state: >
{% if states('binary_sensor.kitchen_contact_sensor') in ('opened', 'closed') %}
{{ states('binary_sensor.kitchen_contact_sensor') }}
{% else %}
{{ this.state }}
{% endif %}
# Load frontend themes from the themes folder
frontend:
themes: !include_dir_merge_named themes
automation: !include automations.yaml
script: !include scripts.yaml
scene: !include scenes.yaml
Ahhh yickes. I got it now. That means I should repeat the template for each of my contact sensors with various different names and use these new ones for automations, UI, etc. Right?