Polygon tracker Zones

I’d like this too, found this simple python code that works a treat. I have no idea how to make this a custom component or appdaemon app though

It may not serve everyone’s purpose but I created several overlapping zones with the same name for an area we visit from time to time, works well. Reports people as being in ‘The Woods’ regardless of which actual zone they’re in. You can make all sorts of shapes then.

2 Likes

Didn’t realise you could do that, I have just used your idea, thanks.

I tried creating a group with them in but it doesn’t seem to work

I’ve never tried creating a group - what additional functionality does that give you?

Was going to use it in automations but didn’t work. I can just list all the zones in the triggers I think

ah, OK. I don’t have any automations based on zones outside the house. I just use it to ‘spy’ on my son :slight_smile: and make sure he’s mostly, roughly where he’s supposed to be.

He is, of course, fully aware of that.

1 Like

As you can get lat and long coridinates from a device tracker you can write a sensor that detects if a device is between two lats and two longs and use that.
This is an area that is parallel to the equator though so not perfect but probably just a bit easier than multiple overlapping circles - But that is a neat idea @eggman I suppose they all add to the tools in our toolbox

:rofl:

Am I the only one who finds this a bit creepy?

:stuck_out_tongue_winking_eye:

Problem with a solution like that is not ‘inside/outside’, it’s the drawing on a map. Pretty sure that’s why a custom component doesn’t exist. Basically need a lot of backend and fronend work to support the shape visually before you can even get the ‘inside/outside’ working.

lol - he works with a volunteer group that manage local woodland, which the council would otherwise let go to rot. Some lovely paths, wildlife areas, gardens, streams kept clear of silt etc.

2 Likes

Creepy ?

Depends if he is also tracking device_tracker_vampire
who is also in the ‘woods’ and sun.sun is below_hozizon

:vampire:

1 Like

I might struggle

this works well for my needs

2 Likes

Yep, that’s not a rectangle let alone one parallel to the equator :rofl:

1 Like

I tried the python script above by drawing a polygon in Google earth and getting the vertex coordinates from there. Worked fine for the test I did. The script will say whether a coordinate is inside or outside the polygon.
Don’t think I’d need to draw or display the zone in home assistant if that’s the hard part.

I have been doing something very similar… and to boot it covers about 80 acres of woods as well.

1 Like

Can you share more details and how you integrated it into home assistant?

UPDATED

I’m in no way an expert in this, I’m sure there’s a better way.
You need appdaeamon installing.
I made this appdaemon app from a script I found online:

import hassapi as hass

class PolyZone(hass.Hass):

    def initialize(self):
        self.log("PolyZone Started")
        self.handle = self.listen_state(self.coord_changed, self.args["tracked_device"], attribute = "all")

    def coord_changed(self, entity, attribute, old, new, kwargs):
        self.run_in(self.point_in_poly, 0)

    def point_in_poly(self, kwargs):
        zone = str(self.args["poly_zone"]).lower()
        name = str(self.get_state(entity_id=self.args["tracked_device"], attribute="friendly_name"))
        entity = "binary_sensor." + str(self.get_state(entity_id=self.args["tracked_device"], attribute="entity_id").split(".")[1]) + "_polyzone_" + zone
        x = self.get_state(entity_id=self.args["tracked_device"], attribute="latitude")
        y = self.get_state(entity_id=self.args["tracked_device"], attribute="longitude")
        poly = (self.args["poly"])
        self.log(name + " Coordinates: "+str(x)+", "+str(y))

        # check if point is a vertex
        if (x,y) in poly: self.log("On a Poly Zone Vertex")

        # check if point is on a boundary
        for i in range(len(poly)):
            p1 = None
            p2 = None
            if i==0:
                p1 = poly[0]
                p2 = poly[1]
            else:
                p1 = poly[i-1]
                p2 = poly[i]
            if p1[1] == p2[1] and p1[1] == y and x > min(p1[0], p2[0]) and x < max(p1[0], p2[0]):
                self.log("On the Poly Zone Boundary")

        n = len(poly)
        inside = False

        p1x,p1y = poly[0]
        for i in range(n+1):
            p2x,p2y = poly[i % n]
            if y > min(p1y,p2y):
                if y <= max(p1y,p2y):
                    if x <= max(p1x,p2x):
                        if p1y != p2y:
                            xints = (y-p1y)*(p2x-p1x)/(p2y-p1y)+p1x
                        if p1x == p2x or x <= xints:
                            inside = not inside
            p1x,p1y = p2x,p2y

        if inside:
            self.log("Inside the Poly Zone")
            self.set_state(entity, state = 'on', friendly_name = name + " PolyZone " + zone.title())

        else:
            self.log("Outside the Poly Zone")
            self.set_state(entity, state = 'off', friendly_name = name + " PolyZone " + zone.title())
 

Add this to apps.yaml, insert all the vertex coords (get them from google earth). Make sure the last one is that same as the first one to create a closed loop:

poly_zone_home:
  module: poly_zone
  class: PolyZone
  tracked_device: device_tracker.xxxxxx_phone
  poly_zone: Home
  poly: [[53.xxxxxx, -0.xxxxxxx], [53.xxxxxx, -0.xxxxxxx],[53.xxxxxx, -0.xxxxxxx],
        [53.xxxxxx, -0.xxxxxxx], [53.xxxxxx, -0.xxxxxxx], [53.xxxxxx, -0.xxxxxxx],
        [53.xxxxxx, -0.xxxxxxx]]

A binary sensor will be created to show whether the configured device tracker is within the zone or not

2 Likes

Bumping this as I’d really like it, and I’m afraid the idea that @Holdestmade provided isn’t working for me on the latest version of HA. I’ve double and triple checked that I have all entities and files created and named appropriately, etc. and yet I never see anything more than

2020-12-20 20:53:04.754377 INFO poly_zone_work: PolyZone Started

in the AppDaemon log (so the state of the input boolean never changes and is therefore useless). Fingers crossed something like this becomes easier to do someday!

I too would like this feature. Sure a circle is easy to capture and check, but poly lines aren’t that much harder. Someone did it for you a few comments up, but we need something integrated into HA, not a hack.

Still working fine for me on latest version of ha and appdaemon.