ESP Home BLE Tracking

HI, I have been trying to get BLE tracking working, I get it working but the default 300 second
delay makes it pretty useless, when I try to change this to 10 seconds the ESP Home editor gives an error on the scan interval line. What Am I doing wrong?

Thanks

captive_portal:

# Enable logging
logger:

# Enable Home Assistant API
api:

ota:
  port: 8266  
  
# Bluetooth Tracker          
esp32_ble_tracker:
  scan_interval: 10s 




binary_sensor:
  
  - platform: ble_presence
    mac_address: C5:68:04:6A:0C:F5
    name: "Tracker John"

What error are you getting?

I get a red cross next to the line number in the editor on the line with scan_interval and it wont let me upload to the esp

If you hover on the red cross you get an error message.

Ahhh !! I didnt know that. It says The option was removed as it didnt have any effect. Do you know the best way of speeding up the scan by ant chance?

Presumably the interval option in the docs

Yep tried that, all the parameters listed throw up variuos errors. Not sure what is going on…

None of which you post here.

I found the problem
the syntax should be

scan_parameters:
Interval: 10s

Not as I had above.
scan_interval: 10s
Thanks for your prompting me to try out the various settings

regards John

As I said above.

Were you able to successfully implement the BLE presence with EspHome and the Esp32?

Not really, I got it to work but it was slow to respond and unreliable. I tried various time scan setting to
no avail. Gave up eventually.
I am now in the process of trying out a dedicated MQTT BLE detector, at the moment I havnt finished building the hardware so cant say if it is better.
I am using this https://jptrsn.github.io/ESP32-mqtt-room/index.html#getting-started. I am using an ESP8266 with an external Bluetooth module as the ESP 32 does have problems with it bluetooth apparently
Cheers

Don’t mean to necrodig this topic, but I fell on it while hunting for a solution on device trackers.
I’m pretty sure modern smartphones are fuzzing / hiding their real Bluetooth mac address to avoid tracking. It’s a good thing since you don’t want a person or business to be able to identify you’re in a specific location, passively (ie without you knowing).

Hence, the “declared” Bluetooth mac as seen by many devices will be a random one.
If you want to accurately know which BLE devices are around, it’s probably better to use an ibeacon feature, meaning you poll this BLE beacon with an app every 5 min, showing you indeed are around.

Esphome features this and combining it with owntrack (or similar) should do the trick.

I am using the marvellous esp32_ble_tracker with sensor platform xiaomi lywsd03mmc passing data to the Home Assistant HACS Passive BLE monitor integration. I have 2 esp32 nodes equipped with esp32_ble_tracker gathering data from 4 xiaomi_lywsd03mmc units. Why 2 nodes? Redundancy, and better coverage - sometimes one node picks up the data, sometimes the other.

Each of the esp32 nodes automatically creates 4 entity names in Home Assistant for the 4 Xiaomi units, so I end up with duplicate entities for the 4 units. That is, it creates sensor.mijia1_temperature and sensor.mijia1_temperature_2 for the same lywsd03mmc on each of the two esp32 nodes. I merge the 2 back into a single data stream in Home Assistant with a template sensor.

My question: is there a way to stop the duplication of the Home Assistant entity names from the espnode end of things? Then, I can remove my template sensor, and reduce the data load on Home Assistant.

For reference, here’s my espnode yaml extract: (It is identical on each of the espnodes, except that one is id: blehub1 and the other is blehub2: the MAC for the xiaomi unit is the same)

esp32_ble_tracker:
  scan_parameters:
    active: false
  id: blehub1

sensor:
  - platform: xiaomi_lywsd03mmc
    mac_address: "A4:C1:38:XX:XX:XX"
    bindkey: "xxxxxxxxxxxxxxxxxxxxxxxxxxx"
    temperature:
      name: "Mijia1 Temperature"

And here’s my (minimal) Home Assistant config.yaml extract to use the ble monitor integration:

ble_monitor:

And the template sensor to merge the two entities back into one:

