Need help with MQTT sensor

Hi everyone
I have BLE tracker gateway (not ESPHome) that sends all detected devices to MQTT in JSON format.
This is an example that I am currently working with:

{
    "v": 1,
    "mid": 23739,
    "time": 1629005825,
    "ip": "192.168.1.118",
    "mac": "94B97E56838C",
    "devices": [
        [
            2,
            "73A84CAA68EA",
            -73,
            "1AFF4C000215B722B840712C40779CB70AEE0E06900000640001C5"
        ],
        [
            2,
            "7C26FFE8A115",
            -71,
            "1AFF4C0002157A527466E3C943E7B0A8E8654AD2C0AC00640001C5"
        ]
    ]
}

The long string UUID of my HA beacon that the phone sends (or any beacon for that matter).
How can I match it to a device that is tracked (each one of my 3 phones in the house)?
Just have no idea how to work with this sort of data. BLE tracker device sends all the devices it detected at that moment. So could be either one or many.

The long string is the actual data sent by the BLE device. It’s not relevant for you.
The short string “7…” is the MAC address is the device. This is the string to match to your devices.

One way you could achieve your goal is maybe to use the device_tracker.see service

You could, in an atomation, do the matching between an existing device and the MAC received on MQTTT and call the service.

Just thinking out loud, though…

you are not correct
the mac address sent here is the mac of the tracker device. The long string includes uuid of my device plus other info
I can just try matching the known uuid to see if its in the string. But first need to extract it somehow from the info received in mqtt

That definitely looks like a MAC to me, but I could be wrong, ofc.
Doesn’t your “BLE tracker gateway” have documentation?

ah, yes, sorry. Should have paid more attention
Phone MAC is dynamic, so useless
The long string is iBeacon string.
Here I am asking more about how to get the json info from mqtt to sensor. So that I just get a sensor with one or multiple “long” strings that I can then match to the HA companion beacons

ah, yes, sorry. Should have paid more attention
Phone MAC is dynamic, so useless
The long string is iBeacon string.
Here I am asking more about how to get the json info from mqtt to sensor. So that I just get a sensor with one or multiple “long” strings that I can then match to the HA companion beacons
Like sensor is called “detected devices” and a list of all device strings that the device sent to mqtt

I am doing all this because companion app sends uuid in the wrong format for ESPHome to parse it correctly. Plus I already have this tracker =)

It’s very difficult to assess what you know and don’t know and where you are stuck.

At the end of the day, you need a

this is exactly what I need help with
I did read the page but have difficulty creating a configuration that will extract just the info that I need

I tried:

  - platform: mqtt
    name: "BLEGATEWAY"
    state_topic: "BLETRACKER"
    value_template: "{{ value_json.time }}"
    json_attributes_topic: "BLETRACKER"
    json_attributes_template: "{{ value_json.devices }}"

But get nothing in the attributes side

Will be easier if you tell what the sensor should look like, i.e. what state and attributes it should have based on the json you showed.

state will be “time”, as what you see in the first post is what I have to work with
Attributes would be “devices”, but I do not know how to make them to be separate from each other if they are sent in an array. Sometimes tracker may pickup just one device but if other devices are nearby, it will send them all at once in an array

The end goal is to use this info to match the UUID of the Home Assistant companion app and mark device as “home” when detected.

This template will extract the long string of the first device from the above value:

{{value_json.devices[0][3] }}

you will only need to change the first index value to get the device you want ([0]->[1], [2],[3], etc)

If as you say the payload is always changing you will need to get more creative on which index value to use depending on the use case.

This will create an attribute per device
The “device_count” is an excuse to not have to deal with the final comma of the list of devices (not valid json)

- platform: mqtt
  name: "BLEGATEWAY"
  state_topic: "BLETRACKER"
  value_template: "{{ value_json.time }}"
  json_attributes_topic: "BLETRACKER"
  json_attributes_template: >-
    {
      {% for dev in value_json["devices"] %}
        "device_{{ loop.index }}": "{{ dev[3] }}",
      {% endfor %}
      "device_count": "{{ value_json["devices"]|length }}"
    }
device_1: 1AFF4C000215B722B840712C40779CB70AEE0E06900000640001C5
device_2: 1AFF4C0002157A527466E3C943E7B0A8E8654AD2C0AC00640001C5
device_count: '2'

Thank you! Works great! Adding it to my notes on how to execute code