One automation to notify when occupants enter or leaves any zones

I have created 10 zones and I have 4 occupants being tracked by Owntrack.

I want to create an automation where it will send a notification to me whenever any of the occupants leave or enter any of the zones.

Right now, this is what I did…

- alias: 'Occupant 1 Enters Zone 1 Alert'
  trigger:
    platform: zone
    entity_id: device_tracker.mqtt_occupant1
    zone: zone.zone1
    event: enter
  action:
    - service: notify.notify
      data_template:
        message: >
          {{ trigger.to_state.attributes.friendly_name }} has arrived at zone 1.
        title: ""

but this is only 1 of what I want. I still want…

  • Occupant 1 Leaves Zone 1 Alert
  • Occupant 1 Enters Zone 2 Alert
  • Occupant 1 Leaves Zone 2 Alert
  • Occupant 1 Enters Zone 3 Alert
  • Occupant 1 Leaves Zone 3 Alert

…and so on.

For my case, it will take up to 80 automation to achieve what I want.

I am wondering is there anymore elegant way to do this?

I don’t use the zone platform - so I expect that someone can recommend some platform-specific finessing, but don’t forget that entity_id can accept a list. So list all 4 occupants there and you’re down to 20 automations from 80.

with smartphones?

p.s. check zanzito

yes you are right. but 20 is still a lot. It will be even more when I more zones.[quote=“anon35356645, post:3, topic:16441”]
p.s. check zanzito
[/quote]

I’m using it to replace owntrack. but it doesn’t help in what I want.

I was meaning that.

WHat do you use as detectors? YOu have 10 detectors for the 10 zones?

I see. But the goal of this thread is how to shrink all the zones and occupants into one automation.

The phones with zanzito (emulate owntrack) are the detector.

mmhh maybe I am missing something.

There is the area (room/spot/garden/etc) and there is the person (which could be identified as general human or a specific person). How you identify the area and how do you identify the human (or person)?

You must create the zones that you want to track. To create zone, please refer here.

After that, your devices (phones) that you want to track must install Owntrack (or Zanzito) or iCloud so that your phone GPS coordinates will be publish to the MQTT topics that HA is also listening to.

When the phone enters or leaves the zone that you created, it will trigger the automation.

SO basically is only GPS based? I got it, I thought was more for rooms inside a house/apartment

Im also looking for an ‘anyzone’ trigger so I only need 2 automations for each person one to enter and one for leaving the zone. As masterkenobi put it I would have to have an automation for each person x each zone x 2 (enter and leave)

example of what I thought the automation would be as follows

  - alias: name enered zone
    trigger:
      platform: zone
      entity_id: device_tracker.name_phone
      zone: zone.any
    action:     
     - service: notify.piannouncer
       data:
          message: "Name has entered {{ states('device_tracker.name_phone') }}"

