When First Person Arrives - select people and a zone, add your conditions and actions

I have several devices with companion app, but not all of them are personal mobile phones (some are just tablets with limited dashboards for everyone to use), so tracking the zone itself wasn’t an option for me. Instead I created an automation that only considers users from defined list. I decided to make it into a blueprint to share.

Open your Home Assistant instance and show the blueprint import dialog with a specific blueprint pre-filled.

There’s also another blueprint for When Last Person Leaves, if you need that.

If you use it and it works for you, please consider leaving a :heart: or a comment. Thank you :slight_smile:

blueprint:
    name: 🚪 When First Person Arrives
    description: |
        * Perform an action when first person arrives to a zone. 
        * All the logic is incapsulated within the blueprint.
        * You can add additional conditions if you need to.
    domain: automation
    author: oneplusuniverse
    input:
        zone_input:
            name: Zone
            description: Select a zone to monitor
            default: "zone.home"
            selector:
                entity:
                    filter:
                        domain: zone
        person_input:
            name: Entities with location
            description: Select entities to track
            selector:
                entity:
                    multiple: true
                    filter:
                        domain: person
        additional_conditions:
            name: Conditions
            default: []
            description: Any additional conditions
            selector:
                condition:
        actions:
            name: Actions
            description: Add an action to run when firt tracked person enters selected zone.
            selector:
                action:

variables:
  person_list_variable: !input person_input
  zone_variable: !input zone_input

trigger:
    platform: state
    entity_id: !input zone_input
  
condition:
    - and: !input additional_conditions
    - alias: Internal logic
      condition: and
      conditions:
          - condition: template
            value_template: '{{ (person_list_variable + state_attr(zone_variable, "persons")) | count - dict.fromkeys(person_list_variable + state_attr(zone_variable, "persons")) | count == 1}}'
          - condition: template
            value_template: '{{ trigger.from_state.state|int(0) < trigger.to_state.state|int(0) }}'

action: !input actions

mode: single
3 Likes

Very nice, I have one question: I would like to send a notification to the person who is entering home with this blueprint, do you know how the get the user who is entering?

Something like this but I don’t know how to test and find the right values in the condition:

actions:
      - variables:
          action_open: "{{ 'OPEN_' ~ context.id }}"
          action_garage1: "{{ 'GARAGE1_' ~ context.id }}"
          action_garage2: "{{ 'GARAGE2_' ~ context.id }}"
      - choose:
          - conditions:
              - condition: template
                value_template: "  {{ trigger.to_state.persons == person.user1 }}" # HERE how to get user1 is the one entering home?
            sequence:
              - service: notify.mobile_app_user1
                data:
                  message: Bentornato a casa, cosa vuoi fare?
                  title: Casa
                  data:
                    actions:
                      - action: "{{ action_open }}"
                        title: Apri tapparelle
                      - action: "{{ action_garage1 }}"
                        title: Garage Grande
                      - action: "{{ action_garage2 }}"
                        title: Garage Piccolo

Thank you!

This blueprint doesn’t expose any of those variables, but you can try something like this, which is crud, but should work:

{# people in your zone of choice: {% set list1 = state_attr("zone.home", "persons") %} #}
{% set list1 = ["person.ipad", "person.ipad2" ] %}
{# your selected people as an array: ["person.person1", ...] #}
{% set list2 = ["person.ted", "person.ipad", "person.sarah"] %}

{% set ns = namespace(person=null) %}

{% for person in list2 %}
  {% if person in list1 %}
    {% set ns.person = person %}
  {% endif %}
{% endfor %}

{{ ns.person }} {# <- your person #}

You’ll have to specify your zone manually as well as the list of people I’m afraid. Not sure if template variables from the blueprint are exposed and available in action template, but you can try.

Even from that, you’ll get just the person from a person domain, like person.marcello and you’ll have to figure out how to send notification to that person. Probably could be achieved by extending the list of notifiers under notify in configuration.yaml file. Or maybe there’s another way that I’m not familiar with.

Anyway, thanks for your question and good luck! :slight_smile:
Let us know here if you’ll figure it out.

Yes thank you for your idea! After your suggestions, I tried different ways and found the below action works for my purpose (send a notification to the person who is entering home):

      - choose:
          - conditions:
              - condition: template
                value_template: >-
                  {{ state_attr(zone_variable, 'persons')[0] ==
                  'person.marcello' }}
            sequence:
              - service: notify.mobile_app_marcello
                data:
                  message: Welcome back!
                  title: Home

So I can access in the action the blueprint variable, I didn’t know that and it’s very useful.
I don’t know if I am the only one with this idea, but in future improvement it could be added a variable from blueprint with the persone who’s entering home so it can be used in next actions.

Thank you very much again!

That’s very nice, there’s one thing with that approach - if you are trying to not track every person, getting the first one wouldn’t work as someone could still be at home while not being tracked: like in my case, I have an Ipad with user logged in at home and I don’t want to track it. As soon as the first person arrives,

value_template: >-
                  {{ state_attr(zone_variable, 'persons')[0] ==
                  'person.marcello' }}

will return false since first entry in the list would be an ipad person even tho it was not the one that triggered the automation.

I’ll consider exposing those things as an additional variables, thanks for your suggestion! :slight_smile:

1 Like

Oh yes I fix with this:

value_template: >-
                  {{ 'person.marcello' in state_attr(zone_variable, 'persons') }}
1 Like

I’d also like to do something along these lines.

I’m looking to have a personalised welcome home announcement via Alexa to the last person who comes back into the home zone, who then opens our front door with a door sensor.

Is this possible?