Page 1 of 2

Geofencing

Posted: 14 Dec 2017 14:15
by Scott12
I'm trying to set up a set of flows for highly accurate geofencing. I have several flows that use the Location trigger for various geofencing applications, but I want a more robust solution for my home geofence. Here's the structure I'm using currently:

Code: Select all

ENTERING:
1)	"Geofence Service Boundary" Trigger - Location: entering Home (r: 1500)
	a)	Execute Flow: “Location Scan” Trigger: Periodic Location Update / High Accuracy / Frequency: 5s
2a)	"Geofence Arrival Boundary" Trigger - Location: entering Home (r: 120)
	a)	Stop Flow: “Location Scan”
	b)	Script: global_me_home = true
2b)	“Home WiFi Connect” Trigger: WiFi Connected (your home WiFi SSIDs)
	a)	Stop Flow: “Location Scan”
	b)	Script: global_me_home = true

EXITING:
1)	“Home WiFi Disconnect” Trigger: WiFi Disconnected (your home WiFi SSIDs)
	a)	Execute Flow: “Location Scan” Trigger: Periodic Location Update / High Accuracy / Frequency: 5s
2)	“Geofence Departure Boundary” Trigger - Location: exiting Home (r: 400)
	a)	If WiFi NOT Connected (your home WiFi SSIDs)
		(1)	Script: global_me_home = false
3)	"Geofence Service Boundary" Trigger - Location: exiting Home (r: 1500)
	a)	Stop Flow: “Location Scan”
So to put that into English, when the normal location trigger hits around 1500 meters, I start the Periodic Location Update running every 5s, so I get a more constant update. Then if I go inside 120m, or if my home WiFi connects, I stop the Periodic Location Update and set myself as AT HOME.

When leaving, I start the Periodic Location Update when my WiFi disconnects. Then when I leave a 400m radius, I set myself to NOT HOME, and at 1500m I stop the Periodic Location Update.

My biggest question is, am I getting the benefit from the Periodic Location Update that I think I am? As I understand it, the Location trigger only updates roughly every 4 minutes. My assumption is that, by running the Periodic Location Update with a 5s frequency, I'm force-feeding the Location trigger updates, and therefore the Location triggers should fire within a few seconds of me crossing their border, rather than within a few minutes of doing so.

My intent is to use this to control some of my home automation (SmartThings based) stuff based on arriving and leaving home, which is why I want the more precise geofencing here. Am I understanding this behavior correctly? Does anyone have a better approach for this?

Re: Geofencing

Posted: 14 Dec 2017 16:05
by Desmanto
I actually also wanna know the best practice of geofencing. So far, the solution is similar to yours, using periodic location update, but added condition to activate the flow only in certain range of time where we are most likely to go to home.
But for those who don't go home on a fixed schedule, the time range will be too long. And using periodic location update will consume a significant amount of battery, which is one of the most precious resource we wanna conserve. The tradeoff always need to be balanced, accuracy vs battery life.

I have thought about this several times. The best I can think for entering is to put a wifi AP near the gate/front door. This Wifi AP acts just like a beacon to mark that you have arrived home. Leave the phone wifi turned on even though it is not connected to anything. When you approach the front door, your phone will connected to the AP and immediately trigger the flow; activating all sort of smarthome things you need. For exiting, just the same as yours now, but not need to use 5s frequency. Just use 1 minute or more (to conserve battery). 1-5 minutes latency to turn off the smarthome things is acceptable. You don't want mis-triggering the flow when you are at home, and turn off all of the lamp accidentally.

Re: Geofencing

Posted: 14 Dec 2017 21:12
by Scott12
@Desmanto, thanks for the feedback. Using time-windows doesn't work for me, but I do think I'll need to reduce the radius on my 1500m Service Boundary, because there are a couple places I go somewhat frequently (restaurant, hardware store) that are inside that area. I'd be running the Location Scan the whole time I'm at those places. But of course, if I shrink it too far, I'll end up hitting the 120m Arrival Boundary without the 5s scan ever running. So I guess the question is, is there any reason to bother with the location part when ENTERING? My WiFi typically connects before I get into the house, so maybe I should just use that as the arrival key by itself, skipping the Service & Arrival Boundary steps. I'll have to give that some more thought.

