Bermuda - Bluetooth/BLE Room Presence and tracking [custom integration]

alias: Pannenkoekenplant area follower
description: "When Bermuda detects this plant moved area, update the plant and relevant sensors to that area so room temperature and light intensity aggregates make sense again."
trigger:
  - platform: state
    entity_id:
      - sensor.pannenkoekenplant_1_area
    attribute: area_id
action:
  - service: ha_registry.update_entity
    data:
      area_id: |
        {{ state_attr('sensor.pannenkoekenplant_1_area','area_id') }}
      entity_id:
        - plant.pannenkoekenplant
        - sensor.plant_sensor_6bf38a_temperature
        - sensor.plant_sensor_6bf38a_illuminance
mode: restart

:heart_eyes:

This can most definitely be improved, but first I need to get more bluetooth_proxies :upside_down_face:

1 Like

Haha that’s awesome! Nicely done :smiley:

Thanks! One more thing I was wondering about; A lot of my proxies (shelly’s) are exactly (in the wall) in between areas, I guess when strategically placed, this would allow for accurate area detection, however I don´t think the current implementation supports that. (e.g. each proxy = in 1 area).
I have noticed that devices jump between proxies (and as such, so does the distance reported to the connected proxy). I wonder whether we can keep that information separate, and with that, actually use the information from more than 1 proxy to have a better understanding of where that device is.

Understandably rewriting based on a modern device_tracker integration has higher prio (having the devices merged would be great, bermuda tracker & tracked device).

Yeah, definitely using a 1:1 mapping for proxy/area initially, but ultimately I want to try and be able to support trilateration to place items anywhere between proxies.

Right now you could use the dump_devices service (Developer ToolsServicesBermuda BLE Trilateration: Dump Devices) which will give you a dump of everything it knows. Part of that is the distances from each proxy receiver for each device (even the ones you don’t have entities set up for). From that you could start trying out some calculations, or even create template sensors - if you’re up for that level of messing around.

I like the idea of being able to get the distances from adjacent areas though. I could probably change the distance sensor so that it lists the other areas as attributes. That might make an easier way to create automations against - but I think it will also create a lot more database activity, if I understand it correctly.

The device_tracker stuff probably won’t get much more attention unless some bugs come up - device_tracker only does home/not-home, and that feature seems fairly solid now. The main directions I am interested in are:

  • sorting out things that don’t just track by MAC address (eg iBeacons, iPhones/Private BLE etc) and
  • trilateration or more accurately tracking devices in relation to proxies.

The latter is currently just “which proxy am I closest to”, but I would love to see how close we can get to “exactly where am I in the house?”. The closest-proxy thing jumps about too much currently, especially when the closest proxy just misses a packet. I have some basic filtering already but it definitely needs improvement.

I’m hesitant to start creating lots of sensors and a bit hesitant about creating lots of attributes (although maybe that’s not too bad), but using the yaml from dump_devices is certainly worth looking at if you’re keen.

It’s definitely an area I’m thinking about though, so it’s good to hear different ideas.

3 Likes

Hi and great work @agittins!

I see so much potential for this. However for me the ability to track IOS (especially apple watch) is critical. I will have approx 20 ESP proxy devices and a combination of cameras and mm wave sensors. By combining these I hope I can get really close to track who is where in the house, even the kids…

Some initials thoughts from me (not a math-geek or programmer/developer) so don’t judge me.

Potential ways forward:
Alt1: Using a web interface where the user (1) adds the floor plan, (2) “draw” room boundaries and (3) places all sensors to be used. With this we can get the relative X,Y coordinates. This should be enough to then use n-number of sensors and relevant formulas to retrieve the relative X,Y coordinate which then can be compared to the floor plan to determine the location.

I started to look into triliteration. One thing that immediatly came to mind was that there is no need or even favourable to not track in 3D i.e. if it connects to devices on different levels there should be a way to only use data from sensors on the same floor. Since distance is measured by RSSI my gut feeling says that this can likely fool the system.

Alt2: Since the tracking is based on RSSI signals that can be disturbed by a number of things I was thinking that there might be a use case for AI. My thinking is that in a UI the user enters the name of the zone/room and then press a “record” button. The system stores all the RSSI data from all sensors while the user walks, jumps, lays in bed etc. for lets say 5 minutes with 2 second scan interval (which would give 150 datapoints or longer/tighter scan interval depending on how many data points is necessary for accurate analysis) in that room/zone. By doing this for all zones it should be possible to train an AI model to determine which room/zone the user is in.

