Need a little help with an door lock automation that is saying "stopped" in trace timeline

Hello all,

Can someone give me a hand to figure out why the automation below is not working? The trace timeline is saying “Triggered by state of group.family” and then “stopped because only a single execution is allowed (runtime 0.00 seconds)”

All this automation really does is lock the doors individually and send a notification with which door was left unlocked if one was left unlocked (3 doors and 1 garage door). Well, that’s what it’s supposed to do at least. Thank you all for troubleshooting this to help me. I am using the choose function with trigger id to help the chooses. Thank you all again for any help you can provide me

id: '1652196340033'
alias: (ACTION- AUTOMATIC- SECURITY- GEOLOCATION) Door Lock When Away
description: ''
trigger:
  - platform: state
    entity_id:
      - group.family
    from: home
    to: not_home
    for:
      hours: 0
      minutes: 5
      seconds: 0
    id: left_home_frontdoorlock
  - platform: state
    entity_id:
      - group.family
    from: home
    to: not_home
    for:
      hours: 0
      minutes: 5
      seconds: 0
    id: left_home_backdoorlock
  - platform: state
    entity_id:
      - group.family
    id: left_home_garageentrydoorlock
    from: home
    to: not_home
    for:
      hours: 0
      minutes: 5
      seconds: 0
  - platform: state
    entity_id:
      - group.family
    id: left_home_garagedoordown
    from: home
    to: not_home
    for:
      hours: 0
      minutes: 5
      seconds: 0
condition: []
action:
  - choose:
      - conditions:
          - condition: trigger
            id: left_home_frontdoorlock
          - condition: state
            entity_id: lock.front_door_lock
            state: unlocked
        sequence:
          - service: lock.lock
            data: {}
            target:
              area_id: living_room
          - service: notify.mobile_app_weston
            data:
              message: >-
                '{{ trigger.to_state.attributes.friendly_name }} Left unlocked
                with no one home. The door has been locked by automation.'
              title: Door Left Unlocked with no one home
      - conditions:
          - condition: trigger
            id: left_home_backdoorlock
          - condition: state
            entity_id: lock.back_door_lock
            state: unlocked
        sequence:
          - service: lock.lock
            data: {}
            target:
              area_id: kitchen
          - service: notify.mobile_app_weston
            data:
              message: >-
                '{{ trigger.to_state.attributes.friendly_name }} Left unlocked
                with no one home. The door has been locked by automation.'
              title: Door Left Unlocked with no one home
      - conditions:
          - condition: trigger
            id: left_home_garageentrydoorlock
          - condition: state
            entity_id: lock.garage_entry_door_lock
            state: unlocked
            for:
              hours: 0
              minutes: 5
              seconds: 0
        sequence:
          - service: lock.lock
            data: {}
            target:
              area_id: laundry_room
          - service: notify.mobile_app_weston
            data:
              message: >-
                '{{ trigger.to_state.attributes.friendly_name }} Left unlocked
                with no one home. The door has been locked by automation.'
              title: Door Left Unlocked with no one home
      - conditions:
          - condition: trigger
            id: left_home_garagedoordown
          - condition: state
            entity_id: cover.garage_door
            state: opened
            for:
              hours: 0
              minutes: 5
              seconds: 0
        sequence:
          - service: cover.close_cover
            data: {}
            target:
              area_id: garage
          - service: notify.mobile_app_weston
            data:
              message: >-
                '{{ trigger.to_state.attributes.friendly_name }} Left open with
                no one home. The door has been closed by automation.'
              title: Door Open with no one home
    default: []
mode: single

Why do you have four identical triggers?
They are not in any way (as far as I can see) related to the id name.

I would think something like this should work with the same functionality.

Essentially removing all the trigger_id’s from both triggers and actions

id: '1652196340033'
alias: (ACTION- AUTOMATIC- SECURITY- GEOLOCATION) Door Lock When Away
description: ''
trigger:
  - platform: state
    entity_id:
      - group.family
    from: home
    to: not_home
    for:
      hours: 0
      minutes: 5
      seconds: 0
