Overlapping Zones

The wife asked for a way to set up an alert when one of us is home and the other is a few miles out from home. Now that I am typing this out I am realizing a way to do this could be with the the Google Travel Time component. But I am going to present my other attempted solution since it has some odd behavior.

The way I tried to do this was via zones. I created a zone using the lat/long of my home and a radius of 5300. This amounts to about a 10 to 12 minute drive to my house from all directions. I then set an alert for when one of us enters that zone to be sent via Pushbullet. I’m pretty sure this was actually working fairly well at one point. But it was a long time ago.

Now what happens is somewhat odd and I can’t figure out why it happens or how to fix it. Now when either of us enters the “Close to Home” zone, that person is detected as being at Home. Then a couple minutes later they show as Away. Then when they enter the actual “Home” zone; it switches to Home again.

It is almost as though HA sees the “Close to Home” zone (it shows up as a zone in the dev tools states display)… and then completely ignores it. I get no “close to home” notifications. I originally thought it was because I was using the same exact lat/long as Home. So first I knocked a couple decimals off of the coordinates in my C2H zone and that didn’t change anything. So then I moved the coordinates down the street a bit so the coordinates were even more different. Same behavior.

So I guess my question is, are overlapping zones not supported or am I doing something stupid here? I tend to assume the second option but who knows, even a broken watch and all that.

I’m currently at work and don’t have access to my configs at the moment, but when I do I will post up the relevant portions of code here.

Figure today is as good a day as any to actually remember to post the code portions.

Closetohome.yaml:

- name: Close
  latitude: !secret home_lat
  longitude: !secret home_long
  radius: 5300
  passive: true

almosthomenotify.yaml:

- name: Close
  latitude: !secret home_lat
  longitude: !secret home_long
  radius: 5300
  passive: true

Here is a portion of the logbook when my wife was driving home from work the other day:

So what I am seeing is when my wife enters the “Close to Home” zone, HA seems to think she is actually entering the home zone. It then immediately sees that she is actually not home and sets her back to away. And then about 10 minutes later (I really nailed that radius ::self pat on back:: ) she actually gets home and her state is properly set.

Anyone have any idea on why this behavior is happening?

I’m also wondering about overlapping zones - did you figure anything out? I am wondering about setting up a zone for “school”, and a smaller zone within my “school” zone for “library”.

Unfortunately, no I haven’t. I’ve basically just given up on figuring it out and just consider it a quirk that I’ll deal with.

Looking at the source code (dev/homeassistant/components/zone.py) it looks like an attempt has been made in async_active_zone to define the active zone as the zone you are closest to the centre of (so two overlapping zones which are not concentric will pick the zone you are closer to the centre of) but there is an extra condition for concentric zones so that the active zone will be the smallest zone.

Where there are two non-overlapping zones where you are exactly equidistant from both (unlikely given these distances are floating point numbers), the smaller zone is used). If you are equidistant from two zones that are the same size, then the one with the lower id will be used.

The in_zone function just returns true if you are within the radius of the zone, so you can be in several zones at once, but there is just one active zone.

2 Likes

@oembob and @Cade_dopp It might be easier and more versatile to use a distance template trigger. Here is an automation I have that looks at all device trackers, and turns off the lights once the closest one of us is at least half a mile away. Yours could just remove the “closest” portion as you want it to fire for anyone approaching, and change the greater than symbol to less than.

automation 14:
  alias: Lights Off Far Away
  trigger:
	platform: template
	value_template: "{{ distance(closest(states.device_tracker)) | float > 0.5 }}"
  action: 
	- service: light.turn_off
	  entity_id: group.interior_lights
	- service: media_player.turn_off

Also, you could use templating to determine the target for the notification, like this notification that welcomes me home and provides me an action to unlock the door. You could also use a template in your message to specify who is arriving home in the notification body.

automation 18:
  - alias: Welcome Home Notification
	trigger:
	  platform: state
	  entity_id: device_tracker.wife_wife, device_tracker.me_me
	  from: 'not_home'
	  to: 'home'
	action:
	  - service: notify.html5
		data_template:
		  message: "Welcome Home"
		  title: "Welcome home"
		  target: >
			{% if trigger.entity_id == 'device_tracker.me_me' %}
			  MePhone
			{% elif trigger.entity_id == 'device_tracker.wife_wife' %}
			  WifePhone
			{% endif %}
		  data:
			#tag: "door_unlock"
			actions:
			  - action: unlock_door
				title: "Unlock Door"

Edit: didn’t notice that this thread is a year old… but I still stand by my recommendations. :laughing:

5 Likes

