WTH we only have round shaped zones?

I think we should be able to set zones that are square shaped, rectangular shaped… Or even be able to set streets as zones, so they can be used for automations.

100% agree. Defined zones (point and click to create a boarder) would be fantastic. Especially for much larger zones to fit awkward shaped areas without encroaching on others, or needing to create multiple for the same area. Such as lakes, beaches, or city limits.

3 Likes

I guess this is related to a limitation on the battery-saving features of mobile phones. I guess phones can easily notify apps if the location is somewhere around a point; but doing the same with arbitrary polygons can be more difficult. Still, I don’t know, and I could be completely wrong here.

4 Likes

I also might be wrong, but surely the phone just sends it’s GPS coordinates and HA works out if it is in that zone or not?

Maybe someone who knows more about the inner workings of the mobile app can shed some light on it.

Absolutely! We should be able to make different shaped zones.

I use a simple appdaemon app to use polygon zones, would be great to have this integrated

https://community.home-assistant.io/t/zones-that-are-not-circles/175914/25

I think there was a feature request for this also. I also would like to see polygon zones. My use case is tracking which yard the automower is in. I currently accomplish this through the integration, but it should be done in Home Assistant.

map_camera_zone

1 Like

Point and click defined polygon shaped zones would be a game changer.
I would also be insanely happy with a “general zone” which maintains the functionality we currently have, and a “precise zone” which implements this new functionality.

4 Likes

I was thinking it would be good to choose which entities are tracked to the polygonal zones level. I don’t need to know if my phone is in the front yard, just that it’s home. I really only want specific entities, in my case the mower to be compared against the polygonal zones.

2 Likes

Mobile phones only support geofencing with circular zones, and geofencing is what ensures your phone wakes up and sends back a location update. Supporting polygonal zones for mobile phones would basically require the phone to send location updates much more frequently draining battery, rather than send them periodically, and when the system geofence feature triggers.

That said, for any devices that do report longitude and latitude frequently, then conceptually home assistant could be upgraded to handle polygonal zones. The issue then becomes though that one of the most common uses for zones (tracking phones, tablets, and similar devices) won’t work very well with such zones.

1 Like

This will not work well with the rest of services that use zones like the Android app. Most geofencing apps take the zone radius. You can’t do that in another shape properly if you want accurate zone tracking.

1 Like

it was annoying me too, so I made it myself.

  1. I added new zone integration called polygon_zone with additional property points
  2. used it with shapely
  3. hacked zone component methods async_active_zone and in_zone, so if it has points then
    I am checking if latitude and longitude are within polygon:
from shapely.geometry import Point, Polygon

(...)

point = Point(latitude, longitude)

if 'points' in zone.attributes:
  polygon = Polygon(zone.attributes['points'])
  if polygon.contains(point):
    return zone
else:
   # use original function

hacking part:

from homeassistant import components

(...)

components.zone.async_active_zone = polygon_async_active_zone
components.zone.in_zone = polygon_in_zone

(...)

as frontend I used leaflet and @geoman-io/leaflet-geoman-free

And it works much much better than zones with radius. No more issues with triggering zones enter/leave because road is nearby parking, or overlapping zones etc.

Overriding original methods allows automation triggers to work correctly, and original zones are still supported.

WTH moment: why zone trigger do not have time field “for 5 minutes” or something like that?

12 Likes

Try and add it to GitHub and see if they can adopt it in native HA

1 Like

I would like to implement this as well but your description is a bit thin. I’m not sure where to even start. 10 lines of code tucked in somewhere? How about a quick YouTube video walking through what you did? Has anyone else accomplished this?

Here is my implementation, it is really messy, but it works.

https://github.com/Baael/polygon-zone

I will try to find out where I lost frontend editor files. I need it now, but I migrated my workstation and I have now a mess with old repos.

In short:

  • I created a new class called polygon_zone that inherits from zone.
  • I added an attribute named points.
  • If the zone has points, then it’s a polygon_zone; otherwise, I use the old class, so I don’t have issues with backward compatibility.
  • I determine the center of the zone and its area.
  • In the _init_.py file, in the in_zone method, I check if the instance has the points attribute. If it does, I check if the tracker’s point is within the polygon using polygon.contains(point) . This is on lines 97 to 104.
  • Otherwise, I check if the point is within the radius.

Geoman demo: https://geoman.io/demo

And this is how zone attributes looks like:

latitude: 49.96778200818155
longitude: 18.950865372705703
radius: 0
passive: false
persons: 
editable: true
points: 
- - 49.96801773903324
  - 18.95048797130585
- - 49.9676346544119
  - 18.9503887295723
- - 49.96754837367113
  - 18.95126044750214
- - 49.96793176283414
  - 18.951327502727512

icon: mdi:map-marker
friendly_name: Intermarche

latitude and longitude are polygon centroid
points are array of [lat,lng] points

5 Likes

Awesome work on this! I’ve been looking for a solution like this for some time now.

I installed your custom component using HACS - however, using your zone attributes example, I’m unsure how to set up a zone using YAML as zones appear to be stored in .storage/zone, which has JSON.

Have you been able to find the frontend editor files?

I get that, but maybe being able to set one zone made from several overlapping circle zones would be an acceptable workaround?

I was searching for a similar functionality and I found your answer.
I was thinking about using the same library.
What do you think about adding architectural discussion with the proposed solution?
Maybe a new field that would define the type should be added, so the backend and front would know how to calculate in zone and how to display the editor.

I’ve created PR: Allow zone to be a polygon by Misiu · Pull Request #105729 · home-assistant/core · GitHub that is based on @wojciech6789 code.
Reviews are welcome :slight_smile:

4 Likes

Would love this, there’s a few things in my city which I enter purely because I can’t cut out the roads them