One automation to notify when occupants enter or leaves any zones

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 }}"
6 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!

can any one share there automation there some used with google geocode custom component. As I Testing and thinking its going to be good to use all the time :slight_smile:

This is what I use now, works fine:

  alias: Notify when someone enters or leaves a zone
  description: ''
  trigger:
    platform: state
    entity_id: person.one, person.two, person.three, person.four
  condition:
    condition: template
    value_template: "{{ trigger.from_state.state != trigger.to_state.state }}"
  action:
    - service: notify.all_devices
      data_template:
        title: 'Location update {{ states.sensor.time.state }}'
        message: "{{ trigger.to_state.attributes.friendly_name }} {% if trigger.to_state.state == 'not_home' %}has left {{ trigger.from_state.state }}{% endif %}{% if trigger.from_state.state == 'not_home' %}arrived at {{ trigger.to_state.state }}{% endif %}"

4 Likes

I use the android homeassistant app which generates various sensors, including the location etc.

- alias: 'Telegram - Whois it'
  trigger:
  - platform: event
    event_type: telegram_command
    event_data:
      command: /whois
  action:
    - service: telegram_bot.send_message
      data_template:
        message: "{{ states('sensor.redmi_note_8_pro_geocoded_location') }}"
    - service: telegram_bot.send_location                                                                      
      data_template:                                                                                           
        latitude: '{{states.device_tracker.redmi_note_8_pro.attributes.latitude}}'                               
        longitude: '{{states.device_tracker.redmi_note_8_pro.attributes.longitude}}'

you can customize the trigger as you like

Thanks for sharing this roumen! Works like a charm.
Just had to create a notify group for all_devices.
Is there a way to not notify the person that has the zone change of them zone changing as it adds an extra notification count?
Cheers

If you’re able to implement this without creating different groups I would be interested as well! :slight_smile:

Thanks! Works perfect for me. You saved me a lot of time, now I don’t need to create many automations. One automations is enough for all zones and persons :grinning:

Hi,
Is this sill OK syntax?
I am not able to run this (changed persons and notification devices ofcourse). :slight_smile:
Any help would be highly appreciated.
BR,
Gordan

1 Like

Indica mihi cur, cum usura vestri automation, dat error?


Ego esset gratum auxilium tuum

the question is removed. everything works, just writes in debugging that the state has not changed.

Thanks to everyone in this thread!

My HA has many “zones”, walmart, crappy tire, grocery store, ect.
some zones are side-by-side and a person could go from zone to zone with no “not_home” , breaking some of the automation templates above.

I made some modifications to account for this, and added message “tag”, and clearing of previous /out of date location message

trigger:
  - platform: state
    entity_id:
      - person.person_1
condition:
  condition: template
  value_template: "{{ trigger.from_state.state != trigger.to_state.state }}"
action:
  - service: notify.mobile_app_person_2
    data:
      message: clear_notification
      data:
        tag: location
  - service: notify.mobile_app_person_2
    data_template:
      title: Location update {{ states.sensor.time.state }}
      message: >
        {{ trigger.to_state.attributes.friendly_name }}
        {% if trigger.to_state.state == 'not_home' %}has left {{trigger.from_state.state }} 
        {% else %}arrived at {{ trigger.to_state.state }}
        {% endif %}
      data:
        tag: location

results in messages:
TITLE: Location Update 17:25
MESSAGE: Person 1 has arrived at home

{clear previous message}
TITLE: Location Update 17:26
MESSAGE: Person 1 has left home

{clear previous message}
TITLE: Location Update 17:26
MESSAGE: Person 1 has arrived at walmart

{clear previous message}
TITLE: Location Update 17:27
MESSAGE: Person 1 has arrived at lowes

{clear previous message}
TITLE: Location Update 17:28
MESSAGE: Person 1 has left lowes

{clear previous message}
TITLE: Location Update 17:29
MESSAGE: Person 1 has arrived at home

3 Likes

where do i put this code?

automation