home-assistant / core

:house_with_garden: Open source home automation that puts local control and privacy first.
https://www.home-assistant.io
Apache License 2.0
73.5k stars 30.7k forks source link

iBeacon Tracker distance measurement is incorrectly flagging the beacon as briefly far away #80223

Closed BlueCharmBeacons closed 1 year ago

BlueCharmBeacons commented 2 years ago

The problem

The iBeacon Tracker distance measurement is incorrectly flagging the beacon as far away one minute (over 4 meters in my automation), then about a minute later is showing it as very nearby (under 2 meters in my automation). The beacon is sitting 12 inches away from the HA. I am not using any ESP for tracking, just the HA.

My guess is that the scanner is seeing single inaccurate RSSI numbers and based on that single number, marking the beacon as far away enough to turn off my Shelly plug. This is actually very normal behavior from just about every scanner or smartphone I have ever tested. Beacon scanners will sometimes measure signal strength for one or two RSSIs inaccurately. Why? It's complicated and there seems to me no way to avoid this. Here's a link with some good data about this:

https://www.wouterbulten.nl/blog/tech/kalman-filters-explained-removing-noise-from-rssi-signals/

What I suggest is the intergation code should watch the RSSI figures, and if it sees a "wacky" one that is more that 10 less than the one before, it should adjust that figure for that single RSSI measurement to "no more than 10 less than the one before."

An example:

Scanner measures RSSI signal received from beacon and sees this string of numbers over time -50 -48 -40 -43 -47 -65 -49 -53 -50 etc.

The -65 was an aberration, probably caused by a signal collision or interference or a poltergeist.

So I would suggest that the code can adjust this -65 figure to -57 in this case, i.e. only 10 less than the one before (the -47). Call this a "smoothing factor" or whatever. Would be cool if users could adjust this to suit their tastes.

There are other ways to attack this signal wavering (e.g. Kalman filters), but they're complicated, and I have found that my "dumb" adjustment method works fine.

By the way, I have never seen any significant number of aberrant RSSI figures in the upwards direction, i.e. I haven't seen this happening before on any scanner: -50 -48 -40 -43 -47 -25 -49 -53 -50 etc. So the simple 10 adjustment only needs to be coded for one direction of aberrant RSSI figures.

What version of Home Assistant Core has the issue?

2022.10.3

What was the last working version of Home Assistant Core?

No response

What type of installation are you running?

Home Assistant OS

Integration causing the issue

iBeacon Tracker

Link to integration documentation on our website

https://www.home-assistant.io/integrations/ibeacon

Diagnostics information

No response

Example YAML snippet

Running two simple automations. One turns off a shelly plug when beacon distance is above 4 meters. The other turns on the same plug when beacon distance is under 2 meters. 

type: distance
platform: device
device_id: ef89db5413b5414116c647629a24a811
entity_id: sensor.bluecharm_3_1889_estimated_distance
domain: sensor
above: 4

type: distance
platform: device
device_id: ef89db5413b5414116c647629a24a811
entity_id: sensor.bluecharm_3_1889_estimated_distance
domain: sensor
below: 2

Anything in the logs that might be useful for us?

shellyplugus-083af20184d8  turned on triggered by automation  Turn on office shelly plug when BC3 gets near  triggered by numeric state of  BlueCharm_3 1889 Estimated Distance
5:19:25 PM - 24 minutes ago

Turn on office shelly plug when BC3 gets near  triggered by numeric state of  BlueCharm_3 1889 Estimated Distance
5:19:25 PM - 24 minutes ago

shellyplugus-083af20184d8  turned off triggered by automation  Turn off office shelly plug when BC3 goes away  triggered by numeric state of  BlueCharm_3 1889 Estimated Distance
5:18:25 PM - 25 minutes ago

Turn off office shelly plug when BC3 goes away triggered by numeric state of BlueCharm_3 1889 Estimated Distance
5:18:25 PM - 25 minutes ago - Traces

Additional information

No response

homeassistant commented 2 years ago

Hey there @bdraco, mind taking a look at this issue as it has been labeled with an integration (ibeacon) you are listed as a code owner for? Thanks! (message by CodeOwnersMention)


ibeacon documentation ibeacon source (message by IssueLinks)

BlueCharmBeacons commented 2 years ago

HA beacon RSSI record

bdraco commented 2 years ago

The library doesn't implement any data smoothing. That would be a nice improvement, but its not something I have on the radar to implement soon as our current focus is to migrate as many people from using https://github.com/custom-components/ble_monitor to core integrations as most of the trouble we have with bluetooth is users running both at the same time. Once thats done, there will be some more time to take some of the newer bluetooth integrations from MVP to more feature packed.

If someone wants to pick this up and implement data smoothing in the library, I'll find the time to review it 👍

ih8gates commented 2 years ago

I use the distance of one of my BlueCharms to try and determine whether my trash can is at the house or further away at the street. Like you said, distance can be a funky measurement as there are so many things that affect RSSI. I use a Home Assistant statistics sensor to try and smooth this out a bit. I'm not an expert at the stats stuff, so you may find a better way (I'd be glad to hear it! I'm not even sure if "mean" is the best characteristic to use... )

