DIY BLE door sensor + MQTT

Hi.

I have made a diy BLE sensor that communicates through MQTT.
I can get the sensor showing as entity with value “1” or “0” just fine.

I tried creating a binary sensor with “device_class: door”, but it didn’t update and always showed as “closed”.

This is my sensor configuration for now, that is working :

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

  ## 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 works, but doesn’t change colour or show door icon as open or closed. Any pointers on how to get the binary_sensor device_class working?

This was my failed config

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 -%}
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 value == "1") %}
      'ON'
      {% else %}
      'OFF'
      {% endif %}

It will show in the inferface as ‘Open’ and ‘Closed’
Provided the mqtt returns 1 or 0

When subscribing through terminal ssh the mqtt returns “1” and “0”
I added your suggestion, but it returns the following ;

Invalid config for [binary_sensor.mqtt]: invalid template (TemplateSyntaxError: unexpected ‘)’) for dictionary value @ data[‘value_template’]. Got ‘{% if value == “1”) %} 'ON' {% else %} 'OFF' {% endif %}’. (See ?, line ?).

Forgot to remove a )

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 value == "1" %}
      'ON'
      {% else %}
      'OFF'
      {% endif %}

Hah, you’re right. I didn’t see it there, thanks.

No error in config now, but still not working as it should.
I subscribe to both the “wake up” topic and “regular update” topic, which returns “0”. So it should be working, but it shows as “unavailable” in lovelace.

If you open the door, does it change ?

In Home Assistant, no.
In Terminal SSH topic subscription, yes.

And this :

binary_sensor:
  - platform: mqtt
    name: “main_entrance"
    state_topic: "/yyy/esp2/jjjjj/mag"
    device_class: door
    payload_on: "1"
    payload_off: "0"


Are you sure about the topic ? “/yyy/esp2/jjjjj/mag” and not “yyy/esp2/jjjjj/mag” ?

1 Like

The topic was correct and the config you proposed is working.
Thank. You.
I’ve spent the better part of a day trying to solve that.

I tried adding it to “sensors.yaml” which didn’t work, but under “configuration.yaml” it’s working.
Maybe it need to be a “sensor” and not “binary_sensor” to work under sensors.yaml ?

You can create a binary_sensors.yaml and include that.

Thank you for all the help!