Automation for entering and leaving Zones

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)?