But here's my sensor.

  - platform: statistics
    name: "Trash Can Distance Mean"
    entity_id: sensor.trashcan_distance
    state_characteristic: mean
    sampling_size: 100
    max_age:
      minutes: 5

I get the sensor.trashcan_distance from a template sensor that grabs the info from Room Assistant, but in your case, you can use the estimated_distance sensor that the iBeacon integration provides.

BlueCharmBeacons commented 2 years ago

I use the distance of one of my BlueCharms to try and determine whether my trash can is at the house or further away at the street. Like you said, distance can be a funky measurement as there are so many things that affect RSSI. I use a Home Assistant statistics sensor to try and smooth this out a bit. I'm not an expert at the stats stuff, so you may find a better way (I'd be glad to hear it! I'm not even sure if "mean" is the best characteristic to use... )

Hi Scott,

Just wanted to let you know that I saw your suggestion here, and I am working on it now. I am trying a different integration than Statistics ( I could not make much sense of the different choices; I shoulda paid more attention in stats class!); I am using Filter instead (maybe these aren't called integrations? I can't figure out the vocabulary of HA yet!). Basically the same idea as Statistics but a bit simpler math for my simple brain.

It seems like I have got it working-ish, but I still need to test it for reliability and false reports. I put this in config.yaml:

Trying to smooth RSSI data

sensor:

Also, I am trying to get faster reactions so I (and other people) can use this for turning on/off lights in a room or opening/closing garage doors.

So far, my iBeacon Tracker reactions have been good but pretty slow (20-40 seconds). I am thinking maybe I need to adjust some sort of config file for the iBeacon Tracker, but I am not sure if that exists or where to find it. Still figuring this stuff out.

Thanks for the tip!

ih8gates commented 2 years ago

My tests with the new iBeacon integration show slow responses, too. I continue to use Room Assistant for presence sensing. Looking forward to using the iBeacon integration exclusively since it's much easier to maintain a few ESPHome BT proxies that it is to run Room Assistant on multiple Raspberry Pi's.

BlueCharmBeacons commented 2 years ago

When I use iBeacon Tracker and set it to trigger on estimated distance, I get reaction time of 20-40 seconds. Probably ok for trashcan or mail delivery notifications, but not fast enough for turning on lights in a room.

When I experimented yesterday setting it to trigger on Signal Strength above or below certain numbers, I assumed it would have roughly the same delay; so I was surprised that the delay was now 1.5-2.5 minutes!

I can’t find any yaml or config screen to change the scan interval/window for iBeacon Tracker, so I guess it is what it is. Next, I’m going to play around with ESPresence to see how fast that will react. Quick a few of my customers use it successfully, but it sounds like it’s a bowl of tech spaghetti to get it dialed in correctly.

ih8gates commented 2 years ago

@BlueCharmBeacons I'm wondering if it'd be helpful for you to be looking at the "source" attribute of the beacon to determine location. I set up a second ESP Bluetooth Proxy this morning and wrote a template to help me understand what Home Assistant is seeing. If you see it "home" and near a proxy, you know that's the closest proxy. You can create a template sensor or use a template as a trigger for an automation.

I dropped this in a markdown card on my dashboard:

{%- set bts = {
  '00:19:86:CC:AA:BB':'built-in',
  'esp32-bluetooth-proxy-1aaaa':'first proxy',
  'esp32-bluetooth-proxy-2bbbb':'second proxy'
} -%}
{%- set beacons = {
  'device_tracker.scott_bike_ble':'Scott''s Bike',
  'device_tracker.scotts_car_bluetooth_beacon':'Scott''s Car',
  'device_tracker.trash_can_bluetooth_beacon':'Trash Can',
  'device_tracker.trash_can_motion_bluetooth_beacon':'Trash Can Motion',
  'device_tracker.recycle_can_bluetooth_beacon':'Recycle Bin',
} -%}

{% for bt in bts %}
  {{bts[bt] -}}
  {% for beacon in beacons -%}
    {% if state_attr( beacon , 'source') == bt %}
    * {{beacons[beacon] }} ({{states(beacon) }})
    {%- endif -%}
  {% endfor %}
{% endfor %}

and I get something like:

built-in
* Scotts Bike (home)

first proxy
* Scotts Car (home)

second proxy
* Trash Can (home)
* Trash Can Motion (home)
* Recycle Bin (home)

Because of this issue, that Trash Can Motion is forever home!

issue-triage-workflows[bot] commented 1 year ago

There hasn't been any activity on this issue recently. Due to the high number of incoming GitHub notifications, we have to clean some of the old issues, as many of them have already been resolved with the latest updates. Please make sure to update to the latest Home Assistant version and check if that solves the issue. Let us know if that works for you by adding a comment 👍 This issue has now been marked as stale and will be closed if no further activity occurs. Thank you for your contributions.