Template condition renders fine in template editor but fails in automation

this template renders just fine in the template editor with the result “true”:

{% if as_timestamp(states.sensor.lastactive.last_changed) | timestamp_custom('%H') == '07' %}true{% else %}false{% endif %}

But used in an automation, it fails:

condition:
  - condition: template
    value_template: >-
      {% if as_timestamp(states.sensor.lastactive.last_changed) |
      timestamp_custom('%H') == '07' %}true{% else %}false{% endif %}

The error message when klicking Test is:

Error occurred while testing condition
template value should be a string for dictionary value @ data['value_template']. Got None

I have tried with single quotes, double quotes and no quotes, but to no avail.
How come it passes in the editor as valid, but fails when used in a real situation?

There is a known issue with testing templates in the Ui.

Test the automation by forcing a trigger to confirm the condition works.

Ok, I deleted the trigger, saved the automation, reloaded automations and clicked Run Action and still it doesn’t run. The trace shows nothing.

Can you put the trigger back and trigger it by changing the triggers entity state in developer tools and then view the trace to see if the condition passed?

Do you get errors in the logs when triggering ?

Can you post up the full yaml not just the condition?

Thanks to your advice, I found the culprit;
For some reason the Entity-ID had changed and HA added a trailing underscore_2 to the ID. Thus the automation failed due to an unavailable entity and not because of a faulty template.
So now it runs as it should, although the automation UI still claims that the template is incorrect when (if) I click Test.
I find this behavior very strange, and I hope the team is able to fix this soon.
Anyway - thank you so much for your help.

Glad you got it sorted.

It will do, its a known issue, hopefully fixed soon!

1 Like