I’m trying to configure an automation with a template condition but I’m not getting why it does not works. I tried my template with the dev tool and it seems to work there.
The automation:
alias: Time tests
description: ''
trigger:
- platform: time
at: '22:00'
condition:
- condition: template
value_template: >
{% set fifteenMinutesInSeconds = 15 * 60 %}
{% set date = states('sensor.date') %}
{% set nowTime = states('sensor.time') %}
{% set nowTimestamp = strptime(date + ' ' + nowTime, '%y-%m-%d %H:%M:%S')
| as_timestamp %}
{% set startTime = states('input_datetime.cb_extra_heater_start_time') %}
{% set startTimestamp = strptime(date + ' ' + startTime, '%y-%m-%d
%H:%M:%S') | as_timestamp %}
{% set endTime = states('input_datetime.cb_extra_heater_end_time') %}
{% set endTimestamp = strptime(date + ' ' + endTime, '%y-%m-%d %H:%M:%S')
| as_timestamp %}
{{ (startTimestamp <= nowTimestamp) and (nowTimestamp <= (endTimestamp -
fifteenMinutesInSeconds)) }}
action:
- service: notify.notify
data:
message: test3
mode: single
Any idea what I’m doing wrong ? Is it because it is multiline ?
Also another small question related to the condition, is there an eaesier way to compare time (without checking date)?
Please take care about your formatting (or using a screenshot) like stated here (point 14):
EDIT: somehow some text didn’t get saved.
Have you tried switching the mode for the condition to yaml and see what comes up? If not, the three dots in the upper right corner, switch to yaml and see what comes up. There you can as well set the > that you would need for multilines.
alias: Time tests
description: ''
trigger:
- platform: time
at: '22:00'
condition:
- condition: template
value_template: >
{% set offset = 15 * 60 %}
{% set t = as_timestamp(now().date()) %}
{% set start = t + state_attr('input_datetime.cb_extra_heater_start_time', 'timestamp') %}
{% set end = t + state_attr('input_datetime.cb_extra_heater_end_time', 'timestamp') - offset %}
{{ start <= now().timestamp() <= end }}
action:
- service: notify.notify
data:
message: test
mode: single
Question: The automation triggers at a specific time (22:00) so why is it using a condition to check if the time is within a time-range? If it’s built like that simply for testing purposes, what is the ultimate intended purpose of this automation?
Thank you for your answer. It’s indeed easier than the logic I had
But it still always passes.
I tried with what you suggested but it still always passes (even with {{false}} as the last line)
I’m not getting what I’m doing wrong.
The automation will turn off an extra heater in a bedroom when an inputs is changing or when it’s time to start or when the temperature is going down. The trigger I putted was just for testing purpose.
I tried it again in the template editor and I’m getting False.
The logic is OK, get the expected result in the template editor but is still passing the condition.
What could impact the evaluation of the condition other than the condition itself ?
Yes in the template editor is works well.
But as soon as I put it as a condition in an automation, it is always passing.
My goal is to pass the condition only when the time is within the range (which is not the case right now).
If I click on execute, it executes the action even if it is not in the range.
The automation is exactly what you suggested:
alias: Time tests
description: ''
trigger:
- platform: time
at: '22:00'
condition:
- condition: template
value_template: >
{% set offset = 15 * 60 %}
{% set t = as_timestamp(now().date()) %}
{% set start = t + state_attr('input_datetime.cb_extra_heater_start_time', 'timestamp') %}
{% set end = t + state_attr('input_datetime.cb_extra_heater_end_time', 'timestamp') - offset %}
{{ start <= now().timestamp() <= end }}
action:
- service: notify.notify
data:
message: test
mode: single
Oh I feel so stupid. So from the beginning the code was ok but (not as good as what 123 taras suggested, but working) and I missed such a big thing. I’m really sorry…