Presence Automation with 2 Persons leaving home

If you go into SETTINGS then to PEOPLE you can see your HA accounts/people. You can associate multiple or single devices with a person, like this.

I use Life360 as well as Unifi for device tracking, so have 2 entities listed to track.

What this helps with is when you get a new phone, you won’t have to change any automations as they will all say person.jason. All you need to do is update the associated phone in the person.

device_tracker.daves_phone

@kanga_who My bad - yes - I had done that when setting up HA. Sorry. Each of our phones are associated with each of us - my to me and my wife’s phone to her.

So -if I change the state as previously stated, the automation should execute - correct?

@kanga_who Are you saying to change family group entities to:

family:
  name: Family
  entities:
     - device_tracker.daves_phone
     - device_tracker.delores_phone

or leave them as, sine they person entity is associated with their reactive phones,

family:
  name: Family
  entities:
    - person.david
    - person.delores

If you have the phones associated with a person, use the person example

@kanga_who ok - leaving as:

family:
  name: Family
  entities:
    - person.david
    - person.delores

Now - testing.

So I should be able to go into “Developer Tool”, “State”, change entity states of Dave’s Phone from home to not home and do the same with the wife’s phone and the automation should trigger - correct?

@kanga_who No joy. The automation is not being triggered.

This is the automation:

alias: Dave Family Kitchen Lights Off
description: ""
trigger:
  - platform: state
    entity_id:
      - group.family
    from: home
    to: not home
    for:
      hours: 0
      minutes: 2
      seconds: 0
condition: []
action:
  - type: turn_on
    device_id: b68dc161768ebea7ff13c6206cb023ba
    entity_id: light.hall_bathroom_light_sw
    domain: light
mode: single

This the group:

family:
  name: Family
  entities:
    - person.dave_LASTNAME
    - person.delores_LASTNAME

This is part of the configuration.yaml:

######### YAML File Locations #########################
automation: !include automations.yaml
script: !include scripts.yaml
scene: !include scenes.yaml
group: !include groups.yaml
#template: !include templates.yaml

What am I missing?

You will need to make sure that both of your phones continue to be shown as away for over 2 mins for that to work - if you are using the HA app for tracking it may update you back to being home faster than that.

For testing purposes it might be worth just removing the 2mins so it should trigger instantly.

alias: Dave Family Kitchen Lights Off
description: "Dave Family Kitchen Lights Off"
trigger:
  - platform: state
    entity_id: group.family
    from: 'home'
    to: 'not_home'
action:
  - service: homeassistant.turn_off
    entity_id: light.hall_bathroom_light_sw

Using Node Red for my automations I was curious why I couldn’t get a simple automation to work when the number of people in zone.home dropped to zero. Instead of the trigger being a numeric state I was able to get it to work by making the trigger any change to the zone.home state and make the numeric state a condition. That’s how I would do it in Node Red - watch the entity for a state change and then take action based on the value that changed. Not sure this helps if you’re now trying to use the group method. Just goes to show that there’s more than one way to skin an automation!

trigger:
  - platform: state
    entity_id:
      - zone.home
condition:
  - condition: numeric_state
    entity_id: zone.home
    below: 1

You missed the underscore. It should be:

to: not_home

So, you’ve received many suggestions, which is great. Lots of things to learn with HA! :grinning_face_with_smiling_eyes:

I’ve been using HA for years and have actually worked on some of the core code (automations & scripts, as well as the Life360 integration.) That doesn’t make my suggestions any better, but I do have some unique insights. :sweat_smile: Anyway…

For a newbie, I suggest using the most “HA way” of doing things, at least to start. In this particular scenario, I’d recommend simplifying. You should be able to do this strictly via the UI. (There’s nothing wrong with editing YAML files directly. Most experienced/advanced users do. But “baby steps”… lol)

If I understand correctly, you already have two person entities created, each associated with a corresponding device_tracker entity. That’s all you should need.

For the automation, create one “Numeric state” trigger. For “Entity”, pick “Home/zone.home”. Then put 1 in the “Below” box. Once you’ve done that, if you were to select the “Edit in YAML” option for the trigger, it should look like this:

image

Then, put whatever you want in the Actions section.

This automation should run whenever the numeric interpretation of the state of the Home Zone entity (which is representation of the number of person entities located within that zone) changes from a value of 1 or more to a value below 1 (i.e., 0. To state that simply, when the last person leaves the Home Zone.)

It’s important to understand that automation triggers “fire” only when something changes. E.g., when the automation is first created (or if it gets reloaded, e.g., due to being changed via the UI automation editor), it won’t fire if the current numeric value of zone.home is already below 1. The state of zone.home has to change from a value of 1 or more to a value below 1.

Also, remember “not home” is not the same as “not_home”. For device_tracker, person, and group entities made up of these, the states “home” and “not_home” have specific meanings. The former usually shows as “Home” in the UI, whereas the latter usually shows as “Away” (or the equivalent words for other languages.)

