Template for NOT home FOR some minutes

I am trying to turn on a boolean when I am leaving home for 30 minutes, but I cant figure this out.
This is what I have so far but it is not triggering.

  - platform: template
    value_template: "{{ (states.device_tracker.mytracker.state != 'home' and ((as_timestamp(now()) - as_timestamp(states.device_tracker.gmytracker.last_changed)) / 60 )| int > 30) }}"

Being ahead, I want to explain that using the simple state trigger cant work:

  - platform: state
    entity_id: device_tracker.mytracker
    to: 'not_home'
    for:
      minutes: 10

The reason this will not work, is because I can be in less that 10 minutes in another zone. That will not trigger the boolean as the state will not be ā€œnot_homeā€ for 30 minutes.
Thats why I am using this template and this argument ā€œ!=ā€ so this will limit the trigger for just when the state is NOT home, no matter what the state isā€¦

but couldnā€™t you do a similar thing only in reverse:

  - platform: state
    entity_id: device_tracker.mytracker
    from: 'home'
    for:
      minutes: 10

but if that doesnā€™t work then you will not be able to use as_timestamp(now()) in your template because it doesnā€™t cause the template to update on a change. you have to use a date/time sensor:

then you can use this as your template:

{{ ((as_timestamp(states.sensor.date.state + ' ' + states.sensor.time.state) - as_timestamp(states.device_tracker.gmytracker.last_changed)) / 60 ) | int > 30 }}

1 Like

When you use the ā€œforā€ argument, you can only use ā€œtoā€.

I used your template and it still didnt work for me:

  - platform: template
    value_template: '{{ (states.device_tracker.mytracker.state != "home" and ((as_timestamp(states.sensor.date.state + " " + states.sensor.time.state) - as_timestamp(states.device_tracker.mytacker.last_changed)) / 60 ) | int > 1) }}'

if you notice, I changed the single quotes with double because I use them at the beginning and the end of the template. Could this be a problem?

EDIT:
Forget what I said. It works correctly! I just had to wait one more minute! Thank you very much for the template. I just used 1 minute to test the trigger, but I will go for the real deal now. I think it will still work. I will come back if not. Thank you again!

It shouldnā€™t.

Do you have the date & time sensors in your config?

Yea, I edited my post same time with your post. It is working :slight_smile:

Iā€™m kind of trying to do the opposite. I want to run an automation after Iā€™ve entered a zone for X minutes. I have a working automation right, now but with 6 triggers, one for each zone. I was going to do

from: not_home
for:
minutes: X

but realized that does not work. Would the template above work the same way if I just changed it like so

  - platform: template
    value_template: '{{ (states.device_tracker.paul_all.state != "not_home" and ((as_timestamp(states.sensor.date.state + " " + states.sensor.time.state) - as_timestamp(states.device_tracker.paul_all.last_changed)) / 60 ) | int > 6 ) }}'

It should beā€¦ It looks correct to me. Have you tried it?

I have to admit Iā€™m having trouble making this human readable.

value_template: '{{ (states.device_tracker.life360_marj.state != "home" and ((as_timestamp(states.sensor.date.state + " " + states.sensor.time.state) - as_timestamp(states.device_tracker.life360_marj.last_changed)) / 60 ) | int > 1) }}'

Would someone mind deconstructing it? I can never manage to make it be ā€˜trueā€™ in the template editor, despite the fact marjā€™s life360sensor has recently updated.

Iā€™ve taken your template and modified it with my details and deconstructed it the template editor.

Here are the results:

2 Likes