Help with reversing the logical state of a binary sensor

Noob question here : I have setup a reed switch on my gate so it tells me if it open or closed. Unfortunately, I have set it up in such a way that when the gate is physically closed the switch is “on” or logically “open”. So in HA, my gate being closed is shown as “open”. That is confusing :). I could move the physical location of the switch, unless there is a way to change the state in software ? I think state_text could do this but it does not seem to be supported in customizing entities ?Is there a way to do this ? Thanks !

Can you please supply the yaml you are using?
I expect the common way is to use a template binary sensor to reverse the states.

Here’s an example:

thanks @DeadEnd. I have it this way now, showing the whole section of my config file.

binary_sensor:
  - platform: ring
  - platform: nest
  - platform: aarlo
  - platform: template
    sensors:
      gate_invert:
        value_template: >-
        {{ is_state('binary_sensor.gate_is_closed', 'off') }}
        friendly_name: Gate Status
        device_class: garage_door

I get this error even before I save (row 170 is the is_state. Can’t spot the missing comma myself as I am not familiar with the syntax.
missed comma between flow collection entries at line 170, column 58:
… ry_sensor.gate_is_closed’, ‘off’) }}
Can you help me spot it ? Thanks !

value_template: >- 
{{ is_state('binary_sensor.gate_is_closed', 'off') }}

Just a guess right now, but can you check your indentation?
Looking at the post I linked, the line after the value_template needs to be indented.
Other than that… make sure your binary sensor is named ‘binary_sensor.gate_is_closed’ and that its states are on and off (not true/false, or 1/0, etc.)

value_template: >- 
  {{ is_state('binary_sensor.gate_is_closed', 'off') }}

Yep, you got it, it was the indentation :slight_smile:
Here’s the final working version. Thank you so much !

binary_sensor:
  - platform: template
    sensors:
      gate_invert:
        value_template: >-
          {{ is_state('binary_sensor.gate_is_closed', 'off') }}
          friendly_name: Gate Status
          device_class: garage_door

What kind of device is reporting the state of the reed switch?

In other words, what is the device represented by binary_sensor.gate_is_closed and what is its platform. Is this an MQTT Binary Sensor?

I’m curious to learn why it reports its state opposite to what is used by Home Assistant (On is open, Off is closed).

I have the reed switch connect to an ESP8266 using ESPHome. The device is fine, it is just the way I have it physically installed on the arm of the gate, which in retrospect is the opposite of where I should have placed it, noob mistake. As I said in my OP, I could move the physical position or fix it with software logic, which is what @DeadEnd helped me achieve above.

Then just fix it in ESPHome by using the invert filter.

Then binary_sensor.gate_is_closed will report the state correctly and you won’t need a template binary sensor to invert it.

1 Like

Thank you @123. That is a good and simpler way. Implemented and working.

You’re welcome! Glad to hear you like the solution. You may wish to tag the post with “Solution” so other people can find it easily.