DIY BLE Door Sensor with MQTT: A Fun Project with Some Challenges

Hey everyone! I wanted to share my experience with a DIY BLE door sensor project I’ve been working on. It’s been a great way to dive into the world of MQTT and home automation, but there have been a few bumps along the way. :rocket:

So, the goal was to create a simple door sensor using a BLE module that communicates via MQTT. I managed to get the sensor working, and it shows up as an entity with “1” or “0” values. But here’s where things got tricky: I tried setting up a binary sensor with device_class: door, but it just wouldn’t update. It always showed as “closed”, which was pretty frustrating. :confused:

Here’s what my initial configuration looked like:
yaml
##################################

Door magnet sensor

##################################

  • platform: mqtt
    name: “main_entrance”
    state_topic: “/yyy/esp2/fu4fjhs79/mag/p”
    ##################################

Door magnet sensor - converting binary to text

##################################

  • platform: template
    sensors:
    my_device:
    friendly_name: “main_entrance_text_output”
    value_template: >-
    {% if is_state(‘sensor.main_entrance’, ‘1’) %}
    Open
    {% else %}
    Closed
    {% endif %}

This setup works, but it doesn’t change the color or show the door icon when the state changes. I really wanted that visual feedback! So, I tried configuring it as a binary_sensor with device_class: door, but it didn’t work. Here’s the failed config:
yaml
binary_sensor:

  • platform: mqtt
    name: “main_entrance”
    state_topic: “/yyy/esp2/jjjjj/mag”
    availability_topic: “/yyy/esp2/jjjjj/mag”
    device_class: door
    value_template: >-
    {%- if is_state(“binary_sensor.main_entrance”, “1”) -%}
    open
    {%- else -%}
    closed
    {%- endif -%}

After some research and trial and error, I realized the issue might be with how the state values were being interpreted. I ended up sticking with the template sensor approach since it’s reliable, even if it doesn’t provide the visual cues I wanted. :person_shrugging::male_sign:

If anyone has tips on getting the binary_sensor device_class to work properly, I’d love to hear them! It would be awesome to have that door icon toggle between open and closed states. :blush:

Overall, it’s been a fun project, and I’m proud of what I’ve accomplished so far. DIY-ing these sensors really helps me understand how MQTT and home automation work under the hood. Happy coding everyone! :computer: