Using now() in a template trigger - working but shouldn't be?

Hello,

I recently set up a simple automation to turn my car block heater on at a specific time set in the GUI using a datetime helper.

> alias: Block Heater - Turn On at Time
> description: ''
> trigger:
>   - platform: template
>     value_template: >-
>       {{ now().timestamp() | timestamp_custom('%H:%M') ==
>       (state_attr('input_datetime.departure_time', 'timestamp')) |
>       timestamp_custom('%H:%M', false) }}

I have tested this over several days, and it seems to work fine. However, in some further research, I learned that template triggers that contain an entity should only be evaluated when that entity changes. Since mine does contain an entity, it should not work? But it does!

Two questions:

  • Why does this work when it seems that it shouldn’t? Have the rules for evaluating template triggers changed since the forum posts I read were created?

  • Should I change the way this is written?

I think you are confusing things.
What you have is an automation, that should work.
I think what you have read is about a template sensor.

But I could be wrong. I’m not good at these things.

No, I’m talking about template triggers in automations.

This is the thread I’m basing my confusion on: Template won't trigger automation - #14 by AhmadK

as well as: Templates as automation triggers with time

Still not clear what is wrong. But either way.
There is a much better way to trigger than using a template, perhaps that didn’t exist at the time of that thread.

trigger:
  - platform: time
    at: input_datetime.departure_time
2 Likes

I had thought of that too, but my understanding was you could not use template variables in the time trigger.

He used a template as the value. The value should be the entity not the template of the state.

And that thread is even older than the previous you posted

1 Like

Sorry, I just tested using the time platform as you suggested and it works.

I am just very unfamiliar with using raw entity IDs like that without any form of templating. Very cool.

Thank you.

Above listening on state changes of referenced entities, if you use now() in a trigger, the condition will automatically be evaluated every minute.
It’s documented, just lazy to look for it now :wink: