Binary.sensor to flip state not working

Hello… Got a weird one that I’ve been fighting for the past few days. I have a garage door monitor/controller that I built using a Wemos D1 Mini (irrelevant, but background info) and it’s working great, except for the fact that the reed-switch sensors I’m using are NC (normally-closed), rather than NO (normally-open). So, the switch status from the mqtt sensor comes back as “on” when the door is closed, and “off” when it is NOT closed (open). My problem is that in my hass Glance Card, it highlights the switch when it is “on” (door is closed), rather than when it is “off” (open)–see “Garage Open” entry in example (the garage door is currently closed).
image
Obviously, I want it to highlight the door (and change the icon) when it is open, instead of when it is closed. So I’ve created binary_sensor.garage_door_status (on the far right, in the example), which correctly shows the door closed. But for the life of me, I cannot get it to change to a highlighted door-open icon. Here is my configuration for this sensor:

binary_sensor:
## platform for monitoring garage door open status...
  - platform: template
    sensors:
      garage_door_status:
        friendly_name: Garage Door Status
        device_class: garage_door
        value_template: >-
          {% if is_state("switch.tasmota2", "on") %} "Closed"
          {% else %} "Open"
          {% endif %}
        icon_template: >-
          {% if is_state('binary_sensor.garage_door_status', 'open') %} 
            mdi:garage-alert-variant
          {% else %} 
            mdi:garage-variant
          {% endif %}

From the rather sparse binary_sensor documentation, I’m lead to believe that with the above template implemented, every time the switch.tasmota2 sensor changes state the binary sensor should change, as well as the icon. However, that is not working. Can someone see anything in the code that I’ve missed? I’ve scoured the forums looking for examples, but everything seems to fit. It’s just not working…

I’ve even tried setting automations to take care of the issue as well, and used notifications to help track the flow, as you can see here:

# ***************** Garage Door Status ****************
automation garage_closed:
  alias: Garage door closed state
  trigger:
  - platform: state
    entity_id: switch.tasmota2
    to: "on"
  action:
  - service: notify.notify
    data:
      message: "Garage door is closed. Current bs state: {{ states('binary_sensor.garage_door_status') }}"
  - service: switch.turn_off
    data:
      entity_id: binary_sensor.garage_door_status
#      set: "Closed"
      icon: mdi:garage-alert-variant

automation garage_open:
  alias: Garage door open state
  trigger:
  - platform: state
    entity_id:  switch.tasmota2
    to: "off"
  action:
  - service: notify.notify
    data:
      message: "Garage door is open. Current bs state: {{ states('binary_sensor.garage_door_status') }}"
  - service: switch.turn_on
    data:
      entity_id: binary_sensor.garage_door_status
#      set: "Open"
      icon: mdi:garage-alert-variant

But I only ever receive the closed notifications.

I even tried setting the automation to use the binary sensor, rather than the mqtt switch, but then I get nothing at all. The state of the binary sensor never changes, and that’s the problem. Have tried everything I can think of, including Home Assistant reboot. :frowning: Would greatly appreciate some help here. I’m guessing that I’m either not understanding binary sensors correctly, or I’ve completely missed something in the documentation and forums.

Thanks in advance for any help…

In this example, the garage door is actually open:
image

So you can see that the icon does not change, nor does the status change, for the binary sensor.

        value_template: '{{ not is_state("switch.tasmota2", "on") }}'

EDIT: Also probably:

          {% if is_state('binary_sensor.garage_door_status', 'on') %} 
2 Likes

Dude! You rock! Looks like I was confused about the logic of binary sensors. Took out the automations that I had attempted, cause they’re irrelevant when this is working, and changed my code as suggested. Works like a charm now. Here’s my corrected code, in case this helps anyone else:

  - platform: template
    sensors:
      garage_door_status:
        friendly_name: Garage Door Status
        device_class: garage_door
        value_template: >-
          {{ not is_state("switch.tasmota2", "on") }}
        icon_template: >-
          {% if not is_state("switch.tasmota2", "on") %} mdi:garage-alert-variant
          {% else %} mdi:garage-variant
          {% endif %}

And here’s the sensor showing up exactly as it should (tasmota switch “off” = door open):
image

I’m still not exactly sure why it works this way. Is the , "on" actually required? Or would it work as follows? {{ not is_state("switch.tasmota2") }}

Thanks again!

Have you read Templating?

Your main problem was not using the actual states of the binary sensor. They are always 'on' or 'off'. Depending on the device class they may display with different words, but the sensor itself is always on or off (well, I suppose it’s possible they could be 'unavailable' or 'unknown' sometimes, too, but that’s abnormal.) Also, the value_template needs to render as true or something other than true; i.e., true -> on, false -> off.

I had not read Templating recently, because it didn’t seem as relevant as a few other things. I poured over Template Cover (which has an example using position_template: "{{ states('sensor.garage_door') }}"), Template Binary Sensor, Cover, and everything I could find about Binary Sensors in the forums. Even tried the Discord chat server…but maybe I’m just too impatient, cause I found that to be next to useless… There were several entries in the HA forum that seemed promising, and that’s where I culled many of my ideas, from examples I found there. But just didn’t understand how these entities are set up. Still not sure why they require the … , “on” bit at the end. Seems like it should work like this:
value_template: {{ not is_state("switch.tasmota2") }}
as well (I tried it and it doesn’t). But as long as I know it’s needed, I’ll use it. Hoping this conversation will help someone else who might be struggling with something similar…

That’s good to know. On the Binary Sensor page it says the following (screenshot…I didn’t add the bold highlight), which I thought indicated that any of these “states” were interchangeable.


That is apparently another place where I screwed up. The way this is worded, it appears that we can use any of these states, not just “on” or “off”. Lesson learned.