condition: []
action:
  - choose:
      - conditions:
          - condition: state
            entity_id: lock.front_door_lock
            state: unlocked
        sequence:
          - service: lock.lock
            data: {}
            target:
              area_id: living_room
          - service: notify.mobile_app_weston
            data:
              message: >-
                '{{ trigger.to_state.attributes.friendly_name }} Left unlocked
                with no one home. The door has been locked by automation.'
              title: Door Left Unlocked with no one home
      - conditions:
          - condition: state
            entity_id: lock.back_door_lock
            state: unlocked
        sequence:
          - service: lock.lock
            data: {}
            target:
              area_id: kitchen
          - service: notify.mobile_app_weston
            data:
              message: >-
                '{{ trigger.to_state.attributes.friendly_name }} Left unlocked
                with no one home. The door has been locked by automation.'
              title: Door Left Unlocked with no one home
      - conditions:
          - condition: state
            entity_id: lock.garage_entry_door_lock
            state: unlocked
            for:
              hours: 0
              minutes: 5
              seconds: 0
        sequence:
          - service: lock.lock
            data: {}
            target:
              area_id: laundry_room
          - service: notify.mobile_app_weston
            data:
              message: >-
                '{{ trigger.to_state.attributes.friendly_name }} Left unlocked
                with no one home. The door has been locked by automation.'
              title: Door Left Unlocked with no one home
      - conditions:
          - condition: state
            entity_id: cover.garage_door
            state: opened
            for:
              hours: 0
              minutes: 5
              seconds: 0
        sequence:
          - service: cover.close_cover
            data: {}
            target:
              area_id: garage
          - service: notify.mobile_app_weston
            data:
              message: >-
                '{{ trigger.to_state.attributes.friendly_name }} Left open with
                no one home. The door has been closed by automation.'
              title: Door Open with no one home
    default: []
mode: single

Another approach would be using variables:



id: '1652196340033'
alias: (ACTION- AUTOMATIC- SECURITY- GEOLOCATION) Door Lock When Away
description: ''
trigger:
  - platform: state
    entity_id:
      - group.family
    from: home
    to: not_home
    for:
      minutes: >
        {{ states('input_number.security_delay') }} ## optional, advantage: survives restarts/reloads
condition:
  - condition: template
    value_template: '{{ open |count > 0 }}'
action:
  - service: lock.lock
    data: {}
    target:
      entity_id: '{{ doors }}'
  - service: cover.close_cover
    data: {}
    target:
      entity_id: '{{ covers }}'
  - service: notify.mobile_app_weston
    data:
      message: >
        {{ open |join(', ') }} left open with no one home and has been closed by
        automation.
      title: Door Open with no one home

mode: single
max_exceeded: silent
variables:
  doors: |
    {{ states.lock
      |selectattr('state', 'eq', 'unlocked') 
      |map(attribute='entity_id')
      |list }}
  covers: |
    {{ states.cover
      |selectattr('state', 'eq', 'open') 
      |map(attribute='entity_id')
      |list }}
  open: |
    {{ expand([covers, doors])
      |map(attribute='name')
      |list }}



The variables used here are deliberately kept universal. You can specify them as you wish.

Thank you for this @Hellis81. I will give this a go this afternoon. The reason I wrote it the way I did is I thought I needed a separate trigger id for each branch of the flow (I only want one branch of the flow to go into the action state based on which door is unlocked). Would the way I wrote it only function once and not four times like I thought?-- It looks like mine errored out after looking at the first door?

Essentially, I only want a notification if that portion of the flow starts (within the actions). Also, I don’t want the door to lock a second time if it is already locked which would cause me to get a message on something that had already occured.

I’m sorry, I think some of this is just me being new and not fully understanding how these automations work just yet. Could you speak to the 2 things above in order to help me understand how the system functions.

Also, would the concept linked below not really work either. Would it give me individual messages on a per door basis.