template:
  - sensor:
      - name: "Mijia1 Temp"
        unit_of_measurement: "°C"
        state: >

           {% set temp1 = states('sensor.mijia1_temperature') %}
           {% set temp2 = states('sensor.mijia1_temperature_2') %}
           {% set temp1time = as_timestamp(states.sensor.mijia1_temperature.last_updated) %}
           {% set temp2time = as_timestamp(states.sensor.mijia1_temperature_2.last_updated) %}

           {{ temp1 if (temp1time > temp2time) else temp2 }}

Thanks and Regards…

3 Likes

I would love to know this too. Perhaps you could start a new thread with this to gain more attention?

I think I figured it out myself: use ble_gateway on the esp node, and use the Passive BLE Monitor in my Home Assistant configuration.yaml to do the decryption of the raw packet data.

The data from each node gets sent to Home Assistant using the ble_monitor.parse_data service call, and the data from each Xiaomi unit gets mapped to a single entity in Home Assistant.

That is, my espnode yaml is now:

external_components:
  - source: github://myhomeiot/esphome-components

esp32_ble_tracker:
  scan_parameters:
    active: False

ble_gateway:
  devices:
    - mac_address: !secret mijia1mac # xiaomi_lywsd03mmc1
    - mac_address: !secret mijia2mac # xiaomi_lywsd03mmc2
    - mac_address: !secret mijia3mac # xiaomi_lywsd03mmc3
    - mac_address: !secret mijia4mac # xiaomi_lywsd03mmc4
  on_ble_advertise:
    then:
      homeassistant.service:
        service: ble_monitor.parse_data
        data:
          packet: !lambda return packet;
          gateway_id: $nodename

And my Home Assistant yaml is now:

ble_monitor:
  bt_interface: "disable"
  discovery: False
  devices:
    - mac: !secret mijia1mac
      encryption_key: !secret mijia1key
      temperature_unit: C
      decimals: 1

    - mac: !secret mijia2mac
      encryption_key: !secret mijia2key
      temperature_unit: C
      decimals: 1

    - mac: !secret mijia3mac
      encryption_key: !secret mijia3key
      temperature_unit: C
      decimals: 1

    - mac: !secret mijia4mac
      encryption_key: !secret mijia4key
      temperature_unit: C
      decimals: 1

This seems to work fine: as an added bonus I get the RSSI and Voltage entities, and the RSSI plot shows the RSSI from each of the Xiaomi units to that esp node. Fascinating.

Regards, Phil

3 Likes

Thanks so much Phil! I got it working with 7 thermometers and 3 ESP32s. All that’s left is to work out how to move the data from the previous entities to the new ones so I don’t lose the history.

Mark,

I’ve since reduced the number of bt macs on each esp32 to 2 from 4 - I was getting too much data! I was surprised by the ability of each esp32 to reach each Mijia thermometer - RSSI values were ranging from -60 dBm to -90 dBm and the data from each Mijia was still getting through.

Also, I discovered how to get the de-crypted Mijia data back to the esp32 in order to show it on an attached display (this is in the esp32 yaml config):

  # Get the Mijia1 temperature value back from Home Assistant
  - platform: homeassistant
    id: mijia1_temperature
    name: "Mijia1 Temperature"
    entity_id: sensor.ble_temperature_a4c13829xxxx

  # Get the Mijia1 humidity value back from Home Assistant
  - platform: homeassistant
    id: mijia1_humidity
    name: "Mijia1 Humidity"
    entity_id: sensor.ble_humidity_a4c13829xxxx

Regards, Phil

Hi guys,

“Novice question”.

I’ve configured esp32_ble_tracker (see below) and it seems to work (I see the devices and reported temperature in the log), but I don’t see it in HA. In the integrations I see the device, but in its Controls it says “This device has no entities”. Any ideas? What is wrong?

esp32_ble_tracker:

sensor:
  - platform: xiaomi_lywsd02
    mac_address:  "XX:XX:XX:XX:XX:XX"
    temperature:
      name: "Xiaomi Temperature1"
  - platform: xiaomi_lywsd02
    mac_address:  "XX:XX:XX:XX:XX:XX"
    temperature:
      name: "Xiaomi Temperature2"

Thanks for your help,

Alex