# Presence detection - ESP32 Bluetooth Classic scanner

**URL:** <https://discourse.nodered.org/t/presence-detection-esp32-bluetooth-classic-scanner/83988>\
**Category:** General\
**Created:** [23 December 2023 16:23 UTC](https://discourse.nodered.org/t/presence-detection-esp32-bluetooth-classic-scanner/83988 "2023-12-23T16:23:14Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![krambriw](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/krambriw/32/5429_2.png) [@krambriw](https://discourse.nodered.org/u/krambriw)\
**Post date:** [23 December 2023 16:23 UTC](https://discourse.nodered.org/t/presence-detection-esp32-bluetooth-classic-scanner/83988/1 "2023-12-23T16:23:15Z")

</div>

Here is a little interesting project if you are looking for presence detection to be used in your home automation & security system. Myself, I have since years been using Andrew Freyer's Monitor project.  
How I am using it is described here: [Use Bluetooth to scan for devices as a presence detector? - #15 by xx\_Nexus\_xx](https://discourse.nodered.org/t/use-bluetooth-to-scan-for-devices-as-a-presence-detector/52379/15)

This has worked so good & reliable, I was actually not looking for a replacement, but when I found a new variant, said to be a port of Andrew's work, now possible to run on an ESP32, I could not resist at least trying it

Even better, it is a port shared by RoboMagus that works with ESPHome!! (If you haven't tried ESPHome yet, well it is a simple way of using ESP devices for all kind of tasks, I say it is a "more or less no-coding" environment to program ESP devices)  
[https://esphome.io/guides/installing\_esphome](https://esphome.io/guides/installing_esphome)

The component shared by RoboMagus is not yet merged with the official ESPHome release so some extra simple "things" needs be taken care of, i.e. importing it from github, see the example .yaml below

> <https://github.com/esphome/esphome/pull/4736>
>
> \# What does this implement/fix?
> 
> 
> BLE Presence is great, but cannot work with… all devices. I've had a ported, slimmed down version of \[Andrewjfreyer's \`monitor\`\](https://github.com/andrewjfreyer/monitor) running on a couple of ESP32's locally for over a year to take care of my BT (classic) presence sensing needs. 
> 
> This PR brings the functionality of my first attempt at porting the BT presence monitor (\[ESP32-MQTT-Bluetooth-Monitor\](https://github.com/RoboMagus/ESP32-MQTT-Bluetooth-Monitor)) to ESPHome.
> 
> In a nutshell, BT Classic devices are detected by performing a device name query for a known MAC address. If the device responds, it is present. If it does not respond in the allowed number of attempts, it is assumed absent.
> 
> \*\*This PR is currently kept W.I.P. untill the following items are dealt with:\*\*
> \- \[x\] Working implementation of \`Scan Triggers\` and \`Scan Result Listners\`
> \- \[x\] Documentation for esphome-docs
> \- \[x\] Tested with ESP-IDF framework
> \- \[x\] Binary device presence sensor implementation
> \- \[x\] Tests are added
> 
> \*\*Note:\*\*
> Getting the ESP32 to setup using the correct BT Mode using \`esp-idf\` turned out to require some refactoring of other (ble) components. For this purpose I have extracted common definitions / functionality into a separate component: \`esp32\_bt\_common\`, though I'm not sure if that would be the best solution. Open to suggestions! 😉 
> 
> \## Types of changes
> 
> \- \[\] Bugfix (non-breaking change which fixes an issue)
> \- \[x\] New feature (non-breaking change which adds functionality)
> \- \[\] Breaking change (fix or feature that would cause existing functionality to not work as expected)
> \- \[\] Other
> 
> \*\*Related issue or feature (if applicable):\*\* N/a
> 
> \*\*Pull request in \[esphome-docs\](https://github.com/esphome/esphome-docs) with documentation (if applicable):\*\* esphome/esphome-docs#2873
> 
> \## Test Environment
> 
> \- \[x\] ESP32
> \- \[x\] ESP32 IDF
> \- \[\] ~~ESP8266~~ \`incompatible\`
> \- \[\] ~~RP2040~~ \`incompatible\`
> 
> \## Example entry for \`config.yaml\`:
> \<!--
> Supplying a configuration snippet, makes it easier for a maintainer to test
> your PR. Furthermore, for new integrations, it gives an impression of how
> the configuration would look like.
> Note: Remove this section if this PR does not have an example entry.
> \--\>
> 
> \`\`\`yaml
> \# Example config.yaml
> 
> light:
> # Led for blinking when BT Scan is active:
> - platform: status\_led
> id: state\_led
> pin: GPIO2
> 
> esp32\_ble\_tracker:
> 
> binary\_sensor:
> # BT Classic can run side-by-side with BLE!
> - platform: ble\_presence
> mac\_address: AA:BB:CC:DD:EE:F0
> name: FitBit Presence
> 
> - platform: bt\_classic\_presence
> name: Galaxy Phone Presence
> mac\_address: AA:BB:CC:DD:EE:F1
> num\_scans: 5
> update\_interval: 60s
> 
> - platform: bt\_classic\_presence
> name: Galaxy Watch Presence
> mac\_address: AA:BB:CC:DD:EE:F2
> num\_scans: 3
> update\_interval: 30s
> 
> esp32\_bt\_classic:
> on\_scan\_start:
> - light.turn\_on:
> id: state\_led
> on\_scan\_result:
> # Can listen for ANY scan result:
> - then:
> - homeassistant.event:
> event: esphome.bt\_scan\_result
> data:
> node: "Test-esp32"
> status: !lambda "return status.str();"
> name: !lambda "return name;"
> address: !lambda "return address.str();"
> - light.turn\_off:
> id: state\_led
> # Can listen for specific MAC addresses:
> - mac\_address:
> - AA:BB:CC:DD:EE:F0
> - AA:BB:CC:DD:EE:F2
> then:
> lambda: |-
> ESP\_LOGI("BT\_SCAN\_RESULT", "Result for one of the Watches!\\n address: %s\\n name: %s\\n status: %s (%d)", address.c\_str(), name, status.c\_str(), status.bt\_status());
> \`\`\`
> 
> \## Checklist:
> - \[x\] The code change is tested and works locally.
> - \[x\] Tests have been added to verify that the new code works (under \`tests/\` folder).
> 
> If user exposed functionality or configuration variables are added/changed:
> - \[x\] Documentation added/updated in \[esphome-docs\](https://github.com/esphome/esphome-docs).

First thing I recommend you to do is to install or update ESPHome to latest version. I have ESPHome installed in both Windows, Linux and MacOS. Currently I use MacOS most of the time but the others are also working fine. And you need to have python3 installed, yes

New installations, run in command prompt/terminal: _ **pip3 install esphome** _  
Update existinhg: _ **pip3 install esphome -U** _

Then verify your installation: _ **esphome version** _

With ESPHome you define your projects functionallity and features in a simple way with a .yaml configuratíon file. So, if you try the one I have attached below, you first check that it is correct from syntax point of view: _ **esphome config btclassic\_example.yaml** _

After you have updated the configuration file with your own access point's ssid, password, prefered ip addresses for the device, your mqtt broker and data about your phone to scan (name and mac address etc), you can go ahead and upload it to your ESP32 device: _ **esphome run btclassic\_example.yaml** _

If everything works as expected, your ESP32 should now start to scan, looking for your configured phones and devices

 ![IMG_1835](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/f/f/ff7e7bf59fe4b1c61aa94190e5f61ce02e3e852e.jpeg)

What is great to see, it recognizes my iPhone correctly when it is in or out of reach. I have not tested the distance it can cover but it is a great concept since you can now have multiple cheap ESP32 devices distributed around to cover the whole area of interest

To integrate with Node-RED via MQTT (what else??) a simple flow like below can be used as a starting point. Number of use cases are just limited by lack of fantasy

Enjoy 🎅

Please change extension from .txt to .yaml after download  
[btclassic\_example.txt](https://discourse.nodered.org/uploads/short-url/QupXiwMPbPPTUmz2Gg5ngrQtFt.txt) (1.9 KB)

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/6/6/66d24dca867c3c922d110b612ac51145b47af3c9.png)

---

<div class="post-metadata">

**Author:** ![xx\_Nexus\_xx](https://avatars.discourse-cdn.com/v4/letter/x/54ee81/32.png) [@xx\_Nexus\_xx](https://discourse.nodered.org/u/xx_Nexus_xx)\
**Post date:** [23 December 2023 23:45 UTC](https://discourse.nodered.org/t/presence-detection-esp32-bluetooth-classic-scanner/83988/2 "2023-12-23T23:45:38Z")

</div>

Well documented 👍 👍 I'm using the same approach in meanwhile as well (sorry, had no time to document) ... and I can confirm it works reliable.

I have positioned one ESP32 in every room and can easily detect a phone (or any bluetooth device) moving in my house.

Question: Has anybody use any of these [bluetooth trackers](https://www.aliexpress.com/item/1005005776248634.html?spm=a2g0o.productlist.main.1.30bd1120iVKw6g&algo_pvid=c833ee2f-f85d-40f8-9584-6acf0ddec99a&algo_exp_id=c833ee2f-f85d-40f8-9584-6acf0ddec99a-0&pdp_npi=4%40dis%21AUD%2119.72%216.31%21%21%2194.33%21%21%402101c59817033749301155383ebc0e%2112000034325628150%21sea%21AU%21723212347%21&curPageLogUid=IR5wTC2puwlb) which can be found at Aliexpress? .. Any feedback is welcome

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/8/0/80a1de3a65abc0ecf8cb0913fade0c3b61098b6c.jpeg)

Comment: I'm not using the ESPHome ecosystem, but only the ESP32 module with MQTT functionality and integration into Node-Red

have fun with this project

well done @krambriw (mach weiter so 😃 )

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [21 February 2024 23:45 UTC](https://discourse.nodered.org/t/presence-detection-esp32-bluetooth-classic-scanner/83988/3 "2024-02-21T23:45:47Z")

</div>

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.
