Automation when leaving the house is not triggered

Hello!

I’m new here and a beginner in Home Assistant and am just about to gradually migrate from HomeKit to Home Assistant because, among other things, the unreliability & problems that HomeKit and especially Siri have since IOS/TVOS 16 just gets on my nerves. Currently I’m struggling with a presence automation and would be very happy about a tip …

I want to lock the front door when the last person leaves the house - since there is apparently no person group in HA which would make the whole thing much easier and I have not managed to create a person group manually via yaml I have created two automations… one is that the front door will be locked when I leave the zone of our house with the condition that my wife’s smartphone is not at home and the second automation is the other way around… The HA app is installed on our smartphones and the location of both smartphones is shared.
Unfortunately the automation does not trigger and I don’t know what the reason is and if I have a mistake in thinking… the one automation:

alias: Michael leaves the apartment
description: “”
trigger:

  • platform: device
    device_id: xxx
    domain: device_tracker
    entity_id: device_tracker.xxx_von_michael
    type: leaves
    zone: zone.home
    condition:
  • condition: device
    device_id: xxx
    domain: device_tracker
    entity_id: device_tracker.xxx
    type: is_not_home
    action:
  • device_id: xxx
    domain: lock
    entity_id: lock.nuki_wohnungstur_schloss_lock
    type: lock
  • service: notify.xxx_von_michael
    data:
    message: the apartment door was locked!
  • service: scene.turn_on
    target:
    entity_id: scene.general_all_lamps_out_actions
    metadata: {}
    mode: single

Regards!
Michael

Please format your code properly: we can’t debug that.

See here:

Sorry! Please delete the post then - I fail at the correct formatting since I have no way to check if this is correct…

It’s not hard: type three backticks (```) then paste your code, then type another three backticks (```). Your code should end up looking like this:

alias: Michael leaves the apartment
description: ""
trigger:
 - platform: device

You’ve pasted it as normal text, which the forum software has interpreted with bullet points, removing all the important indentation, and converting the quoting. There could be all sorts of errors in your code hiding in there that we can’t see.

Hint: there is an edit button, no need to keep deleting your attempts. Three dots, then the pencil.

ah ok, thanks a lot, it took me a few tries now…

alias: Eingang - Michael leave the house
description: ""
trigger:
  - platform: device
    device_id: fc9afa1536556669a15b47d6d04540eb
    domain: device_tracker
    entity_id: device_tracker.iphone_14_pro_max_michael
    type: leaves
    zone: zone.home
condition:
  - condition: device
    device_id: 0b4ee295798f79a2df18ad7662bbc3ad
    domain: device_tracker
    entity_id: device_tracker.oneplus9pro
    type: is_not_home
action:
  - device_id: d67bf48b00ad0fd3620707c00c20095f
    domain: lock
    entity_id: lock.nuki_door_lock
    type: lock
  - service: notify.mobile_app_iphone_14_pro_max_michael
    data:
      message: Door is closed!
  - service: scene.turn_on
    target:
      entity_id: scene.general_all_lamps_out_actions
    metadata: {}
mode: single
2 Likes

The automation looks OK, but I don’t know how your devices’ states behave. Let’s try and simplify it:

If you want a person group, it’s set up in configuration.yaml like this:

group:
  residents:
  name: Residents
  entities:
    - person.michael
    - person.frau

You don’t necessarily need that, though.

You then set up Persons under Settings / Persons — add them, and associate them with one or more devices.

In that way, when your phone leaves, person.michael will be not_home. The state of zone.home will be the number of people in the zone, so you can trigger on that going to zero. Of course, if you both turn your phones off, you’ll get locked in :slight_smile: .

First of all, thanks for your effort, I’ve already familiarized myself with HA and also various automations, etc. can create which also work but something so probably trivial hooks it yet… my wife and I are also assigned to their own smartphones and the respective status is recognized perfectly, I have now also created some other zones in our city and when these are entered or left this is displayed at the person!
If I add the group by copy and paste in the configuration.yaml and check the configuration I get the following error:

Invalid config for [group]: Entity ID family is an invalid entity ID for dictionary value @ data[‘group’][‘name’][‘entities’]. Got None. (See /config/configuration.yaml, line 73).

Line 73 for me is the entry group:

But if I understand it right I don’t need the group at all and can query the status of the zone - would that be correct?

alias: Eingang - Michael leave the house
description: ""
trigger:
  - platform: device
    device_id: fc9afa1536556669a15b47d6d04540eb
    domain: device_tracker
    entity_id: device_tracker.iphone_14_pro_max_michael
    type: leaves
    zone: zone.home
condition:
  - condition: state
    entity_id: zone.home
    attribute: persons
    state: "0"
action:
  - device_id: d67bf48b00ad0fd3620707c00c20095f
    domain: lock
    entity_id: lock.nuki_door_lock
    type: lock
  - service: notify.mobile_app_iphone_14_pro_max_michael
    data:
      message: Door is closed!
  - service: scene.turn_on
    target:
      entity_id: scene.general_all_lamps_out_actions
    metadata: {}
mode: single

Exactly. Also, if you trigger off zone.home going to zero you only need one automation and no conditions.

You don’t need the attribute: persons.

2 Likes

That’s even better! Thank you very much for the help! When I had asked in the german Apple support forum because of a problem there were mostly only stupid sprays

Kind Regards!
Michael

Hi Troon!

I still have one problem - two automations created for testing! The first should send me a message when my wife enters our home - this automation works! The second automation should send me a message when the number of people in our house increases from one to two - I am at home all the time! Unfortunately this automation does not work…

This works:

alias: Test Franzi is here
description: ""
trigger:
  - platform: zone
    entity_id: person.franzi
    zone: zone.home
    event: enter
condition: []
action:
  - service: notify.mobile_app_iphone_14_pro_max_michael
    data:
      message: Franzi is here
mode: single

This works not:

alias: Person 1 to 2
description: ""
trigger:
  - platform: state
    entity_id:
      - zone.home
    from: "1"
    attribute: persons
    to: "2"
condition: []
action:
  - service: notify.mobile_app_iphone_14_pro_max_michael
    data:
      message: Person 1 zu 2
mode: single

I’m afraid that my absence automation won’t work either:

alias: Lsst person leave the house
description: ""
trigger:
  - platform: state
    entity_id:
      - zone.home
    attribute: persons
    to: "0"
condition: []
action:
  - device_id: d67bf48b00ad0fd3620707c00c20095f
    domain: lock
    entity_id: lock.nuki_door_lock
    type: lock
mode: single

Have I perhaps implemented the query of the zones incorrectly?

Kind regards!
Michael

Remove the attribute: persons line from the trigger. The UI makes it look as though this is needed, but it isn’t.

1 Like

Ah… thanks a lot again! :slight_smile: