Device Tracker Timing

I’m still working with a simple automation to turn on the lights when an individual comes home. For this to function properly, I only want this to occur when no one is currently home, and a tracked person arrives. The basic .yaml file I came up with for this is:

- id: '1578666729834'
  alias: Interior lights on when front door unlocks after sunset
  description: ''
  trigger:
  - entity_id: lock.schlage_allegion_be469_touchscreen_deadbolt_locked
    platform: state
    to: unlocked
  condition:
  - condition: or
    conditions:
    - after: sunset
      after_offset: "-01:30:00"
      condition: sun
    - before: sunrise
      condition: sun
  - condition: and
    conditions:
      - condition: state
        entity_id: 'device_tracker.donnas_iphone'
        state: 'away'
      - condition: state
        entity_id: 'device_tracker.jons_iphone'
        state: 'away'
      - condition: state
        entity_id: 'device_tracker.device_name_here'
        state: 'away'
  action:
  - scene: scene.front_door_unlock_lights_on

The one thing I’ve noticed is that the iPhones are latching onto the system while coming up the walkway, which kind of defeats the automation because it then assumes someone is home prior to the door being unlocked. So my question is this:

Is there a way to set a timer to allow initiation of this automation as someone enters the fence and comes into the house after the device has registered that person as home?

Perhaps use a ‘for 1 minute’ statement in your conditions.

@Emphyrio, interesting…

So I’m trying to wrap my arms around this. The example given in the Conditions page has a person away for a set period of time using the for statement. I’m wondering how this would fit properly.

So, I only want this automation to work if no one is home. If one or two persons are home of the three, I don’t want it to initiate (this is of course because if someone’s home, no one needs the lights turned on automatically for them). So if all three people are gone, and one returns, that’s when I want it to initiate. But how would the time idea work for that? Are you thinking in reverse – where we would be timing how long all three would be gone, and initiate the sequence when one returns, regardless if the phone latches on as home?

Personally I use a template sensor called someone_present. It is on if anyone is home. You could also put your device trackers in a group for the same effect. Then you can can use ‘for 1 minute’ on that.

I can post some configuration if you want it?

Thanks @Emphyrio. If you could post it that would be great! I’m still in a bit of a learning curve here and it helps to see successful code.

In groups.yaml you can create a group like this:

people_present:
  name: People present
  entities:
    - device_tracker.donnas_iphone
    - device_tracker.jons_iphone
    - device_tracker.device_name_here

If one of the phones is at home, the group will have state ‘home’. If all phones are out of the house, the state will be ‘not_home’.

Then the conditions for your automation could be:

- condition: state
  entity_id: group.people_present
  state: 'home'
- condition: template
  value_template: "{{ (as_timestamp(now()) - as_timestamp(states.group.people_present.last_changed))|int < 120 }}"

The condition template basically says: the last state change of the group ‘people_present’ should be less than 120 seconds ago. So someone arrived at the house less than 2 minutes ago, thus accounting for the ‘connecting at the walkway’ part.

Just being creative here, this is not actually something I use myself. Do you think it fits your case?

@Emphyrio, this is very cool! Thanks! I will definitely give it a try. The string you wrote under value_template is new to me. I’ll have to learn more about formatting statements like that.

One question: What if – for some reason – the link isn’t made to designate the user as “home”? I’m thinking that sometimes the phone might not link until the door is open, depending on verities in the signal. So the person could under some circumstances, approach and open the door prior to the “home” link being made. What are your thoughts about that?

Thanks again for your help so far. This has been very educational.

You can nest the conditions with AND / OR. Just like in your original posting. ‘Or the state of the group is not_home’. (not at a pc atm :upside_down_face:)

@Emphyrio, thanks again for the quick response. I’m sitting in an airport terminal right now and won’t be able to try all of this until this weekend, but I’ll be sure to give you some feedback when I get it up and running. You’ve been very helpful!