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:
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.
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?
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:
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.
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 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?
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…
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.