Inverting or reversing binary sensor display states

There are times when a binary sensor is used in an unconventional application where I would like to reverse open vs closed, wet vs dry, etc. I would be nice to have a switch or checkbox in the entity customization menu or manually through yaml to invert the displayed state.

Depending on the device class, open and closed, wet and dry, clear and detected would be reversed based on the sensor state.

This would eliminate the need to hide the original sensor and create a separate template sensor for this purpose.

Don’t forget to vote for your own request.

THIS. I have a contact sensor on my bathroom door and I always have to remember to reverse it in automations. Thanks for the tip about using a template sensor for this though. :wink:

I believe what Jay is asking for would only affect the Lovelace display of the device class. You would still be stuck with the same reversed states.

Inverting the actual states would be nice. However, if it is more difficult to implement or causes unforeseen negative side-effects, then the display version would still be helpful.

Here is a simple way to invert the state using a template sensor.

binary_sensor:
  - platform: template
    sensors:
        new sensor name:
        friendly_name: 'xxxxx'
        value_template: '{{ is_state("binary_sensor.original_sensor", "off") }}'
        device_class: xxxx
        attribute_templates:
          battery_level: '{{ state_attr("binary_sensor.original_sensor_,"battery_level") }}'
             Remember to grab your attributes from the original sensor if you want them to appear on the new one.
1 Like

Another application for this would be where the status of a relay is on to switch something off, and you are viewing the state of the relay for the state of the device

Relay on = device off, relay off = device on

1 Like

I’m having a hard time getting the battery level to get passed through. Anyone have a thought as to how to handle that? The original sensor ID is: sensor.window_door_sensor_battery_level My code is:

binary_sensor:
  - platform: template
    sensors:
      inverted_sensor:
        value_template: >-
          {{ is_state('binary_sensor.window_door_sensor', 'off') }}
        device_class: door
        friendly_name: Inverted Sensor
        attribute_templates:
          battery_level: '{{ state_attr("sensor.window_door_sensor_", "battery_level") }}'

just create a binary with the inverted return

- name: "vLiveVentClosed"
  unique_id: "binary_sensor.vliveventclosed"
  state: "{{ is_state('unique_id OF YOUR SENSOR', 'off') }}"
1 Like