alias: (ACTION- AUTOMATIC- SECURITY- GEOLOCATION) Door Locked When Away (Choose)
description: ''
trigger:
  - platform: state
    entity_id:
      - binary_sensor.back_door
      - binary_sensor.front_door
      - binary_sensor.garage_entry_door
    to: unlocked
    id: door_unlocked_while_away
    for:
      hours: 0
      minutes: 5
      seconds: 0
  - platform: state
    entity_id:
      - cover.garage_door
    to: opened
    id: garage_door_open_while_away
    for:
      hours: 0
      minutes: 5
      seconds: 0
condition: []
action:
  - choose:
      - conditions:
          - condition: trigger
            id: garage_door_open_while_away
        sequence:
          - service: cover.close_cover
            data: {}
            target:
              area_id: garage
          - service: notify.mobile_app_weston
            data:
              message: >-
                service: notify.mobile_app_weston data:   title: SECURITY  
                message: '{{ trigger.to_state.attributes.friendly_name }}' was
                left unlocked. Locked by automation.
      - conditions:
          - condition: trigger
            id: door_unlocked_while_away
        sequence:
          - service: lock.lock
            data: {}
            target:
              area_id:
                - kitchen
                - laundry_room
                - living_room
          - service: notify.mobile_app_weston
            data:
              title: SECURITY
              message: '{{ trigger.to_state.attributes.friendly_name }}'
    default: []
mode: single

Yes.
Because of the condition:
(one at random)

      - conditions:
          - condition: state
            entity_id: lock.back_door_lock
            state: unlocked

The sequence below will only run if this is true, which would only happen if you want the door to lock.
If the door is already locked then it won’t run.
But the automation would probably trigger four times, but only once would it be allowed to run. ( that is what the error message is about)

It can’t because the condition won’t be true.

I think it could.
But this will lock the doors when you are home.

If I would do this kind of automation then I would create a sensor that holds the door names that is currently unlocked. Like “backdoor, front door and garage”.
Then when this automation runs you send one notification instead of one per door with all doors that is currently unlocked.

Thank you for your responses @Hellis81. I’m going to go through and clean up some of the thread in a bit. I’m realizing some of my previous code post are terrible. I now understand what you meant, when you said “all 4 of my triggers were the same”. I’m just trying to work too fast and need to slow down.

I have simplified my approach a great deal in the below just to better understand how things work. In the end, I will use your approach but the below is going to be for my education. Would the below work? I feel like it worked when I left home today but the notification said “The family was locked” instead of “The back door was locked”. How can I get it to say “back door” without “hard” coding it?

Or do I have the trigger and the condition backwards? Can you help me understand how I know where to put each one. – The way I read it in the way I have it written: When I go to not_home, and the back door is unlocked, do this action…

Thank you for all of the help you have provided me.

alias: (ACTION- AUTOMATIC- SECURITY- GEOLOCATION) Door Lock When Away (Back Door)
description: ''
trigger:
  - platform: state
    entity_id:
      - group.family
    from: home
    to: not_home
    for:
      hours: 0
      minutes: 5
      seconds: 0
condition:
  - condition: state
    entity_id: lock.back_door_lock
    state: unlocked
    for:
      hours: 0
      minutes: 5
      seconds: 0
action:
  - service: lock.lock
    data: {}
    target:
      area_id: kitchen
  - service: notify.mobile_app_weston
    data:
      title: SECURITY
      message: The {{ trigger.to_state.attributes.friendly_name }} was locked.
mode: single

Thank you for this idea. So to make sure I understand this properly. This is creating an array called “doors” and assigning any entity with the state “unlocked” to it? Are these local variables that will only be available to this automation?-- Sorry if these are dumb questions, I’m not a programmer so I am very rough with understanding programming. Can you help me understand the message with the following example: Let’s say the front door was left unlocked and the garage door was left opened. Thank you ahead of time @pedolsky for giving me an education with your very good idea!