With my limited abilities I can see what I can possibly contribute with but I really like the idea of having a reliable tracking system.

1 Like

Hi Hogster and thanks for the input!

TL/DR: I really do waffle on here, and none of it is likely to be relevant to anyone who just wants to use the integration - so it can be safely skipped! If you’re interested in helping / understanding my pathology though, read on…

This is one of those areas where coming up with ideas is one thing but actually implementing them to find out if they work is quite another thing altogether!

The data we get is a set of “distance” measurements, which are:

  • subject to the dark magiks of RF propagation.
  • very noisy, with noise that is massively skewed to overshoots (ie, almost all measurements are actual distance plus a positive noise figure, a few might have near-zero noise, and a tiny amount might have a negative noise offset due to in-phase reflections).
  • unreliably delivered. They might be late, or more likely missing altogether. A missing measurement is also indistinguishable on its own from an infinite distance measurement.

The unreliable delivery and asymmetric noise cause trouble straight away with the classical algorithms like Kalman filtering or even just plain averaging/smoothing. Luckily though, accurately putting certain things at the exact same place as a certain other thing, in as short a time as possible, is something that militaries are quite interested in, so there is (funded) research around, for those able to read the math. I am not skilled at the math, but I can sometimes grok the principles.

Re users drawing maps, that is a (long term) possibility, but I have no plans to do that any time soon. The main thing is that I think it will be unnecessary, but I’m in the wrong part of the Dunning-Kreuger curve to know for sure. I am (un?)reasonably confident that the system should be able to “solve” the spatial arrangement, and potentially-inaccurate user measurements would make that more difficult rather than less. I could be wrong, though. Ultimately I expect we’ll have our own “point cloud” which could then be “pinned” to a user’s map using translate/rotate/scale values.

My thinking is that if two proxies each measure the distance to one beacon, then the distance between the beacons can not be further than the sum of the two measurements (this is called “Triangle Inequality”). If the beacon moves around, that minimum-estimate will gradually improve, and if the beacon is placed at one of the beacons, we get our “answer” for the distance between those two proxies. From now on, when we receive measurements to that beacon, we have the lengths of three sides of a triangle from those proxies, so can arrive at two solutions in space for the beacon, relative to the proxies (the location is mirrored about the axis between the proxies). You can think about this by drawing circles - choose an arbitrary point as proxy-a. Draw a circle from that point with the radius of the distance between proxy-a and proxy-b, and choose anywhere on that as the location of proxy-b. Now if you draw a circle centred on each proxy for the beacon distance measured, those two circles should intersect at two locations, either side of the line between the proxies (or on the line, occasionally). In a way you have the “actual” position, and an ambiguous “reflection” of that position. The reason I think this is “better” than a user-defined map is that the distances then all come from the same, faulty system - rssi measurements, which I think might be easier to integrate than two different measurement systems. Some basic constraints from user input might be really helpful though, things like “these three proxies all lie along a single line, and this fourth proxy is on a line perpendicular to them, intersecting at the first one”, say.

When we have distance “measurements” between three points, we have solved the shape of that triangle but not which “reflection” of that triangle we have. If we bring in another proxy we get a third “fixed” point and solve where the beacon is in relation to those three points (in a 2D plane).

Once we achieve this, it stands to reason we can achieve this with every combination of proxy and beacon, at which point we have “solved” the spatial arrangement of all the beacons and proxies, relative to each other. At that stage one just needs to define a point in real space for any proxy, and an angle in real space to point at another proxy and the entire “cloud” is now aligned with real space. eg “consider my office proxy as being at [0,0,0], and the laundry proxy is exactly North of that”.

This is why I think we don’t need a user-supplied map to solve the space, and you can probably see that with the noisy nature of the data, trying to fit a user-dimensioned map adds a lot of complexity to something that hasn’t yet solved its existing complexities! :smiley:

I’m much keener for being able to visualise a 3D cloud of points in HA rather than going down the user-provided floorplan, but it would be a long time before I have time for either, I suspect.

