Automation to unlock my door when I come home

I want my front door to unlock when I am home and connected to bluetooth.

I have a device tracker that will connect (state: home) when I am just right outside my front door. Only problem is that I put my device in “low power” mode at night and it will disconnect some times during the night. And when I am connected again it will unlock the door - i do not want that.

I have other device trackers on a group - that will tell HA that I am home. (icloud, nmap and geofancy).

Is there a way I could make an automation, like this:

  1. When my state is set to Not_Home (group with device trackers bluetooth, icloud, nmap and geofancy) for more than 10 minutes.
  2. When I am back in my home zone after more than 10 mintues.
  3. Connected to device tracker bluetooth.
  4. Trigger door opening switch.

I have this for now, but it does now work:

- id: aut777
  alias: Automatisk unlock ved bluetooth device tracker home
  initial_state: 'on'
  trigger:
    - platform: state 
      entity_id: group.tracker_group_borge
      to: 'not_home'
      for:
        minutes: 10 
  condition:
      condition: and
      conditions:
        - condition: state
          entity_id: device_tracker.iphone_x
          state: home
        - condition: time
          after: '09:00:00'
          before: '23:29:59'
  action:
      - service: switch.toggle
        entity_id: switch.yale_lock
      - service: notify.pushover
        data:
          title: "Casa de Olsen:"
          message: "Dør låst opp - sjekk hvis lengre enn 10 min borte"

Why don’t you just use the group with all the device_trackers? It will be ‘home’ when any of them are ‘home’, and will be ‘not_home’ when all of them are not ‘home’, right?

Yes, that is possible.

But my problem is that when I am using “low power” mode on my phone some times ALL of the devices will say “not_home”. This does not happen often, but it happens. And then it will unlock the door when all devices are “home” and bluetooth is connected. Thats why I would like a timer saying that last “not_home” status should be at least 10 minutes.

I am not sure if this is possible to do.

- alias: xxx
  trigger:
    platform: state
    entity_id: group.MY_DEVICES
    to: 'home'
  condition:
    condition: template
    value_template: >
      {{ (as_timestamp(now()) -
          as_timestamp(trigger.from_state.last_updated))
         .total_seconds() > 10*60 }}
  action:
    ...

Thank you so much!

I would also need another condition:

 - condition: state
          entity_id: device_tracker.iphone_x
          state: home

Can I add that to your code in any way? Because I need the door to open only when I first get a bluetooth connection.

Would this work?

- id: 'unlock_bt'
  alias: Unlock bluetooth
  initial_state: 'on'
  trigger:
    platform: state
    entity_id: group.tracker_group_borge
    to: 'home' 
  condition:
      condition: and
      conditions:  
        - condition: state
          entity_id: device_tracker.iphone_x
          state: home
        - condition: template
          value_template: >
            {{ (as_timestamp(now()) -
                as_timestamp(trigger.from_state.last_updated))
               .total_seconds() > 10*60 }}
  action:
      - service: switch.toggle
        entity_id: switch.yale_lock
      - service: notify.pushover
        data:
          title: "Casa de Olsen:"
          message: "Dør låst opp!"

HI,

Would the template with the as_timestamp() 's be in any way preferable to this:

      {{ (now() - trigger.from_state.last_changed | default(0)).total_seconds() > 
                                        states('input_number.presence_timer')|int }}

(nevermind the input_number, I only mean to ask about the now() / as_timestamp() )

must confess I believe my template works fine…?

Actually, you’re right. My template should not have had the .total_seconds() part. So either:

        - condition: template
          value_template: >
            {{ (as_timestamp(now()) -
                as_timestamp(trigger.from_state.last_updated)) > 10*60 }}

or

        - condition: template
          value_template: >
            {{ (now() - trigger.from_state.last_updated)
               .total_seconds() > 10*60 }}

@borgeolsen, yes, that looks like it should work, but of course you need to fix my mistake as per above.

1 Like

Just did a test with the new code, it did not open the door when bluetooth connected.

Did I enter the code correctly?

- id: 'unlock_bt'
  alias: Unlock bluetooth
  initial_state: 'on'
  trigger:
    platform: state
    entity_id: group.tracker_group_borge
    to: 'home' 
  condition:
      condition: and
      conditions:  
        - condition: state
          entity_id: device_tracker.iphone_x
          state: home

        - condition: template
          value_template: >
            {{ (now() - trigger.from_state.last_updated)
               .total_seconds() > 1*60 }}
  action:
      - service: switch.toggle
        entity_id: switch.yale_lock
      - service: notify.pushover
        data:
          title: "Casa de Olsen:"
          message: "Dør låst opp!"

Is the bluetooth tracker in group.tracker_group_borge? Also is device_tracker.iphone_x in that group, too?

1 Like

Yes the bt tracker is also in that group.

At the moment this group contains the bt tracker and geofency tracker.

So it contains the bt tracker and the geofency tracker, and device_tracker.iphone_x is yet another tracker, not in that group. Is that right?

The automation should trigger whenever both the bt tracker and geofency trackers have been not ‘home’ for at least one minute, and then one of them changes to ‘home’. Is that what you tried and it didn’t trigger?

1 Like

