Automation for entering and leaving Zones

You are using fancy quotes here:

event: '{{ ''left'' if trigger.event == ''leave'' else ''arrived at'' }}'

Use double quotes instead of two single quotes inside the curly brackets:

event: '{{ "left" if trigger.event == "leave" else "arrived at" }}'

Thanks, appreciate the help!
The problem seems to be within the call service/data, as even with removing the variables completely the error remains?

I don’t know. If you remove the variables it will give an error anyway,because then the variables are not defined.

Thanks for the great automation :slight_smile:
i have one question but can i modify the notification i receive:
actually : the notification says that : Iphone de “name” entered SAMCHA (it’s my zone home), can i have something more explicit ? For example : “name” vient de rentrer à la maison
Thanks

Awesome, and thanks for the great automation!

This implies that if the devices in the notifications group will also receive their own notifications, right?

Also the message part is missing one +

compare:

      message: >
        {{ person + ' ' + event ' ' + zone }}
      message: >
        {{ person + ' ' + event + ' ' + zone }}

That being said, I’m also having issues getting this automation to trigger.

Hi Guys,
I would like to do something similar.
The idea is, after a certain time (like 10 p.m.) check the location of people and if they are all in the house turn on a green light, if one is out of the house turn on a red light until they come back in.
This is what I did and it is working

alias: Back Home
description: “”
trigger:

  • platform: time_pattern
    minutes: /10
    condition:
  • condition: time
    after: “22:00:00”
    before: “02:00:00”
  • condition: zone
    entity_id: device_tracker.iphone_di_rosy
    zone: zone.home
  • condition: zone
    entity_id: device_tracker.iphone_di_sofy
    zone: zone.home
  • condition: zone
    entity_id: device_tracker.iphone_13_di_bea
    zone: zone.home
  • condition: zone
    entity_id: device_tracker.unknown
    zone: zone.fede_home_milan
    action:
  • service: scene.turn_on
    target:
    entity_id: scene.lamp_green
    metadata: {}
    mode: single

But I do not like that the time pattern is always running, I did not find a way to start it after a certain time. And also I have to run in parallel another automation that turn a red light after 9:59PM like this:

alias: Turn ON Red Lamp
description: “”
trigger:

  • platform: time
    at: “21:59:00”
    condition: []
    action:
  • service: scene.turn_on
    target:
    entity_id: scene.lamp_red
    metadata: {}
    mode: single

Is there a way to simplify this? Thanks

Hey guys! just noticed this wasn’t working anymore. I used to use:

service: notify.mobile_app_craigss20v2
data:
  message: |
    {% if trigger.event == "leave" %}
      {{ trigger.from_state.attributes.friendly_name }} left {{ trigger.zone.attributes.friendly_name }}
    {% else %}
      {{ trigger.from_state.attributes.friendly_name }} arrived {{ trigger.zone.attributes.friendly_name }}
    {% endif %}

but now that call service I guess isn’t supported. Looks like you need to use Notification service. Where do i put this information so it specifies the person and the zone in question instead of a generic message?

I was also looking to setup this same automation and I think your solution is the best for what the OP was asking. I didn’t want to have to create a trigger for every zone. I find myself adding new zones all the time and I just wanted to be notified when my family enters and leaves these zones without having to update the automation. It was stated that you can’t have one trigger for multiple zones but your strategy does this perfectly. I’m surprised no one responded to you to help you figure out your syntax. Anyway, better late than never. Here is how I used you started to setup this automation:

alias: "Notify: Zone Changes"
description: ""
trigger:
  - platform: state
    entity_id:
      - device_tracker.laura
    id: Laura
  - platform: state
    entity_id:
      - device_tracker.robert
    id: Robert
condition:
  - condition: template
    value_template: "{{ trigger.from_state.state != trigger.to_state.state }}"
action:
  - variables:
      person: "{{ trigger.from_state.attributes.friendly_name }}"
      to_zone: "{{ trigger.to_state.state }}"
      from_zone: "{{ trigger.from_state.state }}"
  - choose:
      - conditions:
          - condition: trigger
            id: Laura
        sequence:
          - device_id: [DEVICE ID]
            domain: mobile_app
            type: notify
            message: >-
              {{ person }} {% if to_zone == 'not_home' %} left {{ from_zone }}{%
              endif %}{% if from_zone == 'not_home' %} arrived at {{ to_zone
              }}{% endif %}
      - conditions:
          - condition: trigger
            id: Robert
        sequence:
          - device_id: [DEVICE ID]
            domain: mobile_app
            type: notify
            message: >-
              {{ person }} {% if to_zone == 'not_home' %} left {{ from_zone }}{%
              endif %}{% if from_zone == 'not_home' %} arrived at {{ to_zone
              }}{% endif %}
mode: single

3 Likes

This is my first post and I hope it helps someone. This automation notifies with person’s name, left or arrived at, and the zone label.

alias: Notify Zones
description: ""
trigger:
  - platform: state
    entity_id: person.1, person.2, person.3
    not_from:
      - unknown
      - unavailable
    not_to:
      - unknown
      - unavailable
