Binary sensors in Home Assistant are sensors which can only ever have one of two states. Their state is either a
0 or a
1. And to make it understandable for us mere mortals: They can either be off (0) or on (1).
Binary sensors came in many shapes and forms. Just think of all the devices in your smart home, many of them will only ever report one of two states to Home Assistant. A light switch will either be turned on or off. A Wi-Fi-connected outlet will either deliver power or not. A contact sensor placed on a door or window will either report it being open or closed. A lock will either be unlocked or locked. I could go on, but I think you get the picture.
Why not use sensors?
Sensors can have any state. So obviously, they could also have the states off and on. So why use binary sensors if you could just replace them with sensors? The answer is rather simple. When Home Assistant sees a binary sensor, it knows that it can only ever have one of two states. It will never report anything other than a 0 or 1. That way, you can easily represent them in your Home Assistant frontend. For example, the Glance Card can tell you in which state a binary sensor is by the colour of its icon.
Having only two states also makes binary sensors easier to use in Home Assistant automations and scripts. With a sensor, you’d have to compare the state to a predefined value. When using binary sensors, you only have to check whether it is off or on.
What are binary sensor device classes?
Binary sensors can be assigned a specific device class. There is a whole list of device classes available in the Home Assistant documentation. A binary sensor can be set to report on whether a window or door is open, whether a lock is locked or unlocked, whether there is movement detected or not, and so on.
Some binary sensors will automatically report the correct device class. For example, my Aqara motion sensors, which are integrated with Home Assistant using Zigbee2MQTT, are automatically set to the motion device class. But not all devices will do this. In that case, you can set the correct device class by customizing the entity. It can also happen that a binary sensor will report the wrong device class. For example, this can happen when you might be using a door contact sensor on a window.
Device classes are used to correctly display your binary sensors on the Home Assistant frontend. Each state from every device class will have its own icon, and it definitely helps in making your Lovelace UI look nicer.
Some binary sensor examples
There are many different types of devices that report as binary sensors. You can also create your own binary sensors using the binary sensor template. Let’s say you have a thermometer but you’re not really interested in the exact temperature, you only want to know whether a room is cold or not cold. You could create a binary sensor based on that thermometer and have it report cold when a certain temperature is reached. Binary sensors I use include many Zigbee devices from the multiple Xiaomi brand names. I have Aqara motion sensors in just about every room, contact sensors on a few doors and windows, and a bunch of cheap Sonoff switches flashed with ESPHome. I also use binary sensor templates for my room occupancy detection.