I see what you are saying, in the second code I pasted, I didn’t even include anything about only running when the family group goes from home to not_home so that’s no good. So you would do something similar to @pedolsky if you we’re trying to make an automation for this.

You can’t really do that with trigger templates as you are trying to use.
That is why I said I would create a sensor that only reports in text what to include in the notification.
This can be templated to be dynamic in case you add another door somewhere.

No, it’s correct in my opinion.
But doing it the other way around would give you the trigger template working, BUT it will probably not function as you want it to.

If the trigger is lock is unlocked for five minutes then if you say hi to your neighbor for a while then the condition that you are not_home will be false.
What happens is that the automation stops and leavea the door unlocked.

You could however remove the for 5 minutes, that means if say front door is unlocked as you leave home zone less than 5 minutes after the door was unlocked then the door will be locked.
I think that would be a rather good automation.

That are no dump questions, HA isn’t always easy for beginners. Right, the variables only take opened covers and locks into account. They only work local in this automation.

If re are open doors / covers, the automation will create the following message (I used a persisting notification for testing purposes):

Please note, that I’m not using smart locks, so I replaced it with switches.

You can copy the whole thing into the ‘Template’ section in Developer Tools. To see results in a proper way, we need to translate the variables for use in Developer Tools. Copy this block first:


{% set covers = states.cover
        |selectattr('state', 'eq', 'open') 
        |map(attribute='entity_id')
        |list %}

{% set doors = states.lock
        |selectattr('state', 'eq', 'unlocked') 
        |map(attribute='entity_id')
        |list %}

{% set open = expand([covers, doors])
        |map(attribute='name')
        |list %}

———————-

<place here the automation code>

Drop the `for: ` line!

Then copy the automation code and paste it under the block above.

P.S.: Omit the line if you don’t have an input number helper.
I agree with Hellis that you should omit the option altogether. Imagine you are already sitting in the car for 5 minutes and then you can turn around because the doors are open.

I believe this will work great! I have one last unforeseen question though. The screenshot below should help with my question. I have installed a HACS integration called “keymaster” that helps me send codes to the door etc… It looks like it has created 3 additional “locks” (these are “virtual” entities that don’t exist in the physical world) that I don’t want to be reported on as it is creating double entries in the “message”. Is there anyway to have this notification only include entities that are in the “Kitchen”, “Living Room”, and “Laundry Room” areas? Thank you for any help you can provide @pedolsky as I am not knowledgeable enough to fix these issues myself just yet.

The last thing I can think of to ask is, is there a way to have the list of issues be displayed on their own line in the notification rather than be comma delimited. As the below. (If not, no problem but just thought I would ask because of how knowledgeable you are)
Ex.
The following have been left unlocked or open with no one home:
Front Door Lock
Garage Door
They have been locked or closed by automation.

I agree with you all and removed it. Thank you both!

Copy the following into Developer Tools / Template and see the difference:


#### Unlocked Locks by area:
{{ expand(area_entities('Kitchen'), area_entities('Living Room'), area_entities('Laundry Room'))
|selectattr('domain', 'eq', 'lock') 
|selectattr('state', 'eq', 'unlocked') 
|map(attribute='entity_id')
|join('\n') 
}}

#### Unlocked Locks by state:
{{ states.lock
|selectattr('state', 'eq', 'unlocked') 
|map(attribute='entity_id')
|join('\n') 
}}

#### Unlocked Locks by state with rejected regex
{{ states.lock
|rejectattr('entity_id', 'search', '_boltchecked_')
|selectattr('state', 'eq', 'unlocked') 
|map(attribute='entity_id')
|join('\n') 
}}

Replace the line |map(attribute='entity_id') with |map(attribute='name') and see what will happen.

is there a way to have the list of issues be displayed on their own line in the notification rather than be comma delimited

I absolutely have no idea how to format Jinja code, so this one is rather ugly, but at least inserts line breaks:


        message: >

          {{ ['The following have been left unlocked or open with no one home:']|join('\n') }}
          {{ open |join('\n') }}
          {{ [
          'They have been locked or closed by automation.']|join('\n')  }}
 

