Template trigger not working in practice

Hi all!

I have wrote a trigger that should get fired when nobody has been seen in kitchen for 20 seconds:
{% if as_timestamp(now())-as_timestamp(states.switch.kok_rorelse.last_changed) > 20 %}true{% else -%}false{%endif %}
But it never got fired. In the Developer Toolbox for template i got “true”.
When I for a test change to
if as_timestamp(now()) > 20
it is working.

Any Idea?

Code:
trigger: platform: template value_template: '{% if as_timestamp(now())-as_timestamp(states.switch.kok_rorelse.last_changed) > 20 %}true{% else -%}false{%endif %}' action: - service: notify.telegram data: title: '*xx*' message: 'xx'

Regards

not sure about your template trigger but can you use a state trigger for: 20 seconds?

automation:
  trigger:
    platform: state
    entity_id: switch.kok_rorelse
    for:
      seconds: 20

Also I think the template trigger would be looking for a capital True

Also another tip you could use the same statement for your template without needing the if else.

{{ as_timestamp(now())-as_timestamp(states.switch.kok_rorelse.last_changed) > 20 }}

Thank you! I was needed to add “from” and t"o" also, not only for seconds. but now it works :slight_smile:
In the documentation “from” and “to” is optional, but got error
2017-08-06 09:41:49 ERROR (MainThread) [homeassistant.config] Invalid config for [automation]: dependency violation - key "for" requires key "to" to exist @ data['trigger'][0]. Got None. (See /home/homeassistant/.homeassistant/configuration.yaml, line 94). Please check the docs at https://home-assistant.io/components/automation/

platform: state
entity_id: switch.kok_rorelse
from: 'on'
to: 'off'
for:
  seconds: 20

But the {{ as_timestamp(now())-as_timestamp(states.switch.kok_rorelse.last_changed) > 20 }} dosent work, strange?

Now that you mention it. I do not think now() works in triggers. I think I ended up using sensor.time as the trigger.