Get Beacon distance from RSSI

I ran across this RSSI to Distance repo and tried it, it works fairly good and thought i’d post it here. I imagine i’m not the only one who has been banging my head trying to get a working and reliable room level presense detection. I’ve been using Espresense and it works fairly good but you have limited options if you want to make full use of your esp32 and add sensors and what not. One thing i’ve noticed is it works best if the beacon and esp32 are within line of sight because any barriers between the two will increase the distance. Here is the repo, you need to upload the 2 .h files into your config/esphome/custom_components directory and then change your esp32 config accordingly. Here is a sample I used for testing. GitHub - rpatel3001/BleDistance: Distance tracking for BLE iBeacons in esphome

esphome:
  name: ble-sensor
  friendly_name: ble sensor
  includes:
    - custom_components/OneEuro.h
    - custom_components/ble_dist.h
  on_boot:
    then:
      lambda: |-
        addTracker("beacon1test", "7777772e-6b6b-6d63-6e2e-636f6d232323");  
      
        addTracker("beacon2test", "7777772e-6b6b-6d63-6e2e-636f6d121212");    

        addTracker("beaconbuttontest", "7777772e-6b6b-6d63-6e2e-636f6d232328"); 

esp32:
  board: esp32dev
  framework:
    type: arduino

# Enable logging
logger:
  level: DEBUG
  logs:
    esp32_ble_tracker: INFO
  

# Enable Home Assistant API
api:
  encryption:
    key: "2bpN4mGeMItsQJ3QkUehgfcSSfeUDUYjT+a02y5xn40="

ota:
  password: "dcb5af0ad3ef0131fb46ffbe8797805e"

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "Ble-Sensor Fallback Hotspot"
    password: "tN6qaifF97Qt"

captive_portal:

esp32_ble_tracker:
  on_ble_advertise:
    then:
      - lambda: |-
          parseAdvertisement(x);

  on_ble_service_data_advertise:
    - mac_address: DD:34:02:07:E3:D8
      service_uuid: '2080'
      then:
        - lambda: 'id(beacon1battery).publish_state(x[0]);'    

    - mac_address: DD:34:02:07:E1:37
      service_uuid: '2080'
      then:
        - lambda: 'id(beacon2battery).publish_state(x[0]);'   

    

        

sensor:

  - platform: template
    name: "Beacon 1 Distance"
    id: beacon_dist
    update_interval: 10s
    unit_of_measurement: "ft"
    device_class: ""
    state_class: "measurement"
    accuracy_decimals: 1
    entity_category: "diagnostic"
    lambda: |-
      return getTracker("beacon1test").get_dist();

  - platform: template
    name: "Beacon 2 Distance"
    id: beacon2_dist
    update_interval: 10s
    unit_of_measurement: "ft"
    device_class: ""
    state_class: "measurement"
    accuracy_decimals: 1
    entity_category: "diagnostic"
    lambda: |-
      return getTracker("beacon2test").get_dist();

  - platform: template
    name: "Beacon Button"
    id: beacon_button
    update_interval: 5s
    unit_of_measurement: "ft"
    device_class: ""
    state_class: "measurement"
    accuracy_decimals: 1
    entity_category: "diagnostic"
    lambda: |-
      return getTracker("beaconbuttontest").get_dist();     
    filters:
    - timeout: 10s  # sent value will be NaN
    - timeout:
        timeout: 10s
        value: 0  

  - platform: template
    name: "Extra Beacon 1 Battery"
    id: beacon1battery
    icon: 'mdi:battery'
    unit_of_measurement: '%'

  - platform: template
    name: "Extra Beacon 2 Battery"
    id: beacon2battery
    icon: 'mdi:battery'
    unit_of_measurement: '%'  

  - platform: ble_rssi
    ibeacon_uuid: '7777772e-6b6b-6d63-6e2e-636f6d232323'
    name: "BLE Beacon 1 RSSI"  
    ibeacon_major: 3939
    ibeacon_minor: 4848

  - platform: ble_rssi
    ibeacon_uuid: '7777772e-6b6b-6d63-6e2e-636f6d121212'
    name: "BLE Beacon 2 RSSI"  
    ibeacon_major: 3838
    ibeacon_minor: 4949 

 


binary_sensor:
  - platform: ble_presence
    ibeacon_uuid: '7777772e-6b6b-6d63-6e2e-636f6d232323'
    ibeacon_major: 3939
    ibeacon_minor: 4848
    name: "ESP32 BLE Tracker Beacon 1"    

  - platform: ble_presence
    ibeacon_uuid: '7777772e-6b6b-6d63-6e2e-636f6d232328'
    #ibeacon_major: 4848
   # ibeacon_minor: 3939   
    name: "ESP32 BLE Tracker Test Button"   

  - platform: ble_presence
    ibeacon_uuid: '7777772e-6b6b-6d63-6e2e-636f6d121212'
    ibeacon_major: 3838
    ibeacon_minor: 4949 
    name: "ESP32 BLE Tracker Beacon 2"     

Also, If you are using BlueCharm Beacons this will create a sensor for your beacon battery.

esp32_ble_tracker: 

  on_ble_service_data_advertise:
    - mac_address: DD:34:02:07:E3:D8
      service_uuid: '2080'
      then:
        - lambda: 'id(beacon1battery).publish_state(x[0]);'  

sensor:
  - platform: template
    name: "Extra Beacon 1 Battery"
    id: beacon1battery
    icon: 'mdi:battery'
    unit_of_measurement: '%'

2 Likes

This is the card or way i think i’m going to display the beacons if anyone wants to use it too.

1 Like