I’ve been using a trigger essentially like this, and the problem with it - unless I’m misunderstanding something (a real possibility) - is timing. My location is tracked with GPSLogger, which (for battery life) is set to log every three minutes. That means I’m not detected as being home until (on average) 1.5 minutes after I arrive. I’m using this for a driveway light, and 1.5 minutes is a surprisingly long time to sit in the dark!

I’m still looking for a better way.

1 Like

Does GPSlogger have settings for a distance based update too? I’m using that with Owntracks. It updates only every 15 minutes (I specified that), but also any time I move more than 100 meters.

Have you considered the proximity sensor?

You can use that to trigger an action when you’re within a chosen distance, and heading towards home.

2 Likes

I’m finding proximity sensor very confusing (and hard to test); I’m apparently not thinking the same way the document writers were.

What two entities does it use for proximity? If I’ve defined multiple zones - say home and work - and I want to measure my distance (my phone’s distance) from home, should this work?

proximity:
home:
ignored_zones:
- Work
devices:
- device_tracker.myphone
tolerance: 50
unit_of_measurement: mi

Fundamentally I don’t understand “ignored_zones.” If I didn’t identify any ignored_zones above, would it just tell me I was x miles from whichever zone was closest, home or work? That isn’t making sense to me, and as I say, testing is difficult because it requires me to drive around…

Thanks,

GPSlogger has similar features: You can tell it to ignore (don’t upload) any points where movement was less than X, but it still determines one’s location every Y minutes. So to improve my situation I’d have to shorten my interval to however long I’m willing to sit in the driveway, which I think would be a battery hit. I might try it and see how bad it is; my battery doesn’t last all that long anyway, and maybe the GPS isn’t a heavy hitter by itself.

The google_travel_time sensor looks kinda cool; I think I could use it to actuate when I was XX minutes from home, and (in theory) it would consider traffic and everything. I’m not sure that solves my light problem, but it might help.

Thanks.

You could also use the nmap device tracker in addition to GPSLogger. I didn’t have any luck with it, but that may have been fault of my own, or improved now.

If you use the google_travel_time sensor, you might have good luck (I find it very reliable), but I doubt you’ll be able to get the timing perfect unless you target something like turning the lights on 5 to 10 minutes before you get home. Perhaps you could combine it with the proximity sensor to make sure that you’re headed towards home before firing the automation.

Here’s one of mine:

proximity:
  person3_home:
    zone: home
    devices:
      - device_tracker.person3_mobile
    unit_of_measurement: m

That’s all you need. This one provides the number of meters from home, and I use it like this:

automation:
  - alias: 'person3 coming home'
    initial_state: 'on'
    trigger:
      - platform: numeric_state
        entity_id: proximity.person3_home
        below: 701
    condition:
      condition: and
      conditions:
        - condition: template
          value_template: '{{ states.proximity.person3_home.attributes.dir_of_travel == "towards"  }}'
    ...
1 Like

Thanks, that does seem to be working… but it also seems to be something I’d previously tried. I wonder if I had a naming conflict or some other stupid mistake?

Now after a day running errands, the proximity sensor is working properly, but I have had to tinker with the configuration of GPSLogger on my phone. Last evening, when I was sitting at home, i got two spurious actuations that looked like this (I’m using Appdaemon with a test function that just logs):

2018-01-26 22:50:24.805717 INFO PatioLightOnWhenTimGetsHome: Tim is 972 meters from home, moving away.
2018-01-26 22:52:32.883011 INFO PatioLightOnWhenTimGetsHome: Tim is 2 meters from home, moving towards.
2018-01-26 22:57:42.012964 INFO PatioLightOnWhenTimGetsHome: Tim is 972 meters from home, moving  away.
2018-01-26 23:07:49.622730 INFO PatioLightOnWhenTimGetsHome: Tim is 7 meters from home, moving towards.

I read a little about GPS signal multi-pathing, but that seemed to be more about a few tens of meters, not hundreds. And the repeated “972” was interesting.

I couldn’t find much about setting up GPSLogger; it has options about what methods to use for determining location (network, GPS, “passive” meaning some other phone function asked). a distance filter, and an accuracy filter. I had it set to get the location either from the network or from the GPS; I wondered if the 972 meters happened when it couldn’t get one of them and used the other. So, I turned off “network” and “passive,” leaving only “GPS” as the source of location, and it seems to be OK.

I also set the distance filter to 250 meters and the upload interval to 2 minutes. I’m really only interested in this working when I’m driving (if I’m walking I’ll turn the light on before I leave, or manually), so it seems likely I’ll move at least 250 m in 2 minutes…

In short: Thanks, and maybe I’ve got this working. nmap is sounding better (though it takes a bit for my phone to connect, and I have Google WiFi and haven’t seen examples for that).

1 Like