Thank you for these @pedolsky. I am starting to understand the filtering of entities etc. based on the examples you have sent me through creating this automation.

Ok, I will try this. Within the “open” variable, I see it is “mapping” to the “name”, I assume “name” within the open variable is converting all entity ids to friendly names? So I don’t really know what will happen if I replace |map(attribute='entity_id') with |map(attribute='name') but I will test as you said for learning.

No problem at all, you’re ahead of me by a great deal, I didn’t even know it was called “Jinja”. haha. Thank you for everything you have helped me with on this, I have learned a great deal. I will test this as I leave for work tomorrow morning. This morning, the message was sent to me, the only thing I noticed was it looked like the entity ids were sent in the message rather than the friendly names (I am talking from memory so I could be wrong-- maybe what I say were entities that didn’t have a friendly name and that would make sense)

Below is the code I am using up until this point:

alias: (ACTION- AUTOMATIC- SECURITY- GEOLOCATION) Door Lock When Away (Choose2)
description: ''
trigger:
  - platform: state
    entity_id:
      - group.family
    from: home
    to: not_home
condition:
  - condition: template
    value_template: '{{ open |count > 0 }}'
action:
  - service: lock.lock
    data: {}
    target:
      entity_id: '{{ doors }}'
  - service: cover.close_cover
    data: {}
    target:
      entity_id: '{{ covers }}'
  - service: notify.mobile_app_weston
    data:
      message: >
        The following have been left unlocked or open with no one home: {{ open
        |join(', ') }} . They have been locked or closed by automation.
      title: SECURITY
mode: single
max_exceeded: silent
variables:
  doors: |
    {{ states.lock
      |rejectattr('entity_id', 'search', '_boltchecked_')
      |selectattr('state', 'eq', 'unlocked') 
      |map(attribute='entity_id')
      |list }}
  covers: |
    {{ states.cover
      |selectattr('state', 'eq', 'opened') 
      |map(attribute='entity_id')
      |list }}
  open: |
    {{ expand( (doors), (covers) )
      |map(attribute='name')
      |list }}

Ah, I wasn’t clear enough, sorry for that. It was just meant for playing around in the Template Editor, not for use in your automation.

When copying the whole code into Template Editor, you get the following output (remember, I’m using switches instaead of locks):


condition:

#### The condition is fulfilled because counting resulted in greater than 0 = covers are open and/or switches are on

  - condition: template
    value_template: 'True'

action:
  - service: lock.lock
    data: {}
    target:

#### You need the entity ids here! Thus you have to map the attribute entity_id

      entity_id: '['switch.echo_show_do_not_disturb_switch', 'switch.hue_bewegungsmelder_flur_illuminance', 'switch.hue_bewegungsmelder_flur_motion']'
  - service: cover.close_cover
    data: {}
    target:
      entity_id: '['cover.schlafzimmer', 'cover.tradfri_blind']'
  - service: notify.mobile_app_weston
    data:
      message: >

#### You want no entity ids in the message, so you map the attribute name

The following have been left unlocked or open with no one home:
Rollo Schlafzimmer
Echo Show do not disturb switch
Hue  Bewegungsmelder Flur Motion
They have been locked or closed by automation.

I completely understand now. I will test a bit more when I get home this evening and when I leave for work in the morning. I will see if everything looks great in the message.

If so, I will come back and past here for others that are new as myself. I have a good deal of learning to do with regards to templating, yaml, and jinja. Thank you for all you have taught me in such a small amount of time!!

@pedolsky

I hate to even ask this but you have been incredibly helpful and I’m trying my best to learn as you show me things. Can you show me how to apply this same concept to another instance. It would be doors that were open when away from home. In my instance I have 3 doors that have contact sensors on them and a garage door. The doors report “on” and “off” and the garage door reports “opened” and “closed”. I sat down to do this one by myself and as soon as I got into it, I realized I had issues outside of what I learned on the other example.