So, being do-able for a single floor seems… waves hands likely? Solving in 3D for multiple floors is… less certain… certainly harder. I think that mathematically (as far as I can think mathematically!) there’s no reason why it shouldn’t work. With distances from two proxies you get a circle perpendicular to the plane (so it looks like two points in 2D), while distances from three proxies, if perfect, give you 2 points in 3D, which looks like a single point in 2D. Adding another proxy that’s not in the same plane (say, on a different floor) then gives us a hint at the “point”'s distance from the original plane. Except, none of these “points” are actually “points”, they’re all clouds of confusion, due to the noise and uncertainty. I think that adding that third dimension is likely stretching the already low-quality data a lot more than going to two did. I still think it’s possible, just laying out the difficulties so people can manage their expectations! :slight_smile:

As for at which point these uncertainties cause everything to fall apart (or simply be less than useful) I am not sure, but I am pretty confident that there is a lot of useful space between “not perfectly correct” and “entirely busted”.

My hesitation around solving for 3D is that while we work in only two dimensions, we sort of just fold the third dimension into the noise and try to ignore it (since any distance in the third dimension just causes an increase in all the measurements from the origin plane, which is exactly what RF noise/loss does, too). So in a way, extracting the third dimension is trying to extract meaningful data out of our left-over garbage. It should be entirely possible, but I suspect that this data will be even less reliable than that from a 2D solution - and I’m not entirely sure yet how reliable even that will be!

For now, I am going to stick to working out the 2D solution, because at this stage I believe 3d will need all the same solutions that 2D does, plus more - so it seems like a “progressive” path - we need to solve 2D first, and that work won’t be wasted in solving 3D, I think.

In practical terms, there’s probably a lot of functionality possible even without a 3D solution. For room-level occupancy, the nearest-proxy will still be pretty reliable (I expect typical inter-floor materials will add enough RF loss to make this easier) and combining with other sensors (PIR, mm-wave etc) will probably always be an important part, even for 2D (I am using Bermuda plus PIR for my office, and it’s rudimentary, but still great).

Perhaps grouping proxies by floor (which might be akin to what you were suggesting) in order to “constrain” each floor of proxies onto separate, parallel planes would then give us a per-floor location from which we’d choose by closest distance… I don’t know. It’s certainly something I am keeping in mind, but we are a long way from being able to worry about it in a productive way.

Re AI, I’m not sure how much benefit it might provide - again we’d need someone on the other side of the Dunning-Kruger curve to weigh in. Maybe it would be really good for tuning algorithms that clean up the distance measurements, or for real-time estimation of distances based on previously observed patterns - but maybe it’s not a good fit for any current ML patterns - I really don’t know. My gut feeling is that in these earlier stages there’s a lot to do in the basic mathematical realm of cleaning up and interpreting data, and that AI might be more useful down the road - but I could be wrong about that - maybe we could throw a sack of numbers into a GPU and it will work it all out! It would seem likely that it might be well-matched for estimating positions within a room given that RF reflections are so insanely complicated and simple at the same time. I’d love to hear from anyone who might have relevant experience in that area.

Sorry for the long rant, but part of it is just me mulling things over in my mind and thinking out loud.

My current thinking about next-steps that I can/will actually do are:

  • Beacon UUID / phone IRK/PrivateBLE matching

  • create per-proxy distance sensors for each beacon so that I can easily use the history tool to visualise what’s happening with the data, including missing packets. The current distance and area sensors are good, but their data makes it clear that

    • missing packets are causing spurious area switches
    • noisy data makes it hard to choose between nearby areas that are open plan (line-of-sight)
    • having multiple proxies in one area really helps with both issues (graph showing (effectively) area-of-nearest-proxy and distance-to-nearest-proxy over time for my watch):
  • experiment with some smoothing/prediction filters on those distances to fill in missed packets and reduce the noise without losing responsiveness. I’m thinking Kalman filters, or maybe just a “two-euro” filter might be enough. Having the data easily graph-able right in HA will be a good start for evaluating different methods. I like the idea that a Kalman filter gives a “predicted” result, which might be particularly useful for making a decision when a given packet is missing. I know it’s sort of a low-pass filter that’s required, but given the noise is additive rather than symmetrical and also I want to preserve responsiveness I feel that simple smoothing might be a poor fit.

5 Likes

