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:
When my state is set to Not_Home (group with device trackers bluetooth, icloud, nmap and geofancy) for more than 10 minutes.
When I am back in my home zone after more than 10 mintues.
Connected to device tracker bluetooth.
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?
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.
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?
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.
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.
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.
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 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.
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
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.
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!"