One automation to notify when occupants enter or leaves any zones

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

what type of automation? Sorry im a newb…

i get this error message

that is not an error, just a warning that it is not possible to edit in gui, it is yaml only

This works so well it should be one of the default offered automations. This is perfect. I have struggled with various attempts throughout the years trying to nail down a proper location update. This works a treat, love that it deletes the old notifications too. Beautiful. Thank you.

1 Like

This might be the most promising one I have seen yet. I put this in today and have only triggered 1 zone so far and it works. I’ve bookmarked this and will report back after some time.

1 Like

Just to update I have created this Blueprint and it works like a charm. Just copy and paste this into a file (e.g. occupants_tracking.yaml) and save this file in config > blueprints > automation > homeassistant.

After that, you can use this Blueprint via Settings > Automations & Scenes > Blueprints

blueprint:
  name: Occupants Tracking
  description: "This automation blueprint is designed to keep informed on your occupants whereabout. It will notify you when someone is back or left the home."
  
  domain: automation

  input:
    person_entities:
      name: Occupants
      description: "Choose from the list of 'person' entities to track"
      selector:
        entity:
          domain: person
          multiple: true
    notify_service:
      name: Notify Service
      description: "Enter the Notify Service or script such as notify.telegram or notify.mobile_app_device_name to receive notification when the occupant is home or away."
      default: "notify.telegram"
      selector:
        text:

mode: queued
max_exceeded: silent

variables:
  person_entities: !input 'person_entities'

trigger:
  - platform: state
    entity_id: !input 'person_entities'
    to: 'home'
  - platform: state
    entity_id: !input 'person_entities'
    from: 'home'


action:
  - service: !input 'notify_service'
    data:
      message: >-
        {%- set person = trigger.to_state.name -%}
        {%- set new_state = 'back' if trigger.to_state.state == 'home' else 'away' -%}
        {%- set people_home = states.person | selectattr('entity_id', 'in', person_entities) | selectattr('state', 'eq', 'home') | map(attribute='name') | list -%}
        {%- set people_away = states.person | selectattr('entity_id', 'in', person_entities) | rejectattr('state', 'eq', 'home') | map(attribute='name') | list -%}
        
        {{ person }} is {{ new_state }}. Right now, {{''}}
        {%- set group = people_home if new_state == 'back' else people_away -%}
        {%- set group_last = group[-1] if group|length > 1 else None -%}
        {%- set group_list = group[:-1] | join(', ') -%}
        
        {%- if group|length == person_entities|length -%}
          everyone is {{ 'at home' if new_state == 'back' else 'away' }}
        {%- elif group|length == 1 -%}
          {{ person }} is the only one {{ 'at home' if new_state == 'back' else 'away' }}
        {%- else -%}
          {{ group_list }} and {{ group_last }} are {{ 'at home' if new_state == 'back' else 'away' }}
        {%- endif -%}

1 Like

Thank you for the master kenobi.
I was struggling a bit but the problem which i had is i named my notifyer my own telegram-username and tried to use notify.telegram… that doesnt work.
So now in my config.yaml i have the notifier named ‘notify.telegram’ and i can use it as default and as designed, way better.

Notifications look nice. Thank you!