iPhone presence detection with ESPHome(room level, 1s instant detection)

  1. Install “crownstone” application on your iPhone

  2. Coding ESPHome to detect iPhone

esphome:
  name: livingroom-ble-tracker
  platform: ESP32
  board: esp32dev
  arduino_version: dev

# Enable logging
logger:
  #level: very_verbose

# Enable Home Assistant API
api:

ota:

wifi:
  ssid: "xxxxx"
  password: "xxxxx"

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "Livingroom-Ble-Tracker"
    
captive_portal:

esp32_ble_tracker:
  scan_parameters:
    interval: 1s
    window: 1s
    duration: 30s
  on_ble_advertise:
    - then:
      - lambda: |-
            bool user1_livingroom_ble_status = false;
            bool user2_livingroom_ble_status = false;
            
            for (auto data : x.get_manufacturer_datas()) {
              if (strcmp(hexencode(data.data).c_str(), "01.00.00.00.10.00.00.00.00.00.00.00.00.00.00.00.00 (17)") == 0) {
                if (x.get_rssi() > -81){
                  ESP_LOGD("ble_adv", "user1 found in livingroom");
                  user1_livingroom_ble_status = true;
                }
              }
              else if (strcmp(hexencode(data.data).c_str(), "01.00.00.00.xx.00.00.00.00.00.00.00.00.00.00.00.00 (17)") == 0) {
                if (x.get_rssi() > -81){
                  ESP_LOGD("ble_adv", "user2 found in livingroom!");
                  user2_livingroom_ble_status = true;
                }
              }
              ESP_LOGD("ble_adv", "    - %s", hexencode(data.data).c_str());
            }
            
            id(user1_livingroom_ble).publish_state(user1_livingroom_ble_status);
            id(user2_livingroom_ble).publish_state(user2_livingroom_ble_status);
            
binary_sensor:
  - platform: template
    device_class: presence
    name: "user1_livingroom_ble"
    id: user1_livingroom_ble
    filters:
      - delayed_off: 30s
  - platform: template
    device_class: presence
    name: "user2_livingroom_ble"
    id: user2_livingroom_ble
    filters:
      - delayed_off: 30s
  1. After the first run, find a string similar to
    “01.00.00.00.10.00.00.00.00.00.00.00.00.00.00.00.00 (17)”
    and change it in the code
    (starts with “01” and ends with “(17)”)
5 Likes

Please make each post only once.

This seems like a really interesting project.

What are it’s capabilities?
What is the battery drain if battery powered?

hi, i have installed, and everything works fine. The code is for 2 phones, could you post the changed code for 1 phone only? thank you

I tried that too and works nicely, thanks !
Had to change “hexencode” to “format_hex_pretty” and tweaking the RSSI to only detect the phones in that room is a bit tricky, but works :slight_smile:

2 Likes

I’ve finally gotten around to playing this and it works well.

However, the above code changes with each restart of the iPhone or launch of the Crownstone app.

Any way around this?