Hope this helps.

3 Likes

Right. This code should work.
But I agree there are simpler ways that have already being reported.

@pnbruckner Coding - helps when you type it out correctly. That solved the problem concerning @kanga_who suggestion. @kanga_who solution now works. Both I and my wife have to be gone for the action (turning off lights) to work. If either of us is home and one of us leaves, nothing happens - perfect!

@w35l3y Many thanks !

@pnbruckner yes - I am very - very happy with all the assistance! The more the better. Someday - I will learn Node Red - in the future. I switched to HA for its power and flexibility…and I am seeing it!! :astonished:

As you say - baby steps !! Oh ya! And I am sticking to the “HA way”…until I get way more schooled!

break, break…
Ok, so while the automation (from @kanga_who and everyone’s help - thanks!) is working - very cool by the way, I still implemented your suggestion, and (no surprise to you) it is also working. So - as always, many ways to get from point A to point B. This is very cool!

Now with some reading, and real-world implementation examples, I have some understanding (let’s be clear - it’s a very basic understanding) of the “numeric state” function. The word “condition” kept confusing me. As in trigger, condition, action. And it is a condition - but within the trigger itself.

It’s important to understand that automation triggers “fire” only when something changes.
E.g., when the automation is first created (or if it gets reloaded, e.g., due to being changed
via the UI automation editor), it won’t fire if the current numeric value of zone.home is
already below 1. The state of zone.home has to change from a value of 1 or more to a
value below 1.

Yup - learned that - all good.

Also, remember “not home” is not the same as “not_home”. For device_tracker, person, and group
 entities made up of these, the states “home” and “not_home” have specific meanings. The former
usually shows as “Home” in the UI, whereas the latter usually shows as “Away” (or the equivalent
words for other languages.)

Yup - definitely learned that!

Yes - this does help - many thanks!!

So - as I add users, they will become part of zone.home - automatically ? (how does a person get added to zone.home?)? And if I pick a device to track - such as their cell phone, then the cell and person are “associated”.

This is how I like to look at automation triggers & conditions:

Triggers determine when the automation’s actions should run. Conditions determine if the actions will run (when one of the triggers “fires”.)

So, the sequence is…

  1. Some event in the system occurs (e.g., the state of some entity changes, an event is fired, time changes, etc.)
  2. A determination is made if that event should cause any of the triggers to “fire.” (In this sense, you can also think of an automation trigger as an event filter. It only lets certain events start the automation.)
  3. If a trigger fires, then the automation’s conditions (if any) are evaluated. If they are true, then the actions are run.
  4. The actions actually run (in response to a trigger firing, and if the conditions evaluate to true at that time.)

person entities are not added to zones. Zones automatically provide a count (and a list, in their persons attribute) of all the person entities (that exist) whose GPS location is inside the zone. (Actually, both zones and person entities have a size (radius) associated with their GPS location, so they form circles. The zone considers a person inside it if its own location circle overlaps the location circle of the person.) There’s nothing you have to do other than to define zones and person entities.

Yes, a person entity can have zero or more device_tracker entities associated with it. If there is more than one, then the person entity attempts to figure out how to use the location/state of the multiple device_tracker entities in a reasonable way. (Personally, I’ve never liked how it works. I have my own custom integration that, IMHO, does a better job of that. But that’s another story… :laughing: ) Anyway, if the person entity has at least one device_tracker associated with it, then the person entity takes on the location/state of the device_tracker entity/entities.

FWIW, zones only look at person entities, not device_tracker entities. But there are other ways to use zones with device_tracker entities (some of which have already been discussed in this topic, I believe.)

@pnbruckner Very nice explanation - THANKS!

@pnbruckner @w35l3y @kanga_who @mightybosstone @rossk
Many thanks to you all for your individual and collective help. It was outstanding and very much appreciated !!

I am going to copy this entire discussion into my HA notebook on “how to…do things.”

I am taking three things from this discussion: 2 solutions and 1 heck of a lot of learning – again – many thanks to you all.

I think my issue is solved. :+1: :grinning:

1 Like
alias: kitchen roller automation
description: ""
trigger:
  - platform: zone
    entity_id: device_tracker.maria
    zone: zone.home
    event: enter
  - platform: zone
    entity_id: device_tracker.spiros_kallioupis
    zone: zone.home
    event: enter
condition: []
action:
  - if:
      - condition: zone
        entity_id: device_tracker.maria
        zone: zone.home
      - condition: zone
        entity_id: device_tracker.spiros_kallioupis
        zone: zone.home
    then:
      - device_id: f3a0f9f241e258be162ef2f57c81a6d6
        domain: cover
        entity_id: cover.kitchen_roller
        type: set_position
        position: 100
    else:
      - device_id: f3a0f9f241e258be162ef2f57c81a6d6
        domain: cover
        entity_id: cover.kitchen_roller
        type: set_position
        position: 0
mode: single

and work perfect without big job!!!