klogg
(Klogg)
April 6, 2020, 9:44am
1
It is well documented elsewhere on this forum that I am not the best at working with times
If I want to implement a dynamic timeout in a wait_template
, should this work?
- wait_template: >
{% set tnow = as_timestamp(now()) | int %}
{% set timeout = states('input_number.wait_timeout') | int %}
{% if timeout != 0 %}
{{
is_state('sensor.some_sensor', 'some_state') or
as_timestamp(now()) | int > tnow + timeout
}}
{% else %}
{{ is_state('sensor.some_sensor', 'some_state') }}
{% endif %}
klogg
(Klogg)
April 6, 2020, 9:49am
3
Ha ha!
Yes and you are one of those who have in the past been on the receiving end of my crapness with time.
So, why not?
Mutt
(Muttley)
April 6, 2020, 9:49am
4
Your print statement is boolean Read later
klogg
(Klogg)
April 6, 2020, 9:50am
5
Yikes!!!
Yes indeed it is!
But is the principle sound?
Mutt
(Muttley)
April 6, 2020, 9:53am
6
I have absolutely no idea as you have not defined what you expect to do.
I would ‘assume’ that you would want to set a ‘time’ value based on the condition of various sensors but that’s not what you have written.
klogg
(Klogg)
April 6, 2020, 9:54am
7
Hang on, wait_template
wants a true or false
The actual timeout needs a time.
I am not using timeout
just trying to ‘fake it’
I think what I have makes sense.
The wait_template
evaluates to true or false (if my time calculations make sense)
Mutt
(Muttley)
April 6, 2020, 9:58am
8
I’m on a tablet, where is the wait documentation?
tom_l
April 6, 2020, 10:05am
9
It looks sound to me except the logic of including this in both cases:
{{ is_state('sensor.some_sensor', 'some_state') }}
If the time-out has expired or the sensor is in state ‘x’ else if the sensor is in state ‘x’.
Pretty sure you can drop the or sensor bit from the first template and it will still do the same thing.
Mutt
(Muttley)
April 6, 2020, 10:53am
10
Yeah, found the documentation (is someone deliberately hiding stuff? I had to use Google )
You check if timeout is not 0 then go with x (Tom’s term) but if timeout = 0 then now +0 is now anyway, so dispense with the if, get rid of the else and go with the first (like Tom said )
Edited
klogg
(Klogg)
April 6, 2020, 11:52am
11
@tom_l & @Mutt
Thanks.
The reason for the ‘if’ is because I want to be able to have no timeout i.e. wait indefinitely for the sensor state ‘x’.
So,
if timeout is not zero wait for the sensor state ‘x’ or a maximum time of ‘timeout’
if timeout is zero wait for the sensor state ‘x’ indefinitely.
1 Like
Mutt
(Muttley)
April 6, 2020, 3:59pm
12
Okay, let us know how this goes.
@tom_l the reason I’m cautious here is that I have some doubts.
The template is written so that if your input number is say 10 seconds, then with the sensor at ‘some state’ it won’t fire immediately (it should wait 10 secs) but does that mean that the wait: tests the template every second till it fires ?
(okay, there are ‘undocumented’ instances where now() will update in the template)
But I’ve not tested this situation and as far as the docs go there is nothing in the template to trigger an update.
As I said, let us know
tom_l
April 6, 2020, 4:16pm
13
klogg:
The reason for the ‘if’ is because I want to be able to have no timeout i.e. wait indefinitely for the sensor state ‘x’.
So,
if timeout is not zero wait for the sensor state ‘x’ or a maximum time of ‘timeout’
if timeout is zero wait for the sensor state ‘x’ indefinitely.
This may not work if tnow
is continually evaluated. I’m pretty sure it will be.
- wait_template: >
{% set tnow = as_timestamp(now()) | int %}
{% set timeout = states('input_number.wait_timeout') | int %}
{% if timeout != 0 %}
{{
is_state('sensor.some_sensor', 'some_state') or
as_timestamp(now()) | int > tnow + timeout
}}
{% else %}
{{ is_state('sensor.some_sensor', 'some_state') }}
{% endif %}
You could use the automation last_triggered
attribute instead of now()
for tnow
.
1 Like
klogg
(Klogg)
April 6, 2020, 4:41pm
14
Very good point.
So obvious it is almost genius.
Thank you!
Is there a shorthand for ‘the current automation/script ’ like there is for triggers or is the only way to do this?
{% set tnow = as_timestamp(state_attr('automation.the_name_of_this_automation', 'last_triggered')) | int %}
tom_l
April 6, 2020, 5:12pm
15
That’s it. Though you don’t need |int
for timestamps.