ESPHome BLE beacon for room presence

Hi,
I was hoping to use ESPHome on some ESP32’s to setup room presence detection by integrating with OwnTracks and HA.
I’ve got 3 ESP32’s so far - all setup and working fine as BLE beacons, and OwnTracks can detect these and report location to HA.
The problem is, there doesn’t seem to be a way to turn-down the power on the ESP32 BLE transmittor using ESPhome, so OwnTracks detects that it can see all 3 beacons.and therefore can’t tell HA what room I’m in.
Has anyone done something similar and managed to get this kind of setup working?

1 Like

I’ve just started experimenting with two ESP32’s flashed with ESPHome with esp32_ble_beacon. One ESP32 on the top floor, one ESP32 on the ground floor. I’ll be adding more ESP32 devices around my soon. I’m anxious to see how accurate i’ll be able to get the tracking.

In Home Assistant I’ve created two (two beacons) input_boolean-entities for each device i want to track. With an automation I toggle the input_boolean-entities to keep track of which beacons have entering/exited.

This snippet registers the zones and beacons:

zone:
  - name: Woonkamer
    latitude: 52
    longitude: 4
    radius: 1
    passive: true
  - name: Zolder
    latitude: 52
    longitude: 4
    radius: 1
    passive: true

homeassistant:
  customize:
    zone.woonkamer:
      track_ios: true
      beacon:
        uuid: 11111111-1111-1111-1111-111111111111
        major: 1
        minor: 2
    zone.zolder:
      track_ios: true
      beacon:
        uuid: 11111111-1111-1111-1111-111111111111
        major: 1
        minor: 3

This is the snippet for registering the two beacons for each device:

input_boolean:
  beacon_woonkamer_person1:
  beacon_zolder_person1:
  beacon_woonkamer_person2:
  beacon_zolder_person2:
  beacon_woonkamer_person3:
  beacon_zolder_person3:
  beacon_woonkamer_person4:
  beacon_zolder_person4:

This is the automation which toggles the input_boolean:

- alias: Beacon zone
  trigger:
  - platform: event
    event_type: ios.zone_entered
  - platform: event
    event_type: ios.zone_exited
  action:
    - variables:
        zoneMapper:
          zone.woonkamer : 'woonkamer'
          zone.zolder : 'zolder'
        deviceMapper:
          iphone_van_person1 : 'person1'
          iphone_van_person2 : 'person2'
          iphone_van_person3 : 'person3'
          iphone_van_person4 : 'person4'
        inputEntity: "input_boolean.beacon_{{zoneMapper[trigger.event.data.zone]}}_{{deviceMapper[trigger.event.data.sourceDeviceID]}}"
    - choose:
      - conditions: "{{ trigger.event.event_type == 'ios.zone_entered' }}"
        sequence:
          - service: input_boolean.turn_on
            data_template:
              entity_id: "{{inputEntity}}"
    - choose:
      - conditions: "{{ trigger.event.event_type == 'ios.zone_exited' }}"
        sequence:
          - service: input_boolean.turn_off
            data_template:
              entity_id: "{{inputEntity}}"

This is the snippet i use for debugging my setup:

automation:
- alias: ios event
  trigger:
    - platform: event
      event_type: ios.became_active
    - platform: event
      event_type: ios.finished_launching
    - platform: event
      event_type: ios.entered_background
  action:
    - service: logbook.log
      data_template:
        name: IOS Event
        message: "{{trigger.event.data.sourceDeviceID}} {{trigger.event.event_type}}"

Notes:

  • When my iPhone is locked and the companion-app goes into the background, the beacon-exited-event will be sent. So i must ignore the “exit-event” in this situation
  • I’m unable to see my chosen UUIDs on my iPhone using apps, but the beacon-events are getting triggered.
  • In between the two beacons, there is a range in which both beacons are in range and thus both are “entered”.
  • I probably need to figure out how to reduce the range of beacons in ESPHome when i’ll be adding more ESP32 beacons.
  • The ESP32 devices will be integrated into smart-lights which are now part of the Tuya ecospace.
  • I’ve read somewhere that an iPhone would be able to monitor up to 20 beacons.

I’m using Owntracks rather than the HA companion app, but have some of the same challenges - particularly that the phone see’s more than one beacon at once. There doesn’t appear to be a way to reduce the transmit power of the ESP32 devices (yet).

Hi Derek,

I’ll also look into Owntrack, maybe that would give some advantages. Although i have Home Assistant companion installed on the devices of my family members.

As i’ve a profession of developer, i was able to make the necessary changes in ESPHome to set the BLE TX Power and upload it to my devices. Initially it was hard to notice to see a difference in the receive strength on my phone. But when walking around my house, i do see that beacons lose their connection sooner.

This is the Pull Request i’ve just created:
Added “tx_power” parameter to esp32_ble_beacon + minor fix by GrumpyMeow · Pull Request #3299 · esphome/esphome (github.com)

More accuracy might be possible when implementing iOS Beacon Ranging

1 Like

Great work! I’ll give it a try. Thanks

It seems to be working ok (once I figured out how to pull down the PR). Unfortunately, even setting tx_power down as low as 0 still doesn’t seem to reduce the power enough to make room presence detection workable for me (using OwnTracks)

I’ve replaced the module in my Yeelight ceiling light with an ESP32 module with ESPHome. On the lowest TX power I almost get room accuracy. Unfortunatly one floor down (directly under) the given room, the beacon is also detected.

For more accuracy, “ranging” is needed. This will monitor the signal strength of multiple beacons and calculate the most likely position.

On iOS this app provides ranging; ‎Beacon Ranging in de App Store