it contains bt tracker and geofenct tracker. device_tracker_iphone_x is the bt tracker id.

Yes I tried with the whole group away for more than one minute. Then came back home and saw that the geofency tracker changed back to home. And then bt tracker connected just outside my door.

Ok, first, I made a mistake. You should be using trigger.from_state.last_changed not trigger.from_state.last_updated. last_changed is the time of the last state string change, whereas last_updated is the time when any part of the state, including attributes, changed.

Second, if device_tracker.iphone_x is the bt tracker, then you don’t need that first condition. The group can’t be ‘home’ if this tracker (which is in the group) isn’t.

So try this:

- id: 'unlock_bt'
  alias: Unlock bluetooth
  initial_state: 'on'
  trigger:
    platform: state
    entity_id: group.tracker_group_borge
    to: 'home' 
  condition:
    condition: template
    value_template: >
      {{ (now() - trigger.from_state.last_changed).total_seconds() > 1*60 }}
  action:
      - service: switch.toggle
        entity_id: switch.yale_lock
      - service: notify.pushover
        data:
          title: "Casa de Olsen:"
          message: "Dør låst opp!"

This should trigger whenever group.tracker_group_borge was in any state other than ‘home’ for one minute, and then either of the trackers in group.tracker_group_borge changes to ‘home’.

Just to be clear, trigger.from_state is a state object that represents the previous state of group.tracker_group_borge (i.e., before it changed to ‘home’.) And its last_changed field records when it changed to that previous state.

EDIT: Yep, I realized the crossed out statement above was wrong right after I clicked Reply. :frowning:

1 Like

Wait a second, now I’m getting confused…

1 Like

The group will change to “home” if any of the two devices are home, it does not matter that the bt tracker is not connected, it still says that I am home. And I think it will have to also have the BT tracker as a condition, so it will only unlock the door when I am just outside front door.

Going back to the top, you said:

What group is that? I thought that was group.tracker_group_borge, but now you’re saying that only has bt and geofency.

Let’s take a step back.

First, what event or events do you want to unlock the door. And by events I mean when what entity or entities change state?

Next, when that event, or one of those events, occurs, under what conditions do you want the door to actually be unlocked?

Lastly, exactly what group or groups do you have defined that relate to this automation, and exactly what entities does each contain?

1 Like

My bad. The group did have all these trackers in it, but I removed some of them just to make the “not_home” status to work a little better when testing. Some of the trackers did use 5-10 minutes to understand that I was not home any more.

I want to open the door:

  - service: switch.toggle
    entity_id: switch.yale_lock

I only want this to happen automatically when I have been marked “not_home” for more than 1 minute. This so that when trackers that can go into “not_home” due to low power mode on my phone, will not trigger the automation. I tried one automation when I used state change to “home”, and then my bt tracker connected to unlock, but that resulted in the door being opened one or two times during the night.

It is also important that the door will only open when the BT device (device_tracker.iphone_x) is connected.

The group used to track if I am home is this:

tracker_group_borge:
    entities:

      - device_tracker.iphone_x        #bluetooth
#      - device_tracker.owntracks_iphonex
      - device_tracker.96525c0556f54ce88c5eda7843d2ad1b     #geofancy
#      - device_tracker.4098ad917b16   (nmap)
#      - device_tracker.iphonex   (icloud)

Please note that this group will mark me as “home” when the geofancy tracker is within my home area. It does not need both devices “connected” before it changes to home.

Thank you so much for helping me! And sorry for my bad english :slight_smile:

No, your English is fine. It’s just that we hadn’t discussed this in two days and in that time I forgot the details. Then some of the details changed since then. :slight_smile:

I think I understand now what you want. The problem is that the geofency tracker will change state first, which will also change the state of the group. Then when the bt tracker changes, it’s too late to check how long ago the group had been in its previous state before the geofency tracker changed it (because that information is now gone, at least from the state machine. It is in the history, though, assuming you have that enabled.)

I suppose one could figure out how to extract that lost information from the history, but a simpler way might be to have an input_boolean that records whenever the group changes to ‘home’ if it had been not ‘home’ for at least one minute (or whatever you ultimately decide to make the time threshold.) Then, when the bt tracker changes to ‘home’, the input_boolean can be used as a condition in the automation.

So, how about this:

input_boolean:
  away_long_enough:
    initial: off
automation:
  - alias: Record if away long enough
    trigger:
      platform: state
      entity_id: group.tracker_group_borge
      to: 'home'
    action:
      service_template: >
        input_boolean.turn_{{
          'on' if (now() - trigger.from_state.last_changed).total_seconds > 1*60
          else 'off' }}
      entity_id: input_boolean.away_long_enough

and:


  - alias: Unlock bluetooth
    trigger:
      platform: state
      entity_id: device_tracker.iphone_x
      to: 'home'
    condition:
      condition: state
      entity_id: input_boolean.away_long_enough
      state: 'on'
  action:
    - service: switch.toggle
      entity_id: switch.yale_lock
    - service: notify.pushover
      data:
        title: "Casa de Olsen:"
        message: "Dør låst opp!"
1 Like

Great job! Thank you so much!!!

I will try this later tonight at let you know how it worked :slight_smile:

1 Like