Automatically lock and open doors how achieve it?

Hello HA

I am new to home assistant and I been trying to wrap my head around yaml and configurations for homeassistant but it is quickly becoming overwhelming because there is so much to do. I wanted to move my automatic door lock to home assistant and also add an automation that opens my door when my phone connects to the wifi. I already have presence dection with unifi in home assistant but I am struggling with the automations, here is the code I have written so far which does not work properly:

      - alias: Automaticly lock door after 2 minutes if door is closed
        trigger:
          platform: state
          entity_id: lock.ytterdorr
          to: 'unlocked'
          for:
            seconds: 120
        condition:
          - condition: state
            entity_id: binary_sensor.ytterdorr_open
            state: 'off'
        action:
        - service: lock.lock
          entity_id: lock ytterdƶrr

    - alias: Automaticly open door when owners arrives home
        trigger:
          platform: state
          entity_id: person.me.lastname
          from: not_home
          for: 00:05:00
          to: home
        condition:
          - condition: and
            conditions:
              - condition: state
                entity_id: binary_sensor.ytterdorr_open
                state: 'off'
              - condition: state
                entity_id: lock.ytterdorr
                state: 'locked'
        action:
          - service: lock.unlock
            entity_id: lock.ytterdorr

      - alias: Automaticly lock door after 2 minutes when owners leaves home
        trigger:
          platform: state
          entity_id: person.me.lastname
          from: home
          for: 00:02:00
          to: not_home
        condition:
          - condition: and
            conditions:
              - condition: state
                entity_id: binary_sensor.ytterdorr_open
                state: 'off'
              - condition: state
                entity_id: lock.ytterdorr
                state: 'unlocked'
        action:
          - service: lock.lock
            entity_id: lock.ytterdorr

Anyone has any ideas on how to fix this? I am new so I need sample configs to be able to understand what to do.

  1. Try adding a quote on the time '00:05:00'
  2. The first automation entity_id should be lock.ytterdorr.

Note:

  1. By default all conditions are ANDed. You dont need to add AND condition.
  2. Make sure to check the actual state of entity in Developer Tools ā€”> States (tab). Sometimes it is different with the one displayed on the frontend.
1 Like

For the first automation it not going to trigger if it base on lockā€¦ its only trigger when you unlock the door not close. try this

- alias: Automaticly lock door after 2 minutes if door is closed
  trigger:
    platform: state
    entity_id: binary_sensor.ytterdorr_open
    to: 'off'
	for:
      seconds: 120
  action:
    - service: lock.lock
      entity_id: lock.ytterdorr

and what is the purpose you try to achieve here?

          from: not_home
          for: 00:05:00
          to: home

    to: home
    for:
      minutes: 5

Donā€™t you want it unlock straight away when owners come home?

- alias: Automaticly open door when owners arrives home
  trigger:
    platform: state
    entity_id: person.lastname
    to: home
  condition:
    - condition: state
      entity_id: binary_sensor.ytterdorr_open
      state: 'off'
    - condition: state
      entity_id: lock.ytterdorr
      state: 'locked'
  action:
    - service: lock.unlock
      entity_id: lock.ytterdorr 

- alias: Automaticly lock door after 2 minutes when owners leaves home
  trigger:
    platform: state
    entity_id: person.lastname
    to: not_home
    for:
      minutes: 2
  condition:
    - condition: state
      entity_id: binary_sensor.ytterdorr_open
      state: 'off'
    - condition: state
      entity_id: lock.ytterdorr
      state: 'locked'
  action:
    - service: lock.lock
      entity_id: lock.ytterdorr      

If I remove the AND I get a syntax error so I am not sure how to do it

Ok maybe I was not clear in the first post. This is what I want to achieve:

  1. I want the door the automatically lock if the door is closed binary_sensor.ytterdorr_open = off and the the lock is unlocked after 2 minutes.

  2. I want the door the unlock as soon as it senses that my phone or my spouses phone changes status from not_home to home and they have been away for more 5 minutes.

  3. I want the doors the lock if the door is closed and unlocked if both me and my spouse phone statuses changed from home to not_home. I want this to happen 2 mintues after the status has changed

There is a lot of opposition. You cant have ā€œandā€ in trigger. Also how can you be away for 5 min when you just got home. You can have an automation A to turn on an automation B after you ā€œnot_homeā€ for 5m. and automation B will unlock when you come home and turn itself off.

1 Like

Hey I am not sure I am following here I am really trying to but maybe it is better if we just focus on one thing. Lets take the most important one first that would be that it automatically opens when I arrive home and I will remove the other automations from now. Why would this one not work?

  - alias: Automaticly open door when owners arrives home
    trigger:
      platform: state
      entity_id: person.me.lastname
      from: not_home
      for: 00:05:00
      to: home
    condition:
      - condition: and
        conditions:
          - condition: state
            entity_id: binary_sensor.ytterdorr_open
            state: 'off'
          - condition: state
            entity_id: lock.ytterdorr
            state: 'locked'
    action:
      - service: lock.unlock
        entity_id: lock.ytterdorr

So logic in this one is:

  1. Status changes from not_home to home after 5 minutes and the door is closed + locked. In this case it should open. Reason I have the 5 minute delay is that I donā€™t want to lock to spasm unlock and lock when I am just out of range of the wifi

This is part wont work


      from: not_home
      for: 00:05:00
      to: home

correct, in the docs or somewhere GOD KNOWS , for is only applicable to the ā€˜toā€™ parameter.

if you want to really have the not_home for atleast 2 minutes as a condition, you will need to use a template based condition based on time likeā€¦

  - condition: template
    value_template: >
                {% if states.person.me.lastname.last_changed is defined %}
                  {{ ((as_timestamp(now()) - as_timestamp(states.person.me.lastname.last_changed)) / 60) >= 2 }}  
                {% else %}
                  False
                {% endif %}  

didnt test that, but someone can also probably tidy it up.