Hello @agittins , I’ve installed your integration yesterday after several trial with other solutions (espresense, room-assistant, some own statistics sensors based on RSSI). All falling short with high variance of the rssi data. Your integration is also having same issues but it is way more elegant than the others and taking full advantage of ESPHome integration and new HA ble stack.

I’m using Mi Band 6 as beacon and, not only the rssi is erratic, but it also has period of times when it is not advertising (sometimes 2-3 minutes) and getting “away” status (unless increasing a lot the consider_home setting). But this is happening with all solutions, as it is likely a problem in the device (can someone confirm they have the same problem ?).

Other than that, I’m still not able to determine if Shelly are really working or not. By checking the logs of their aiot script, MI Band 6 MAC address is not appearing.

Anyway, a part from the testing, I welcome your active research in this topic, you can give a look to room-assistant spec and code, they were the most advanced and succesful in do statistical filtering of the data. I’ve tried myself to use a simple Mean to the data (60 seconds window), this increased the reliability of the location but decreased the speed of room change.

Good job|

Ok. Some more testing and I also dumped the data of your integration with the dedicated service (thanks).
I remembered I had a Mi Band 2 in a drawer and was able to charge it and integrate it in Bermuda. Shelly scanners are working fine with Bermuda (they do not get Mi Band 6 but they get Mi Band 2, the only difference is that Mi Band 6 is paired with my mobile, but using “hcitool lescan” and ESPHome it is appearing).

I have the impression that Bermuda takes the last advertisement and areas are changing constantly and sometimes surprisingly: I have a shelly in the bathroom 4m away from me and an ESPHome 50 cm away and area is constantly changing to the bathroom, this seems rather unexpected ).

Mi Band 2 is more stable for presence home/away: it is advertising constantly and it is scanned by Shelly; in my home I have 4 ESPHome and 4 Shelly evenly distibuted in the rooms.

I will try to use the dump to give you some more factual feedbacks.

Watching this project with interest! - I tried it out today not realising that it didn’t support the UUID’s published by the Android Companion app. So obviously it didn’t detect the device as I was expecting, it did show a bunch of MACs under the ‘Configure’ window for the integration, but none appeared to relate to my phones BT mac - so I assume it’s rotating them and I’d need a different app to publish a fixed MAC.

Anyway, looks very promising - for the moment I’ll continue using the FormatBLE Tracker but will be switching to this once UUID support is there!

Also - not sure if it’s intentional, but I noticed that the drop down box for selecting MACs on the integration has no scroll bar when it overflows the height of the page… took me a while to spot there were more MACs that weren’t visible, but I could view them using the arrow keys. :slight_smile:

Hi Andy, glad you’ve had some success at least, and good to get some feedback re where the other projects are at!

Making full use of the esphome / ha ble stack was a key desire for me, I didn’t want to have to dedicate any hardware just to the proximity stuff.

Glad you found the dump_devices service, I was going to suggest that for troubleshooting.

Yes, missed adverts is a key issue, as you’ve discovered :slight_smile: The algo currently is a bit too basic and switches too readily when the closest proxy misses a packet. My previous post above hints at what I have in mind for improving this on the software side. If I can smooth and estimate for missing adverts that might improve things greatly.

On the hardware/firmware side, a bit of experimenting with the interval and window parameters of esphome’s scan_parameters might help (I’m currently using 1000ms and 900ms respectively). The defaults of 320/30 regularly results in up to 10 second intervals between captured packets, which is pretty awful. The closer window and interval are to each other the higher percentage of time the esp is “listening” for packets - but the less time it has for keeping wifi alive (incl sending data to HA) and doing other tasks.

Interesting differences with the two Mi bands. Many devices will change how often they advertise (or stop adverts altogether) when they are actively connected. But given you are seeing differences between the shelley and the esphome I wonder if that’s more to do with the listening setup. I’d be quite interested to see the dump info (particularly the hist_interval part) from the shelly and esphome for anything you know that does 1s adverts, to see if it performs better than I’ve managed with the esphomes.

1 Like

Howdy Swifty,

Yes hopefully I’ll had UUID sorted before toooooo long :slight_smile:

Re the scrollbar, I’m just using the default selector tools provided by HA, but on my system at least I do see the scrollbars showing up:


