ESPHome BLE tracking Mi Band 4 - Working

I was using ESPHome BLE tracker to track my Mi Band 3, which was working pretty well.
My new Mi Band 4 arrived and I can confirm it also works.

Mi Band 4 has BLE v5.0, so I’ll be interested in how it might work with Home Assistant on Raspberry Pi 4, which also has Bluetooth 5.0.

ESP32 only has bluetooth 4.0, but v5.0 is backwards compatible.

Hello mahko, could you share your esphome ble config? I’m trying to make my Mi band 4 working too but with no luck at all right now!
Thanks

Hi,

I can confirm this is still working solidly for my Mi Band 4.
Esphome config below. It’s pretty standard. I’m not using the commented out rssi part at the moment.

You need to follow the other standard steps for ESPHome integrations etc.

It’s possible that you may need to enable Discovery mode in the Mi Fit app for you band to advertise properly (Something like Mi Band app > Mi band 4 device > set ‘Discoverable’ to ‘On’)

If you can describe a little more at which point you think you are getting stuck I may be able to help more.

esphome:
  name: presence_detection
  platform: ESP32
  board: esp32doit-devkit-v1

wifi:
  ssid: "XXXXX"
  password: "XXXXX"

# Enable logging
logger:

# Enable Home Assistant API
api:

ota:

esp32_ble_tracker:
  scan_interval: 20s

sensor:
  # - platform: ble_rssi
    # id: mibandrssi_2
    # mac_address: C1:DE:8E:XXXXX
    # name: "RSSI MiBand BathroomESP"
    
binary_sensor:
  - platform: ble_presence
    mac_address: FD:23:07:XXXXX
    id: mibandpresence_2
    name: "Presence MiBand EntryESP"

I also have a binary sensor like this in my hassio config so that it is marked as on for 10 mins after it is last seen by ESPHome.

binary_sensor:
  - platform: template
    sensors:
      mi_band_sticky:
        value_template: >-
          {{is_state('binary_sensor.presence_miband_entryesp', 'on')}}
        delay_off:
          minutes: 10
1 Like

Thank you so much mahko!

Hello Mahko,
I’ve just finished configuring my first esp32 ble tracker based on your settings…and it works very well…:slightly_smiling_face:

What I’m trying to achieve now is the setting of a room occupancy detection system, based on a handful of esp32 tracker spreads around my house.
I can think to a sensor that would track my MiBand around the house, showing the room I’m at the given moment.

Did you do something like this?
If so, have you got some tips?

Thanks

Hello,

No I don’t use it for room level detection, just “home level”. I think you’ll have to sift through some of the threads on the topic.

I think the state-of-the-art for room level detection is still Monitor (running on pi zero) , there has been some dicussion about porting that to ESP32.

I think you may have challenges with detection times being too long (for lights, you proabably want this under second?). If you have motion detectors you might be able to use them for the intial detection/action, then use the esp32 presence to hold the scene.

Your best bet is to search other threads for "room presence detection on ESP32 or EPHome and see where it’s all at.

If you were to try to use ESPhome, you would need to use the rssi sensor - you can approximate which node is closest.

Cheers.

May I ask where to add this code? In configuration.yaml file? I got a mi band 4 and wondering how to make it work with HA. Can you plz tell me in simple steps.
Thanks

To do it the way we have, you need an ESP32, then set up the ESPHome integration, then configure as per the ESPHome page/above.

Do you have an ESP32 already? You need one for this approach.



https://www.google.com/search?q=esp32&rlz=1C1CHBF_en-GBAU859AU859&oq=ESP32&aqs=chrome.0.69i59l3j69i60l3j69i65j69i60.1374j0j7&sourceid=chrome&ie=UTF-8

I can provide more steps if you need…

I have connected esp32 using HA esphome addon. I have followed the tutorial, added an entry in esp32.yaml file, and restarted my pi, however, I do not find my miBand4 anywhere in entities.

I have added the following code in esp32.yaml file,

# ESP32
esp32_ble_tracker:
binary_sensor:
  - platform: ble_presence
    mac_address: E1:00:9E:7F:51:B4
    name: "miBand4"

my esp32 is on and running, have a look at the picture attached.

Looking forward to receiving more tips from you.

Regards,
Sahar

A few things to check next

  • have you made your mi band discoverable in the mi fit app?
  • Can you see the mi band in the “show logs”?
  • have you added a scan interval?
 esp32_ble_tracker:
  scan_interval: 20s
  • have you tried to add the integration via configuration tab?

By the way, apparently it’s not good to put your mac address on these forums, becauase then I can track you:)

Yes

I am sorry. Where can I find these logs?

Now added but it seems it’s having no effect.

Yes, I have tried. See picture attached. But still, i am unable to find it in entities.

My Full esp32 config file

Yes, I have put a fake MAC address for the sake of posting it on the forum.

The picture in you post above “my esp32 is on and running, have a look at the picture attached.” has a little text link saying “show logs”. When that fires up and if the ble scanner is working properly, you will see log lines detecting devices around you. If it’s seeing your mi band every now and then, then we can move on to next steps…

Also, just double checking that you are re-uploading after making config changes?

In logs there is no info about scanning devices. It just shows the following details.

Ah!. Thats what i was not doing. Many thanks for pointing it out. I can now see an entity on my main dashboard.

1 Like

As it is all now setup and working as solid as a rock, I would like to get something more out of it. Is it possible to switch a relay ON/OFF attached to pin2 of the same esp32 based on presence detection?

I think it should be, but I’m not very familiar with that. Have a browse of the Esphome website under components etc. Should be something there?

And take a look at the Esphome automations. It can probably all happen directly on the esp32.

But my knowledge ends there…

Hello,

What if I’m having raspberry pi with bluetooth? may I track my Mi Band 4? if yes, how?

Thank you.

Both devices need to have traditional bluetooth for bluetooth tracking. Mi band only has BLE.

You may be able to track your Mi Band with your raspberry pi (both have BLE) using the Bluetooth LE Tracker. You would need to research/investigate. Also research potential Wifi performance issues as that’s a thing.

hello unfortunately miband4 does not work if it is paired with the mifit application, you can scan miband if the application disconnects from it or turns off the phone then miband sends a ble signal unfortunately in the miband application there is no make discoverable option, maybe in older versions there was such an option or someone can solved this problem?