The hopes would be to build some type of filtering mechanism with the selectattr, rejectattr that would make it to where additional doors would get added automatically as long as I keep the same naming conventions. (I was looking at my entity list and its possible that the above may not be possible because I have tons of entities with the word “door” in them so these may have to be hardcoded somehow, I don’t really know how to do that either-- I saw you used the “search” parameter but that looked like that was wildcard based and almost all of the doors seem to have an entity that would interfere with that possibility (i.e.) there is an entity called binary_sensor.back_door_network)

The doors entity ids are: binary_sensor.front_door, binary_sensor.back_door, binary_sensor.garage_entry_door and the friendly names are: Front Door, Back Door, Garage Entry Door:

The last thing I will try to apply your concepts to (hopefully on my own, because I kind of feel like a bother) is when a water sensor gets wet. The water sensors report “on” and “off” also with entity names of: binary_sensor.dishwasher_water_sensor, binary_sensor_hot_water_heater_water_sensor, binary_sensor.icemaker_water_sensor, and binary_sensor.washing_machine_water_sensor, and friendlies of: Dishwasher Water Sensor, Hot Water Heater Water Sensor, Icemaker Water Sensor, and Washing Machine Water Sensor.

A last question would be, with these entities, is there anyway to get them to report in a proper context like “wet” or “dry” instead of binary “ons” and “offs”. It always screws me up when I get in there and try to start buidling automations etc. and have to think in that context. I’ve noticed the device reports “wet” or “dry”, “closed” or “open” but the entity of the device reports in binary. Is this just the way things work, so in other words, “just get used to it”?

This is a binary world :wink:
„Wet“, „open“, „normal“ etc. are mmh… ‘pretty’ states that will displayed in the frontend. The actual state (e.g. on/off) can only be changed by creating a custom template (binary) sensor.

I hope I get you right. Do you want to create another automation or should the binary sensors added to your existing one?

Oh my goodness. You could add all of this to one automations? Unlocked doors, water sensors, and open doors/ open garage doors.

Wow, I would say for my learning, let’s do the water sensors in one by their self and the open doors in one by theirself.

Im afraid if we start doing too much in one automation, I won’t be able to understand it because of complexity.

The water sensors, I would want to notify no matter what, whether home or not. And the open doors/ garage door, I would only want to notify when in “not_home”.

Thank you for helping me with this in this thread. I hope others will be able to learn from it when they start into home assistant also.

No problem. Paste the following code into the Template Editor:



#### Binary sensors filtered by device_class
{{ states.binary_sensor
|selectattr('attributes.device_class', 'defined') 
|selectattr('attributes.device_class', 'in', ['moisture', 'door']) 
|map(attribute='entity_id')
|list
}}

Please confirm that all of your relevant binary sensors are covered. If not, look under Developer Tools —> States or use this snippet to find out the device class.


#### device_class, e.g.
{{ state_attr('binary_sensor.dishwasher_water_sensor', 'device_class') }}

So my doors look to have a device class of “opening” as they are contact sensors. So when I copy-paste the below into developer tools- template. I do get the proper list of items back (all contact sensors and moisture sensors). In my instance for the notifications, I would like one notification for the wet sensors and one for the open doors. So I would use these in a variable like you taught me in the very first example (in their own individual automations)?

This is great! Thank you for helping me understand this stuff.
Thank you so much @pedolsky

When I get home this evening, I will post the 3 end result codes here for others to be able to review and use with small changes.

  1. Notification for any door being unlocked or garage door being left open with no one in the family group home. (with automated locking or shutting in the garage doors instance)
  2. Notification of any water sensor being triggered because of water. (at any time)
  3. Notification of a door being left open with no one in the family group home. (sorry, no automated shutting sadly, haha)
{{ states.binary_sensor
|selectattr('attributes.device_class', 'defined') 
|selectattr('attributes.device_class', 'in', ['moisture', 'opening']) 
|map(attribute='entity_id')
|list
}}