Unlock door when entering zone

Hello all,

I use the Wyze smart lock but the auto-unlock feature works about 60% of the time while using an insane about of battery.

I’ve been using Home Assistant as a device tracker via GPS for a while and for the most part, it work really well with less battery usage.

I wish to use HA to unlock the Wyze lock when a device comes home but with a condition that the phone must be away from the home zone for x minutes to prevent HA from unlocking the door if the phone just bounces out of the zone for a few seconds.

I’ve created the following automation:

alias: Unlock side door when X get home
description: ""
trigger:
  - platform: state
    entity_id:
      - device_tracker.phone_gpslogger
    to: home
condition:
  - condition: time
    weekday:
      - mon
      - tue
      - wed
      - thu
      - fri
      - sun
      - sat
    after: "06:00:00"
    before: "22:00:00"
  - condition: and
    conditions:
      - condition: state
        entity_id: device_tracker.phone_gpslogger
        state: not_home
        for:
          hours: 0
          minutes: 2
          seconds: 0
action:
  - service: lock.unlock
    data: {}
    target:
      device_id: 11e1b77295609d2427a94aab098c4c0f
mode: single

It’s pretty simple but the issue is when the user gets home, the automation doesn’t trigger because the condition of state: not_home for 2 minutes is no longer valid.

What would be the best way to have HA unlock my door but only if a device has been away from home for at least 2 minutes?

Thanks!

Instead of the state condition, try a template condition that checks trigger.from_state.last_changed. Also, you don’t need to specify the and condition, which is the default for a list of conditions. So something like:

condition:
  - condition: time
    weekday:
      - mon
      - tue
      - wed
      - thu
      - fri
      - sun
      - sat
    after: "06:00:00"
    before: "22:00:00"
  - condition: template
    value_template: >
      {{ (now() - trigger.from_state.last_changed).total_seconds() >= 120 }}

Basically when the trigger fires, it will create a variable called trigger. For a start trigger, it has a field called from_state, which is the state the entity changed from (i.e., it’s last state.) The last_changed field of that state records when it changed to that state. So, we’re testing how long ago the entity changed to its previous state (i.e., the state it was in before it changed to home and triggered the automation.)

1 Like

Thanks for the suggestion! I will give that a test and see if the action triggers when I arrive at home tomorrow.

Cheers!

Unfortunately, that didn’t work. Here is the trace but it seemed to fail at the value_template:

I have never used vaule_templates before and I am unfamiliar with the syntax.

Any suggestions would be greatly appreciated!

id: '1665150846369'
alias: Unlock side door when xx gets home
description: ''
trigger:
  - platform: state
    entity_id:
      - device_tracker.phone_gpslogger
    to: home
condition:
  - condition: time
    weekday:
      - mon
      - tue
      - wed
      - thu
      - fri
      - sun
      - sat
    after: '06:00:00'
    before: '22:00:00'
  - condition: template
    value_template: '{{ (now() - trigger.from_state.last_changed).total_seconds() >= 120 }}'
action:
  - service: lock.unlock
    data: {}
    target:
      device_id: 11e1b77295609d2427a94aab098c4c0f
mode: single