I have some very functional, very responsive automations that are triggered by (or conditionally controlled by) device location using the HA iOS app.
I’m curious if anyone has come up with a reliable way to determine when two users are together. I think the biggest hurdle is that the devices won’t consistently update at the same time, so the devices may appear to be far apart depending on travel speed, update frequency, etc. Updates occur when entering & leaving significant locations, but there are a lot more other locations than there are zones.
Seeking some inspiration here. Using some precision of GPS coordinates along with time delays might yield something workable, but I think it would take a lot of trial & error. If anyone has done anything like this, I’m interested to learn more.
Ahh yes, thanks. I had a similarly-configured template sensor quite awhile ago. I don’t think I got it from a LinkedIn article, but it was probably the same source. It’s vulnerable to the same update issue I mentioned above… and there may just be no way around it without setting an unreasonably-short update frequency.
But I’ll set up this sensor again and see how it goes.
EDIT: Funny, I have several of these in my templates.yaml file that are commented out. I’m pretty sure I did that last year sometime because they just weren’t meaningful.
OK, I spent some time with this last night. Ultimately, what I’m looking for is a binary sensor that is true/on when two people are together, and off when not. The trouble I was having with the proximity-distance approach is that the automations that are triggered or allowed to run based on distance were inconsistent because of timing: if an automation occurs when one person enters a zone already occupied by another person, well, it’s because the distance has dropped to 0 (or close to it) and maybe some of the automations run, and some don’t… I could likely account for all of it with delays buried in the automations, but I’d rather just set up a reliable binary sensor as defined above.
So my binary sensor should turn on 5 minutes after distance drops below 0.1, correct? It appears to. However, that sensor doesn’t remain true despite the distance staying below 0.1:
The problem obviously lies in my binary sensor. Aside from the toggling all night long when distance remained unchanged at 0, when Person2 left at 8:30-ish this morning (red line), the binary sensor had already turned off again just a few seconds prior. The data shows it better than the visual of the chart.
I want the binary to go true after the defined delay (might tune that later), and go false immediately when defined distance threshold is exceeded. I’ve commented out delay_off to see if that helps… though I think a delay of 0 does nothing. I’m open to ideas on improving this. I think I’m close.
Looks like you have multiple binary sensors, because the one you graph has a name different then the yaml you show. It has the person 1 and 2 reversed.
Ohh–on the chart, I added the names after the fact to obscure the actual sensor names. Pay no attention to that. I have one distance sensor and one binary sensor.
I’ve been running this updated template for the last few hours. It’s accurate when the two people are not together; let’s see what happens when they are:
The idea behind the template delay is that if one person enters the home zone, the location can update more reliably than in other areas. Entering the home zone (or any zone) seems to update more quickly. That might be complicated for no reason… we’ll see. I could probably also just use a not-away on the same sensors so that it updates for any zone.
Due to the scale it is hard to tell if the distance was above .1
While debugging, I’d remove it until you know what happens if it works as expected. Then you can decide based on actual data what would be a good value.
That togging in the first 30 minutes or so was when the delay was still part of the state definition. I don’t know why that would cause toggling… but in any case, this works. I may play with the on delay. I need an on delay to ensure the two devices update within the same window.
Thanks all for sharing the nice concepts for determining if two persons are together.
Because speed matters, I added that.
With one update per 1 minutes, travelling 60km/h the distance can change 1km. So the max inter distance needs to be adapted by the speed. I divide the max speed by 30 for some extra margin. So the adapted solution, where I merged the two sensor into one sensor, takes the speed into account:
- binary_sensor:
- unique_id: john_is_with_jane
name: John is with Jane
icon: mdi:account-supervisor-outline
state: >
{% set person1='person.john' %}
{% set person2='person.jane' %}
{% set interdist=distance(person1, person2) | float(0) %}
{% set source1=state_attr(person1,'source') or 'null' %}
{% set speed1=state_attr(source1,'speed') or 0 %}
{% set source2=state_attr(person2,'source') or 'null' %}
{% set speed2=state_attr(source2,'speed') or 0 %}
{{ interdist < 0.1+max(speed1,speed2)/30 }}
# shorter time for home zone as it updates faster
delay_off: >
{%- if is_state('person.john', 'home') or is_state('person.jane', 'home') -%}
00:02:00
{%- else -%}
00:10:00
{%- endif -%}
Note: will publish a trigger based version when done some testing.
In that version, the max distance is calculated by time between two updates x speed
I experimented a few weeks with a trigger based version, that was taking the traveling distance into account to determine if two persons are traveling together in a car.
Long story short. It is not feasible to get that reliable, without mapping on a real map with roads. Reason, my wife owns an android, and I own an iPhone.
The android updates every minute at high speeds, the iPhone typically once per 7 minutes. The margin for 7 minutes gets really large
I found out that the positions in the iPhone updates can be a few minutes delayed when received.
Here a graph while traveling together in a car (blue iPhone, Yellow android):
Until very recently, I was doing this with 100% accuracy using Bluetooth iBeacon transmission on one phone, and Bluetooth Monitoring on the other phone. The distance sensor itself was irrelevant because the main requirement was whether any signal was being received, as Bluetooth has a reasonably constant finite distance.
I say “until recently” because the Bluetooth Monitoring phone had significant battery drain issues - BLE monitoring requires activating the listener for a period of time on a schedule, in order to capture interesting packets. The duration is unfortunately long enough and frequent to drain even a modern phone within a business day.
I plan to try adjusting the settings to see if I can achieve what I need without too much drain, but I don’t think there are sufficient setting options in the Android companion app to mitigate. There are lots of settings for the Beacon side, but not the Monitoring side. Swapping out to a BLE tag of some sort obviously doesn’t help, as the Beacon side is not the issue.
The ideal solution I can see is for HA to expand the Proximity integration so that it can have another Person or Device chosen as the “Zone to track distance to” setting. Assuming that HA is using the longitude/latitude coordinates of the zone then using the long/lat coords of another device instead would be trivial - it’s not, as they somehow calculate the edge of the zone allowing for radius however I am sure it would still be achievable.
I have not checked if there is a current and formal feature request for this.