Using templates for automation if/else true/false

Hi,
my first attempt in using templats - of course not successful. Idea is, to trigger an automation only, if I had been out of the house for at least 10 minutes, to not constantly turn my lights on if there is a little gap in the wifi connection.

works fine in the template editor, but somehow that automation does not accept true/false as values to continue.

I’m using hass.io on a raspberry pi.

{%set stamp_now = as_timestamp(utcnow())%}
{%set stamp_gone = as_timestamp(states.automation.leaving_the_house.last_changed)%}
{%set time_since_gone = ((stamp_now-stamp_gone)/60)| round(0) %}
Left the house {{time_since_gone}} min ago
{% if time_since_gone > 10 %}
true 
{%- else -%}
false
{%- endif %}

hassio tempalte

This:

{% if time_since_gone > 10 %}
true 
{%- else -%}
false
{%- endif %}

Is equivalent to this:

{{ time_since_gone > 10 }}

This will evaluate to true or false depending on the time.

Where is this template located?

Your template will only work as a condition, not a trigger. now() is a function and does not update its state until called.

2 Likes

You can distill it to this:

{{ utcnow().timestamp() - states.automation.leaving_the_house.last_changed.timestamp() > 600 }}

It’s just the current timestamp minus the last_changed timestamp. If the result of the subtraction exceeds 600 seconds (10 minutes) the template reports true (otherwise it’ll report false).

2 Likes

Well put bro

for me thats been the hardest part write less get the same answer