After some searching and playing I found this (Ive adapted it for me, it triggers when the device zone changes, It definatley triggers when entering a zone, so I think it will fire when leaving and entering a zone.But it works.

  - alias: name entered zone
    trigger:
      platform: state
      entity_id: device_tracker.myphone
    action:     
     - service: notify.piannouncer
       data:
          message: "Name is in {{ states('device_tracker.myphone') }}"
3 Likes

Im fairly sure it can be honed down to determine if the occupant has left or entered a region

https://home-assistant.io/docs/automation/templating/

The bottom of the page specifies templating for zones.
just got to figure how the syntax works and make sense of it.

Would be cool to have 1 automation for all zones and devices, insted off haveing alot off automations

The snippet above works for all zones, but you need one per device.

You can take this a bit farther.

  - alias: name entered zone
    trigger:
      platform: state
      entity_id: device_tracker.myphone, device_tracker.secondphone, device_trackert.thirdphone
    action:     
     - service: notify.piannouncer
       data_template:
          message: "{{ trigger.to_state.name }} is in {{ trigger.to_state.state }}"
7 Likes

Note, I don’t have use zones so I can’t test this, but try it out and see how it works. You might have to change trigger.zone to trigger.zone.attributes.friendly_name or trigger.zone.attributes.name.

Also, you will have to find/replace the names of each device_tracker name and zone name.

- alias: Notify When Someone Enters Zone
  trigger:
  - platform: zone
    entity_id: device_tracker.mqtt_occupant1, device_tracker.mqtt_occupant2, device_tracker.mqtt_occupant3, device_tracker.mqtt_occupant4
    zone: zone.zone1
    event: enter
  - platform: zone
    entity_id: device_tracker.mqtt_occupant1, device_tracker.mqtt_occupant2, device_tracker.mqtt_occupant3, device_tracker.mqtt_occupant4
    zone: zone.zone2
    event: enter
  - platform: zone
    entity_id: device_tracker.mqtt_occupant1, device_tracker.mqtt_occupant2, device_tracker.mqtt_occupant3, device_tracker.mqtt_occupant4
    zone: zone.zone3
    event: enter
  - platform: zone
    entity_id: device_tracker.mqtt_occupant1, device_tracker.mqtt_occupant2, device_tracker.mqtt_occupant3, device_tracker.mqtt_occupant4
    zone: zone.zone4
    event: enter
  - platform: zone
    entity_id: device_tracker.mqtt_occupant1, device_tracker.mqtt_occupant2, device_tracker.mqtt_occupant3, device_tracker.mqtt_occupant4
    zone: zone.zone5
    event: enter
  - platform: zone
    entity_id: device_tracker.mqtt_occupant1, device_tracker.mqtt_occupant2, device_tracker.mqtt_occupant3, device_tracker.mqtt_occupant4
    zone: zone.zone6
    event: enter
  - platform: zone
    entity_id: device_tracker.mqtt_occupant1, device_tracker.mqtt_occupant2, device_tracker.mqtt_occupant3, device_tracker.mqtt_occupant4
    zone: zone.zone7
    event: enter
  - platform: zone
    entity_id: device_tracker.mqtt_occupant1, device_tracker.mqtt_occupant2, device_tracker.mqtt_occupant3, device_tracker.mqtt_occupant4
    zone: zone.zone8
    event: enter
  - platform: zone
    entity_id: device_tracker.mqtt_occupant1, device_tracker.mqtt_occupant2, device_tracker.mqtt_occupant3, device_tracker.mqtt_occupant4
    zone: zone.zone9
    event: enter
  - platform: zone
    entity_id: device_tracker.mqtt_occupant1, device_tracker.mqtt_occupant2, device_tracker.mqtt_occupant3, device_tracker.mqtt_occupant4
    zone: zone.zone10
    event: enter
  action:
    service: notify.notify
    data_template:
      message: >
        {{ trigger.entity_id.attributes.friendly_name }} Entered {{ trigger.zone }}

- alias: Notify When Someone Leaves Zone
  trigger:
  - platform: zone
    entity_id: device_tracker.mqtt_occupant1, device_tracker.mqtt_occupant2, device_tracker.mqtt_occupant3, device_tracker.mqtt_occupant4
    zone: zone.zone1
    event: leave
  - platform: zone
    entity_id: device_tracker.mqtt_occupant1, device_tracker.mqtt_occupant2, device_tracker.mqtt_occupant3, device_tracker.mqtt_occupant4
    zone: zone.zone2
    event: leave
  - platform: zone
    entity_id: device_tracker.mqtt_occupant1, device_tracker.mqtt_occupant2, device_tracker.mqtt_occupant3, device_tracker.mqtt_occupant4
    zone: zone.zone3
    event: leave
  - platform: zone
    entity_id: device_tracker.mqtt_occupant1, device_tracker.mqtt_occupant2, device_tracker.mqtt_occupant3, device_tracker.mqtt_occupant4
    zone: zone.zone4
    event: leave
  - platform: zone
    entity_id: device_tracker.mqtt_occupant1, device_tracker.mqtt_occupant2, device_tracker.mqtt_occupant3, device_tracker.mqtt_occupant4
    zone: zone.zone5
    event: leave
  - platform: zone
    entity_id: device_tracker.mqtt_occupant1, device_tracker.mqtt_occupant2, device_tracker.mqtt_occupant3, device_tracker.mqtt_occupant4
    zone: zone.zone6
    event: leave
  - platform: zone
    entity_id: device_tracker.mqtt_occupant1, device_tracker.mqtt_occupant2, device_tracker.mqtt_occupant3, device_tracker.mqtt_occupant4
    zone: zone.zone7
    event: leave
  - platform: zone
    entity_id: device_tracker.mqtt_occupant1, device_tracker.mqtt_occupant2, device_tracker.mqtt_occupant3, device_tracker.mqtt_occupant4
    zone: zone.zone8
    event: leave
  - platform: zone
    entity_id: device_tracker.mqtt_occupant1, device_tracker.mqtt_occupant2, device_tracker.mqtt_occupant3, device_tracker.mqtt_occupant4
    zone: zone.zone9
    event: leave
  - platform: zone
    entity_id: device_tracker.mqtt_occupant1, device_tracker.mqtt_occupant2, device_tracker.mqtt_occupant3, device_tracker.mqtt_occupant4
    zone: zone.zone10
    event: leave
  action:
    service: notify.notify
    data_template:
      message: >
        {{ trigger.entity_id.attributes.friendly_name }} Left {{ trigger.zone }}
1 Like

I’ve got mine set up this way also - once per user that is being tracked.
It works well for my 8 zones - and I get the notifications when people enter or leave a zone.
My message say that "User location changed: From (from_state) to (to_state).

The problem is that while people are in transit - the ‘state’ is changing periodically and I’m getting many messages saying “User location changed: From (‘not_home’) to (‘not_home’)”

Is there any way I can use a condition to filter out these messages? I basically only want messages generated when a user enters or exits a defined zone.

Or - to take this a little further - is there a service that can be called with latitude/longitude to ‘look up’ a location? It would be cool to have it report an ‘intersection’, restaurant name or store instead of ‘not_home’ when users are not within a defined zone?

Either/Or - suppress the messages where both to_state and from_state are equal to ‘not_home’ - or call a web service to look up the location name and send it when not in a zone…

Do you have any suggestions based on your experience?

Check out the google geocode custom component.

It will can give you all kinds of info based off location.

What does your automation look like and what are you using for location tracking?

Hi @rabittn - thanks for the reply. That Google Geocode Component looks like just the sort of thing I was thinking of…I’ll definitely look into that a little further - thanks for sending the link.

I’m currently using Owntracks for the location tracking. I was also using iCloud for a while - but one of the people I was tracking complained of massive battery drain when it was active. I’d like to re-enable it - but I need to be able to control the frequency of the updates so its more battery-friendly before I can do that. So far - Owntracks seems to be pretty reliable once it’s set up.

I’ve got about 8 zones defined so far - and I’d like to expand it to include all of our common destinations - and I’d like to send one alert/notification per user per entry/exit from any defined zone. For now, I’ve got one automation per user that triggers off of device_tracker..state - I didn’t define any from_state or to_state in the trigger, so as expected - it’s sending notifications whenever the state changes - which I’m realizing may sometimes be small differences in latitude/longitude. What I really want is a notification any time the from_state is not equal to the to_state. Is that something that can be coded fairly easily into my existing automation?

Here’s what I’m currently using:

- alias: LocateAidan
  trigger:
    platform: state
    entity_id: device_tracker.aidan_iphone
  action:
    service: notify.ios_my_iphone
    data_template:
      title: Location Alert
      message: >
        Aidan: {{ now().strftime("%Y%m%d-%H%M%S") }} Location has changed from {{ trigger.from_state.state }} to {{ trigger.to_state.state }}
      data:
        attachment:
          content-type: jpeg
        push:
          category: camera
        entity_id: camera.location_aidan  

My other problem with this setup is that the map I’m trying to send as a camera image does not seem to show the correct location all the time. I’m still troubleshooting the problem because the setup is fairly new - but it seems like if I open a notification that is a couple of hours old, the map shows me the persons “current” location - and not the location they were at when the notification was generated (and that’s not what I want). Here’s the camera setup just in case you have any wisdom you might be able to share - although my main problem at the moment is with the automation.

  - name: Location Aidan
    platform: generic
    still_image_url: https://maps.googleapis.com/maps/api/staticmap?center={{ states.device_tracker.aidan_iphone.attributes.latitude }},{{ states.device_tracker.aidan_iphone.attributes.longitude }}&zoom=14&size=400x400&maptype=roadmap&markers=color:blue%7Clabel:A%7C{{ states.device_tracker.aidan_iphone.attributes.latitude }},{{ states.device_tracker.aidan_iphone.attributes.longitude }}
    limit_refetch_to_url_change: true

Thank you in advance for any insight or advice you can provide!