(I’m running chrome on linux)
It could be some old cached css or something (a CTRL-F5 can sometimes solve that), or a particular browser incompatibility. You could try raising it with HA, but you might need to find a core integration that behaves the same way to find a way to reproduce it.

Ah interesting - I wonder if it’s a theme thing, as I’m using ‘Bubble’ - no particular weird browser or anything just Chrome on windows… will have a play and report if needed.

Really looking forward to watching the development on this - like you I think espresence is great but when I’ve already got esphome devices in every room (running LEDs, lux sensors etc) it annoys me to have to deploy another dedicated esp to each room just for the presence stuff :laughing:

2 Likes

Debugging is really a nightmare. You were right about Mi Band 6, I disconnected it from the smartphone and it started to advertise with the right frequence (and for some reason, shelly started to “see” it). But yesterday I stumbled on another problem one of the ESPHome scanners was not proxying anymore (maybe the BLE stack crashed), nothing was reported in the logs, restarting it, fixed the issue: maybe they are not coping with the workload of all the BLE devices. I’m still trying to find the right parameters for Bermuda. There are too many factors affecting the reliability of the “area” and “distance” sensors, but your integration provides for the most elegant “presence” and “room” architecture and I decided to stick to it

Same for me, chrome on windows

This seems interesting: Location Awareness of BLE Connections Using RSSI Monitoring | Novel Bits

Wow, nice find. Yes it’s a very cool idea, and would be useful for both Bermuda and for the HA bluetooth stack generally, as it would also allow connected devices to roam between proxies.

The downside is that I think what they’re describing is basically an API for their SoC firmware, so as a solution it would only apply to Silicon Labs chips.

Supporting this on an esp32 (or any chip) would require either that it was built-in already, or that the radio firmware allowed listening to all traffic, not just traffic destined for that device (ie, analogous to “promiscuous mode” for ethernet adaptors). I don’t know if the current bluetooth stack used in esphome (or for that matter the low-level firmware for the espressif chips) supports that. To support roaming it would also have to support switching the effective MAC address in order to do transparent hand-over (unless the BLE spec has some built-in method for that). I feel like the esp32 as it is already feels a bit “stretched” doing just the esphome ble_proxy work, but I don’t have enough knowledge to know either way.

It might be worth raising in the esphome camp, or tagging @bdraco to see what they think. I do think it would be a pretty major undertaking, but I like the concept.

1 Like

Debugging is really a nightmare.

Haha, yep! When I get the individual sensors per-proxy set up this sort of troubleshooting should be a lot easier and more intuitive.

One FR that would be on the structure side in ha and nothing actual tech related:

Re-use the existing device in home assistant when adding entities. I mentioned it before but am failing to find the right wording.

Found the dev docs on it: Device Registry | Home Assistant Developer Docs

Doing this would also allow for autodiscovery of compatible to-be-tracked devices and in future the device’s area could be automatically updated by Bermuda

Is there a current “best” bluetooth tag that anyone has found to work better than others with this integration and Bluetooth proxy in general?My current use case is trying to add inside/outside tracking for the dogs, so looking for something I could attach to a collar or similar. I have a couple of tile devices laying around, but they don’t seem to advertise too often.

I have some super-generic tag thingies, and they seem to reliably advertise at 2 second intervals.

The ones I bought look exactly like these but for some reason I can’t find the exact listing in my aliexpress history. I know that the precise chip used in these products will sometimes change from batch to batch, so I can’t guarantee how any given ones will work, but mine still seems to be broadcasting reliably, and without having to do anything odd like setting them up in an app or anything.

If your dogs are medium-to-large, I’d probably put a couple of tags on their collars for redundancy, and take care to design your automations on the basis that what you get from Bermuda is an affirmative indication of a maximum distance, not an affirmation of their absense.

That is, if bermuda says it saw the tag at a certain distance, you can be confident that it really did see it, and it really was that far or closer, more or less.

But if you don’t get a reading from Bermuda, it might be for any number of reasons - tag is too far away, tag is covered by sleeping dog mass, proxies failed to be listening at the exact moment the tag broadcast, etc.

Let me know if that doesn’t make any sense or if you’ve got questions about how to set things up differently for say, knowing where the dogs are versus getting an alert when they are (or are not) in an area. Basically though, the more proxies you have the more reliable your results should be.

1 Like