Check My Logic Please

This is a simple automation I’m working on and am sure it’s been done before but I want to have all my doors lock when the second person leaves the house after the first person is already not there. Here’s my Automation. I should add that the Zone Check for a person leaving is “Home” which should be my address as that’s what I set it up to be. I’ve changed the names to “A” and “B” to protect the innocent.

Here’s another screen grab with the drop downs open:

YMAL generated after doing this graphically:

When

platform: device
device_id: de956777faa37250dacb0f1e58e5ae80
domain: device_tracker
entity_id: c7936d1ae44950a1231dd0bfc286ff73
type: leaves
zone: zone.home

And if

condition: device
device_id: 9b2eee3cffd284c0dc9f6a07e18c6635
domain: device_tracker
entity_id: f659a9668037e2f53ab8465d8e63846f
type: is_not_home

Then do

action: lock.lock
metadata: {}
data: {}
target:
  device_id:
    - 9f32a8639d8af862e6da1e9dfe216a75
    - 71a3c5ee4ea2e112372b23db6a6dca6c
    - e858b893ca5a18d5b97e337ae76ad16a
    - 5d1d93f9564391b3ed82dd412aa69e35

Would it be simpler to trigger on the state of the zone? This should be a number equal to the number of tracked people there - so when it changes to zero the last person has left.

Also, devices in automations can cause problems - better to use entity_id if you can.

Is it not working? Or what’s the issue? There is also proximity integration that helps with stuff like that.

For whatever reason my wife’s location isn’t triggering it even though she set the app on her phone to always for location. (I can now track her every move in HA. HEHE).

Anyhow, I’m trying this logic for the trigger but still using the “lock” function for the action:

platform: numeric_state
entity_id:
  - sensor.<wife_iPhone>_geocoded_location
  - sensor.<husband_iPhone>_geocoded_location
above: 0

I’ll report back what happens.

BTW, I see to geocoded_locations for my phone (geocoded_location and geocoded_location_2). Why?

The geocoded location sensor doesn’t normally have a purely numeric state… so a Numeric state trigger is not appropriate.

So I should use the Zone Entity then?

I’m going to try this one:
When:

platform: state
entity_id:
  - person.<wife>
  - person.<husband>
from: home
to: null
for:
  hours: 0
  minutes: 5
  seconds: 0

Then do:

action: lock.lock
metadata: {}
data: {}
target:
  device_id:
    - 9f32a8639d8af862e6da1e9dfe216a75
    - 71a3c5ee4ea2e112372b23db6a6dca6c
    - e858b893ca5a18d5b97e337ae76ad16a
    - 5d1d93f9564391b3ed82dd412aa69e35

That will lock the lock every time either one of you leaves, which is not what you asked for…

But, you need to figure out why your “wife’s location isn’t triggering it even though she set the app on her phone to always for location”. There are multiple ways that you can achieve the goal, but her presence being accurately and reliably sensed is going to be critical to all of them.

So

platform: state
entity_id:
  - person.<wife>
  - person.<husband>

Is Boolean “Or” not Boolean “And”?

How about this?

When

platform: state
entity_id:
  - person.<wife>
from: home
to: null
for:
  hours: 0
  minutes: 5
  seconds: 0

And if

condition: and
conditions:
  - condition: state
    entity_id: person.<husband>
    state: not_home

Using and if should work. Like Didgeridrew said, you’ll need to look into why your wife’s location isn’t updating. Without that data being reliable, your always going to have issues.

1 Like

My wife left 2 hours ago while I was still at work and I see it was triggered in the automations:

For whatever reason the Laundry Room Door Look does not show it is locked in the UI but I’ll physically check it soon when I get home.

Here’s the “Then do”…

Those 3 dots in upper right hand corner of your screenshot, click them and then click ‘run’. It will run that block so you know if it works or not.

That just means that the trigger was triggered, not that the automation ran all the way to the end.

Click the 3 dots > Traces, Grab the trace from when your wife left and post it here.

Maybe it’s been too long but it says, “No traces found” but she’s still away home.

That’s weird, because you should be able to see traces if your automation says “last triggered x hours ago”. Not sure if the traces get wiped out if you edited the automation in the meantime, though.

I suggest you make things easy on yourself and just follow @jackjourneyman’s advice to simply use the numeric state of the home zone:

platform: numeric_state
entity_id:
  - zone.home
for:
  hours: 0
  minutes: 5
  seconds: 0
below: 1

No conditions required since it will only trigger when both of you have left the house. Easier to troubleshoot too, so win-win.

If it still isn’t triggering correctly, check your device tracker. The fact you mention you now have a geocoded_location_2 sensor seems to indicate that your device tracker has been duplicated.

So something like this?

When:

platform: numeric_state
entity_id:
  - zone.home
for:
  hours: 0
  minutes: 5
  seconds: 0
below: 1

Then do:

action: lock.lock
metadata: {}
data: {}
target:
  device_id:
    - 9f32a8639d8af862e6da1e9dfe216a75
    - 71a3c5ee4ea2e112372b23db6a6dca6c
    - e858b893ca5a18d5b97e337ae76ad16a
    - 5d1d93f9564391b3ed82dd412aa69e35

Still looking into the location issues and will report back on that.

Yep, looks good.