I think the gated exit is important, because you don't want arrival events to refire any time your WiFi disconnects & reconnects, or when your phone inexplicably thinks it's 500m away from where it is. But I should be protected from those cases with only setting myself Not Home (global_me_home = false) when WiFi is disconnected AND I've exited the Departure Boundary. For that, like you said, running the Location Scan on exit should be fine with a 1 minute+ frequency.


@Martin, can you offer any insight into the Location and Periodic Location Update triggers? Does the method of running the Periodic Location Update on a 5s frequency do what I think, and force the Location trigger to update every 5 seconds, or am I just wasting battery? Is there a better approach we haven't thought of? Thanks.

Re: Geofencing

Posted: 14 Dec 2017 23:00
by Scotty
I have similar flows, so I thought I'd make a few comments about how I employ them, in case it helps anyone.

1. I don't concern myself about the battery, because whenever I am approaching or leaving my home, I am almost invariably in my car, and my phone is in the wireless charger.

2. Usually, I drive home listening to a podcast. GPS & mobile data are on, WiFi is off.

3. When the GPS detects that I've entered my general home area, a flow is activated that sends an SMS ("Almost Home") to my wife, turns on WiFi, and begins to scan for neighbors' WiFi SSIDs. I don't CONNECT to them (I don't have the PWs) - I merely scan for their presence.

4. As soon as a neighbors' WiFi is detected, the flow checks to see if I'm listening to a podcast; if I am, it turns it off.

5. At this point, I'm entering my driveway, so now the WiFi scan is limited to my own SSIDs. When they're detected, the flow turns off GPS & mobile data, and does a few other arrival-related things.

Leaving home, A "Drive mode" flow turns on. Every 5 seconds, it checks for a connection to my WiFi. When that is lost, it turns WiFi off, turns GPS & mobile data on, turns on a custom speedometer (also automagic-based), sets screen timeout to never, enable flows to auto-reply to incoming SMS or Gmail sent by my family (other people can wait :) ) and sets screen brightness based on the daylight status (using the Plugin "Twilight"). It also places some icons in the status bar, indicating that these things have been activated.

Re: Geofencing

Posted: 15 Dec 2017 14:26
by Desmanto
@Scott12 : For entering, one thing that surely will happen is you are connected to your home wifi. So just use that single trigger as the main one. Of course that assume your wifi is always on and never fail. And if the wifi fail, I think your flow will be disrupted as well, so most likely ENTERING with wifi is preferred.

For flopping signal (wifi disconnect/reconnect when still at home), you can always protect your entering flow. As soon as the wifi is connected and all of the ENTERING flow has been successfully executed, disable all of them. Your entering flows won't triggered the second time until they are enabled back by the exiting flows.

And EXITING flow can't use wifi disconnected, as it can happen when you roam around the house. It should use the same Location scanning you already used. Just run the flow less frequently (1 minute or more). If there is something that is definitely happen when you leave the house, you can use that one as the trigger.

You can use Periodic time with time range too to reduce triggering the EXITING flow. So when you disconnect from the wifi, it will enable this periodic timer flow, probably 5 or 10 minutes. It checks for the wifi again during the time range you are most likely won't be at home (let's say 8 am - 5 pm). If home wifi is not detected, then enable the location scanning flow. If not, do nothing and repeat the check after the period time. If you reconnect to the wifi again, disable this flow again, so it won't check anything even if you stay home at 10 am while connected to home wifi. So this wifi disconnect/connect flow will be separated from the main entering flow.

This will protect your exiting and make sure it happens only during certain time range. You location flow exiting will be only enabled when you are in the time range you are most likely not at home and when you leave the house (not connected to wifi). And if after enabled, the location flow shows you are not at home, then do all the EXITING flow and enable back all ENTERING flow (so it can trigger again after we are returning home).

