Jan 14, 2023

How to invert a binary sensor in Zigbee2MQTT

Disclosure: This post contains affiliate links. If you click through and make a purchase, I’ll earn a commission, at no additional cost to you. Read my full disclosure here.

An abstract, digital illustration of two inverted wine glasses.

Recently, I encountered an issue with my self-made chair occupancy sensor. Though the reasons for needing to replace the sensor are not relevant for this article, the problem I faced may be one that you are currently experiencing. The sensor would incorrectly report the chair as unoccupied when I stood up and occupied when I sat. This caused inaccuracies in my occupancy groups in Home Assistant. In this guide, I will show you how to invert a binary sensor in Zigbee2MQTT to address this issue.

To invert a binary sensor in Zigbee2MQTT for it to report correctly to Home Assistant, you will need access to Zigbee2MQTT's configuration.yaml file. While you can do just about everything else using its dashboard, this option setting appear to be possible just yet. You will need access to the dashboard if you want to figure out what the sensor you are using is reporting. As illustrated by the screenshot below, I am dealing with a contact sensor.

Knowing the sensor type, we can dive into the configuration.yaml file and look for it. If you haven't done so yet, you might want to set a friendly name for your sensor using the Zigbee2MQTT dashboard before doing so, as it makes finding the sensor a lot easier. Once found, add the following lines, making sure to replace contact with whatever you are using:

  '0x0015bc001e014be2':
    friendly_name: office/sensor/chair_occupancy
    homeassistant:
      contact:
        payload_on: true
        payload_off: false

And just like that, you have inverted your binary sensor in Zigbee2MQTT. To make the changes effective, you will need to restart the application. If you haven't done so yet, I recommend returning to Home Assistant and making sure that the sensor is also shown correctly.

A screenshot of Home Assistant's settings for an inverted binary sensor.

Leave a comment

Share to...