Quick Help creating a Binary sensor

Hi All,

I’m trying to create an mqtt binary sensor for some presence detection but I simply cannot understand value templates.

Basically, I have a tasmota device with ibeacon support that detects my bluetooth tracker and then I would like to create a presence sensor with it.

This is the tasmota console:

17:30:23.755 MQT: tele/tasmota_C6EEB4/SENSOR = {"Time":"2022-07-13T17:30:23","IBEACON":{"MAC":"D3F9xxxxxxxx","RSSI":-88,"STATE":"ON","PERSEC":0}}

I then created this binary sensor on my configuration yaml, but the result is always unavailable:

mqtt:
  binary_sensor:
    - name: "Presence  Detection Test"
      state_topic: "tele/tasmota_C6EEB4/SENSOR"
      payload_on: "ON"
      availability:
        - topic: "tele/tasmota_C6EEB4/SENSOR"
          payload_available: "Online"
          payload_not_available: "Offline"
      device_class: presence
      value_template: "{{ value_json['D3F9xxxxx'].state }}"

Any help is much appreciated!

Try:

     value_template: "{{ value_json["IBEACON"]["STATE"] }}"

Thank you! This creates a working sensor! The problem now is that any Bluetooth device will turn the sensor on. I’m trying to add the Mac address to the template but I don’t know how.

You can’t directly use the MAC address as a sensor, you need to demux it somehow, that is extract multiple sensors from a single one.

There are various ways. You can use YAML and an automation, then match the MAC address and change the state of another binary sensor based on the MAC address. You can also do it in Python, which is what I do. There are probably other ways as well, if no one else chimes in with a suggestion I will try some code when I get back to my PC later today.

Try this

      value_template: >
        {% if value_json is defined and value_json.IBEACON.MAC == D3F9xxxxxxxx %}
          {{ value_json.IBEACON.STATE }}
        {% else %}
          {{ states('binary_sensor.Presence_Detection_Test') }}
        {% endif %}   	
1 Like

That will work. You’ll need one per MAC address.

or this

  value_template: >
        {% if value_json is defined and value_json.IBEACON.MAC=="D3F9xxxxxxxxxxx" %}  MAC=="001A22089D95" %}
          {{ value_json.IBEACON.STATE }}
        {% else %}
          {{ states('binary_sensor.Presence_Detection_Test') }}
        {% endif %}   	  

Hmm, strangely that doesn’t work either. Now I have these 2 errors:

Error parsing value: 'dict object' has no attribute 'IBEACON' (value: {"Time":"2022-07-18T17:54:28","Switch1":"ON","Switch2":"OFF","ENERGY":{"TotalStartTime":"2022-06-01T20:50:40","Total":0.263,"Yesterday":0.000,"Today":0.000,"Period":[ 0, 0],"Power":[ 0, 0],"ApparentPower":[ 0, 0],"ReactivePower":[ 0, 0],"Factor":[0.00,0.00],"Voltage": 0,"Current":[0.000,0.000],"BL09XX":{"Temperature":42.1}},"TempUnit":"C"}, template: {% if value_json is defined and value_json.IBEACON.MAC == D3F9xxxxxx %} {{ value_json.IBEACON.STATE }} {% else %} {{ states('binary_sensor.rodri_living_room_presence') }} {% endif %})
Template variable error: 'dict object' has no attribute 'IBEACON' when rendering '{% if value_json is defined and value_json.IBEACON.MAC == D3F9xxxxx %} {{ value_json.IBEACON.STATE }} {% else %} {{ states('binary_sensor.rodri_living_room_presence') }} {% endif %}'

The binary sensor currently looks like this:

mqtt:
  binary_sensor:
    - name: "Rodri Living Room Presence"
      state_topic: "tele/tasmota_C6EEB4/SENSOR"
      payload_on: "ON"
      availability:
        - topic: "tele/tasmota_C6EEB4/SENSOR"
          payload_available: "Online"
          payload_not_available: "Offline"
      device_class: presence
      value_template: >
        {% if value_json is defined and value_json.IBEACON.MAC == D3F9xxxx %}
          {{ value_json.IBEACON.STATE }}
        {% else %}
          {{ states('binary_sensor.rodri_living_room_presence') }}
        {% endif %} 

I searched but I am unable to fix it.

Try the second value template with “D3F9xxxx…” with the " "
It works in Developer Tools Template

Also need to add
payload_on: “ON”
payload_off: “OFF”

That’s it, thank you so much @Spiro and thank you to @zoogara too!

This is great. This allows me to use a sonoff dual r3 as a smart switch but also double as presence detection for the ibeacon we carry around in our keychains. I’m dropping below the final binary sensor in case it helps anyone:

mqtt:
  binary_sensor:
    - name: "Rodri Living Room Presence"
      state_topic: "tele/tasmota_C6EEB4/SENSOR"
      payload_on: "ON"
      payload_off: "OFF"
      device_class: presence
      value_template: >
        {% if value_json is defined and value_json.IBEACON.MAC == "D3F9xxxxx" %}
          {{ value_json.IBEACON.STATE }}
        {% else %}
          {{ states('binary_sensor.rodri_living_room_presence') }}
        {% endif %}