Help with templating and automation - always false

Hi,
I want to start a pump based on a time but want to make a test before that. When I test the template in developer options it works, meaning it returns either a true or false but when I use it in my automation it always returns a false thus the autiomation does not trigger. Anybody that can help me?

"{{ (now()|as_timestamp - state_attr('automation.fan_on_upstairs','last_triggered')|as_timestamp) > 40000 }}"

Is as_timestamp a filter?
I was quite sure it’s a function (or whatever it’s called in Jinja/python).

{{ as_timestamp(blabla) }}

I believe both ways are correct. However, changing it to

"{{ as_timestamp(now()) - as_timestamp(state_attr('automation.fan_on_upstairs','last_triggered'))>5000}}"

Does not change the issue, unfortunately :frowning:

Break it down in the template editor to see what is going on. e.g. what do these resolve to:

{{ now().timestamp() }}
{{ state_attr('automation.fan_on_upstairs','last_triggered') }}
{{ state_attr('automation.fan_on_upstairs','last_triggered')|as_timestamp }}
{{ now().timestamp() - state_attr('automation.fan_on_upstairs','last_triggered')|as_timestamp }}

A great input. This is what I get:
1(number): 1653905762.04604
2 (string) : 2022-05-29 18:17:54.801431+00:00
3 (number): 1653848274.801431
4 (number): 57628.371796131134
And if I do the last one (the template from the first post) I get a string “True”

So all looks good to me. However in the automation is reminds false thus the automation never initiate.

It won’t initiate unless it goes from lower than 4000 to higher than 4000

If you restart the automations and you just added it and it’s above the threshold, it won’t trigger until it drops and then crosses the threshold

Well this is my issue, even if I change it to > 1 the automation still wont trigger and if I look at “show trace” it still shows as false

As Petro explained. It won’t trigger if it’s already true

Use this in the developer tools:

{{ now().timestamp() - state_attr('automation.fan_on_upstairs','last_triggered')|as_timestamp }}

Take the value you get and add 120 to it and replace the x with that value:

"{{ (now()|as_timestamp - state_attr('automation.fan_on_upstairs','last_triggered')|as_timestamp) > x }}"

Now you just need to wait about 1,5 minutes and it will trigger.

the difference in your template is like 50k. You have to have that difference drop below 4k, then that difference has to go above 4k to trigger. In order for templates to trigger they need to resolve False then at a later time, resolve True. Otherwise it will not trigger.

Okay then there must be something I have misunderstood. I wanted to make an automation that triggers at a fixed time with the condition below and only trigger if this condition is true.

"{{ (now()|as_timestamp - state_attr('automation.fan_on_upstairs','last_triggered')|as_timestamp) > 40000 }}"

How to do that? :slight_smile:

Make sure the template is false first and then it becomes true by itself. (read my post again)
Then edit your automation to 40 000

That should work as a condition. As a trigger it needs to do what I described above.

Please see attached picture

I really do not understand what is wrong

Was it less than one second ago since the automation triggered last time?
You need to make sure the template returns false when you save the automation.
I doubt the template you posted returns false.

make variables and inspect the variables, see what they resolve to.

alias: ....
trigger:...
variables:
  current: "{{ as_timestamp(now()) }}"
  before: "{{ as_timestamp(state_attr(... 
condition:
- condition: template
  value_template: "{{ current - before > 1 }}"
action:
...

I just tried precisely what You described and unfortunaetly it did not change anything, the template is still evaluated as “false”

I will try later today and get back with the result, thanks for the suggestion!

I see your problem. You aren’t understanding the differences between YAML and the templating engine and you’re combining single line yaml and multi-line yaml syntax.

When you use some_field: >-, you’re telling the yaml that all indented lines below this belong to this some_field.

When you use some_field: "..." you’re telling the yaml that everything between the quotation marks belongs to this field and is a string. Yaml will naturally remove the quotation marks because it’s not part of the value.

When you combine the 2, your result will always be wrapped in quotes. And "True" does not resolve to True and "False" does not resolve to False.

We would have seen this sooner if you posted your entire yaml, i.e. the field and the template.

Either way, I can see that in your condition.

If you’re going to use multiline notation for your templates, remove the exterior quotes.

condition: template
value_template: >-
  {{ (now()|as_timestamp - state_attr('automation.fan_on_upstairs','last_triggered')|as_timestamp) > 40000 }}

OR keep the quotes and remove the multi-line notation

condition: template
value_template: "{{ (now()|as_timestamp - state_attr('automation.fan_on_upstairs','last_triggered')|as_timestamp) > 40000 }}"

See this thread for understanding the differences between yaml and jinja.

1 Like

OMG that fixed it THANKS!!!