iPhone presence detection with ESPHome(instant detection)

  1. Install “crownstone” application on your iPhone

  2. Coding ESPHome to detect iPhone

    • Use an ESP32 board
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.xx.xx.xx.xx.xx.xx.xx.xx.xx.xx.xx.xx.xx.xx.xx.xx (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.xx.xx.xx.xx.xx.xx.xx.xx.xx.xx.xx.xx.xx.xx.xx.xx (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.xx.xx.xx.xx.xx.xx.xx.xx.xx.xx.xx.xx.xx.xx.xx.xx (17)”
    and change it in the code
    (starts with “01” and ends with “(17)”)
4 Likes

Is this room to room or at home detection?

If you need room level detection, install ESP32 in every room and adjust the rssi value.

Thanks for the great example and it works. Could you share an example of how to output an RSSI sensor for each person? For example, it would be more convenient if this RSSI could be changed through the Home Assistant, so as not to get into the code every time and not update the ESP32 board