Integrating a Binary Sensor with MQTT: A Step-by-Step Guide

Hey everyone, I’m excited to share my recent project where I successfully integrated a binary sensor with MQTT in Home Assistant. I wanted to create a reliable door sensor using a Sonoff RF-Bridge and Tasmota, but I ran into some challenges along the way. Let me walk you through my journey and how I overcame them!

First, I set up the RF-Bridge to receive signals from my door sensor. The sensor sends two distinct codes—one for open and one for closed. I configured Mosquitto Broker to handle these messages, but initially, the binary sensor in Home Assistant showed ‘unknown’ state. After some research and tweaking, I realized the issue was with the payload configuration in my configuration.yaml file.

Here’s what worked for me:
yaml
mqtt:
binary_sensor:
- name: “Office Door”
state_topic: “tele/RF-Bridge01/RESULT”
value_template: “{{value_json.RfReceived.Data}}”
payload_on: “3BBA0A”
payload_off: “3BBA0E”
device_class: door
qos: 1

I also made sure to restart Home Assistant several times to ensure the changes took effect. Now, the sensor reliably updates its state based on the door’s position!

If you’re facing similar issues, here are a few troubleshooting tips:

  1. Check MQTT Logs: Ensure the broker is receiving the sensor data. You can use tools like mosquitto_sub to monitor the topic.
  2. Verify Payloads: Make sure the payload_on and payload_off values match exactly what your sensor sends.
  3. Test with Simple Scripts: Use a Python script to simulate sensor data and see if Home Assistant responds correctly.

I’d love to hear about your experiences with MQTT and binary sensors. Did you face any unique challenges? How did you resolve them? Let’s keep the discussion going and help each other out!

Happy automating! :rocket: