Template/Automation Question About Home Arrival

I’m not the best with templates, and I may be missing something super simple. so I was hoping maybe someone would have some fancy way to do this easily…

I am adding a condition to an automation to prevent it from triggering if someone came home recently… for that I have this:

{{ as_timestamp(now()) - as_timestamp(states.group.family.last_changed) < 300}}

But what I would actually like is it to be only if that state change was to ‘home’ but I can’t seem to find how to do this with a specific state… also I would like it to evaluate true if ANY member of the group.family arrived home recently, not just the state of the whole group… I didn’t know if that would require me to make a separate template sensor with each person in order to do so.

Second part of my question is in regards to the automation trigger.

  trigger:
  - platform: template
    value_template: "{{ state_attr('binary_sensor.front_door','lock') == 'unlocked' and state_attr('binary_sensor.front_door','door') == 'closed'}}"
    for:
      minutes: 1

I think like this it will only evaluate on changes to the door lock being locked/unlocked or being opened/closed… am I correct to assume that at the one minute mark of being closed and not locked, if the condition is not met it will never check it again unless I change it to a time pattern trigger or something?

This is the easy part.

If any member of the group is home the group will be ‘on’ by default. For a group of device trackers or persons that will show as the state being ‘home’.

That said you should be able to test if the state of the group is home AND <300 seconds have elapsed.

I think something like this should work:

{{ is_state('group.people', 'home') and (as_timestamp(now()) - as_timestamp(states.group.family.last_changed) < 300) }}

or if you use the group state change as the trigger you could use the trigger object to get the to_state:

{{ trigger.to_state.state == 'home' and (as_timestamp(now()) - as_timestamp(states.group.family.last_changed) < 300) }}

Would that still work if the group is already marked as home, and someone else in the group came home? My thought was that wouldn’t work because the state of the group isn’t actually changing if there is already people at home. Or does it see the change inside the group as well?

Also, someone recently posted how to test trigger.to_state or trigger.entity_id in the template editor using {% set trigger= or something… I can’t seem to find that again… any chance you know what that was.

Ah, I missed that subtlety in the question.

No I don’t think it will.

I can’t think of an “easy” way to do it in a condition without listing out each person in the group separately.

Again if you are using the people as triggers it could be easier, tho.

It’s hard to tell if there is a better solution tho since you haven’t given much context and/or posted an automation.

Sorry, it’s just for locking the door… but today when we came home I unlocked the door from my watch, but the kids were running around in the yard… so by the time I went to go in it re-locked… so I realized that there is many cases where that may happen and I would want it to stay unlocked a little longer when we first arrive.

#####################################################
- alias: Front Door Auto Lock
  trigger:
  - platform: template
    value_template: "{{ state_attr('binary_sensor.front_door','lock') == 'unlocked' and state_attr('binary_sensor.front_door','door') == 'closed'}}"
    for:
      minutes: 1
  condition:
  #  - condition: time
  #    after: '23:20:00'
  #    before: '05:00:00'
    # - condition: template
    #   value_template: "{{ state_attr('binary_sensor.front_door','door') == 'closed' }}"
    - condition: template
      value_template: "{{states('sensor.home_assistant_not_restarted_recently')}}"
  action:
  - service: notify.alexa_media
    data_template:
      target: 
        - media_player.echo_show
        - media_player.living_room_echo
        - media_player.kitchen_dot
      data:
        type: announce
      message: > 
        {% if state_attr('binary_sensor.front_door','door') == 'closed' %}
           "I'm locking the Front Door for you"
        {% elif state_attr('binary_sensor.front_door','door') == 'open' %}
           "It seems you left the Front Door open, please close and lock it"
        {% endif %}
  - service: notify.mobile_app_brians_iphone
    data:
      message: 'Front Door Auto-Locking'
  - delay: '00:00:03'
  - data:
      entity_id: lock.lock_front_door_lock
    service: lock.lock

I’ve played around a bit and I’ve got something to try but I’m not really in a position to do much testing on it.

Here is the template that will look at all of the people in your group and tell you the timestamp for the last person to get home (I think…):

{{ expand('group.all_persons') | selectattr('state', 'eq', 'home') | map(attribute='last_changed') | list | max }}

Just replace your group name in for mine.

I believe the timestamp is in UTC so you’ll either have to convert it to your timezone or convert your now() function to UTC.

I’ll look at doing that too but I just wanterd to give you something to start playing around with.

EDIT:

I think the easiest way to do it would be to use:

{% set last_home = expand('group.all_persons') | selectattr('state', 'eq', 'home') | map(attribute='last_changed') | list | max %}
{{ (as_timestamp(utcnow()) - as_timestamp(last_home)) > 300 }}

That worked !!! thank you!

 {{ (as_timestamp(now()) - as_timestamp(expand('group.family') | selectattr('state', 'eq', 'home') | map(attribute='last_changed') | list | max)) > 300 }}

i did this before i saw your example in the bottom of the post (im on mobile) but this seems to work, anything wrong with this way that i may be missing?

Did you see my last edit?

So you didn’t need to convert them both to the same TZ?

HA! we’re playing “edit tag”… :laughing:

I was just wondering if they are for sure both in the same TZ for the original template.

When I was testing I’m pretty sure my last_changed attribute was in UTC but now() is in local time so they could be hours apart. Mine was 5 hours off.

THat’s why I suggested usiong utcnow() to do the comparison instead of trying to convert the last_changed timestamp to local. It was easier for me that way.

I don’t seem to need to convert it, after forcing a state change from home to not_home on one of the person entities in my group I get 1.43… (assuming seconds from the state change correct?)

I didn’t get a chance to try the first example before the edit without the utcnow…

Well, it’s good to know it all worked. I had to dig a bit to come up with that one. :wink:

Thank you again, I know a lot of you guys are crazy good with the templates, otherwise I was gonna struggle making some ridiculous template sensor listing every person in the group with a timestamp equation for each…

It’s these little things that make home automation awesome… when you think to yourself… I wish this would do “this” instead…and you can make it happen.

1 Like