condition:
  - condition: template
    value_template: "{{ trigger.from_state.state != trigger.to_state.state }}"
action:
  - service: notify.mobile
    data_template:
      message: >-
        {{ trigger.to_state.attributes.friendly_name }} {% if
        trigger.to_state.state == 'not_home' %}left {{ trigger.from_state.state
        }}{% endif %}{% if trigger.from_state.state == 'not_home' %}arrived at
        {{ trigger.to_state.state }}{% endif %}
mode: single
2 Likes

Thank you. Aside from editing the person.1, person.2 etc…

1- Is there anything I should change in your code to make it work for the Home zone?
2- Does this code notify the person that they themselves left/arrived at the Home zone? Or does it only notify about other persons (which I think is ideal)?

  1. It will notify for all zones, including the home zone.
  2. The tracker(s) designated under ‘entity_id’ will trigger the automation, yourself included if you like.

You also need to edit the service: notify.mobile line to fit your notify service or companion app entity.

1 Like

Thank you. All zone notification is a great way to reduce clutter! I can do one per person!

I copied the code and changed the person entity and edited the notify.mobile, but I got this error

Error rendering data template: UndefinedError: ‘dict object’ has no attribute ‘to_state’

Step ConfigChanged VariablesRelated logbook entries

My message template includes the translation of the word ‘home’ in italian. So now my message is perfectly readable. I also changed the logic a bit. This will avoid empty messages when passing from zone to zone, without reaching a ‘not_home’ status

  message: >-
    {% set from = trigger.to_state.state %}  {% set to =
    trigger.to_state.state %}  {% set person =
    trigger.to_state.attributes.friendly_name %} {% if from == 'home' -%}{%
    set from = 'casa' %}{%- endif %} {% if to == 'home' -%}{% set to = 'casa'
    %}{%- endif %} {{person}} {%if from != 'not_home' -%}ha lasciato {{from}} {%
    if to != 'not_home' -%} e {%- endif %}{%-endif %} {% if to != 'not_home' -%}
    ha raggiunto {{to}} {%- endif %}
1 Like

How would you specify that only person.1 receive notifications about person.2, and vice versa. I’ve tried a few things, but I’m pretty new to HA (only about two weeks) and haven’t figured out a lot yet. Thanks.

1 Like

that was my question as well, good point

Maybe there’s an elegant way to do it, but you could used 2 automations, one for person 1 monitoring person 2 and one for person 2 monitoring person 1 and use the specific notify.mobile person.

#1 - device_tracker.bens_iphone is tracked to notify notify.mobile_app_brads_iphone

#2 - device_tracker.brads_iphone is tracked to notify notify.mobile_app_bens_iphone

Thanks for posting the cod above, I also added this to give a date/time stamp to the message:

{{now().strftime("%H:%M:%S on %m/%d/%y")}}
        message: >
          {{ trigger.to_state.attributes.friendly_name }} {% if
          trigger.to_state.state == 'not_home' %}left {{ trigger.from_state.state
          }} {{now().strftime("%H:%M:%S on %m/%d/%y")}} {% endif %}{% if trigger.from_state.state == 'not_home' %}arrived at
          {{ trigger.to_state.state }} {{now().strftime("%H:%M:%S on %m/%d/%y")}} {% endif %}
1 Like

Sorry for this newbie question (I just discovered HA few days ago)
Where do I have to put this code?
I tried to append it in my “configuration.yaml” (after having substituted correct persons and zone IDs) and keeping correct indentation but that produce parsing errors.

I have the following automation code

alias: Zone Notifications
description: ""
trigger:
  - platform: state
    entity_id: >-
      device1,
      device2
    not_from:
      - unknown
      - unavailable
    not_to:
      - unknown
      - unavailable
condition:
  - condition: template
    value_template: "{{ trigger.from_state.state != trigger.to_state.state }}"
action:
  - service: notify.telegram_main
    data_template:
      message: >-
        {{ trigger.to_state.attributes.friendly_name }} {% if
        trigger.to_state.state == 'not_home' %}left {{ trigger.from_state.state
        }}{% endif %}{% if trigger.from_state.state == 'not_home' %}approaching
        {{ trigger.to_state.state }}{% endif %}
mode: queued

I have two problems with it…
1 - Periodically I get a ‘blank’ notification…in that it has the person’s name, but nothing else…which tells me that the person is neither entering or leaving a zone, and in fact…I’m sitting in the same room with them when it happens…they haven’t been able to identify what activity they are doing that’s causing it either…so, there is that…
2 - Reading up on Templating - Home Assistant it says “Avoid using states.sensor.temperature.state , instead use states('sensor.temperature').” as this can cause errors…this specifically uses those…so, I’m wondering if anyone with more skill on this has suggestions for changes that would prevent me from getting blanks, and to use the ‘suggested’ syntax

I noticed your trigger is “not_from” and “not_to”. I’m thinking rather than using that, why not use “from” and “to” instead and use “home” and “not_home”, then it won’t trigger only on those known states (and the states you want)?