When Last Person Leaves - 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 First Person Arrives, 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 Last Person Leaves
    description: |
        * Perform an action when last person leaves 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 last tracked person leaves 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: not
      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 }}'

action: !input actions

mode: single
2 Likes

Any tips as to why this might not be working for me?