You will have a lot of flows to deal with this geofencing. But It worths the accuracy and battery life. So far, I only use ENTERING flow, to detect my home wifi. I have several smarthome things already, but not yet utilize them to fullest. I am thinking about bluetooth tag too viewtopic.php?f=5&t=6883 Or maybe use NFC (my phone doesn't have nfc :( ) or even strong magnet neodymium near the table where I usually put my phone on.

@Scotty : Nice idea, neighbor's wifi will be much more accurate in most cases. That's the perk of having unlimited battery life. Accuracy is the only things to concern about.

Re: Geofencing

Posted: 26 Jan 2018 21:25
by hoods
@Scott12: could you share your flow with us? I'm an automagic rookie but would like to setup a presence check in a similar way. Learning from an existing flow would help a lot.

Thanks in advance, Sven

Re: Geofencing

Posted: 27 Jan 2018 08:35
by xdauser
There is one more way to determine if you are at home or not. Use Bluetooth - by using Bluetooth LE beacon or Bluetooth equipped device like TV, speakers. This method has a advantage of drawing small amount energy from battery. You can leave Bluetooth to be switched on all the time and just checking if certain named Bluetooth device is in range.

Re: Geofencing

Posted: 06 Feb 2018 21:54
by Scott12
@hoods, it's a bit complicated, because I have half a dozen flows, plus others that they call, but I'll give a summary here of what I'm using currently (it's been simplified somewhat since my 1st post here). I have a Samsung SmartThings system at home, and I update my "presence" for that with this process. If you want more detail on the SmartThings steps, let me know & I'll flesh those out a bit more.

Flow1: "Arrive Me"
  1. No Trigger
  2. Condition: if variable global_my_presence is not "present"
  3. Plugin: A Thing
    1. Device Type: Simulate Presence Sensor
    2. Device: Scott_Home (this is a thing I created)
    3. Command: arrived
  4. Script: global_my_presence = "present";
  5. Action: Execute Flows - "Enter Known Location" (this flow sets various things such as volume levels, screen timeout, etc, based on my location)
Flow2: "Depart Me"
  1. No Trigger
  2. Condition: if variable global_me_home is TRUE
  3. Plugin: A Thing
    1. Device Type: Simulate Presence Sensor
    2. Device: Scott_Home
    3. Command: departed
  4. Script: global_my_presence = "not present";
  5. Action: Execute Flows - "Reset Location Profile" (similar to Enter Known Location)
Flow3: "Geofence Arrival Boundary"
  1. Trigger: Location - Entering Home Area - 120m radius
  2. Script: global_me_home = true;
  3. Action: Execute Flows - "Arrive Me"
Flow4: "Geofence Departure Boundary"
  1. Trigger: Location - Exiting Home Area - 400m radius
  2. Script: global_me_home = false;
  3. Condition: WiFi Connected - only continue if my home WiFi SSIDs are not connected
  4. Action: Execute Flows - "Depart Me"
Flow5: "Home WiFi Connect"
  1. Trigger: WiFi Connected (only include home WiFi SSIDs)
  2. Script: global_me_home = true;
  3. Action: Execute Flows - "Arrive Me"
Flow6: "Home WiFi Disconnect"
  1. Trigger: WiFi Disconnected (only include home WiFi SSIDs)
  2. Script: global_me_home = false;
  3. Condition: Location - Home with 400m radius, check the "Accept Every Update: Inside" box
  4. If above condition is FALSE, then...
  5. Action: Execute Flows - "Depart Me"
So the idea is, I consider myself home if EITHER my WiFi connects, OR I'm within 120m of home. I consider myself departed only when my WiFi is disconnected AND I'm more than 400m from home. I don't want my presence to bounce if my WiFi cuts out for a minute or my phone's GPS freaks out (which happens from time to time). I don't have this controlling access (door locks etc.) yet - when I get a connected lock (which is on my shopping list :D), I may have to adjust a bit.

Re: Geofencing

Posted: 13 Feb 2018 07:00
by Xamgin
I am not sure what geofencing is about???

Re: Geofencing

Posted: 14 Feb 2018 14:45
by Scott12
Basically it's a way to identify when you've entered or exited an area.
https://en.wikipedia.org/wiki/Geo-fence

For the discussion here, it's a way, using your mobile phone, to identify when you've entered or exited your home (really, the immediate area around your home). You're setting up a digital "fence", and when your phone passes inside that fence, it marks you as being home. When your phone passes outside the fence, it marks you as being away.

There's a lot of potential uses. If you have kids who have phones, you could use this to get a notification when they get home from school. You could also set up a fence around their school, and monitor them arriving/leaving there. If you have a smart-home setup, you can have it turn on lights when you get home, turn down the thermostat when you leave, and even unlock the door for you if you want (assuming you have all that stuff connected).