Multiline template in automation condition

Hello,

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)?

Hello and welcome! :slight_smile:

Please take care about your formatting (or using a screenshot) like stated here (point 14):

EDIT: somehow some text didn’t get saved. :frowning:
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.

Instead of just showing some code and saying why isn’t this working, please describe what you are trying to do. Makes it easier to understand.

Sorry for the screenshot, I replaced it with the yaml of the automation.

What I’m trying to do:
I have 2 inputs (start time and end time) and the goal of the condition is:

  • Actual time is equal of after the start time
    and
  • Actual time is before the end time minus 15 minutes

Thanks for changing the screenshot. :slight_smile:

The + (plus) behind the > is not correct for multiline, it should only be >. Otherwise nothing obvious stands out.

Did the log say something?

EDIT: this one:

1 Like

I removed the + but still the same result.
Nothing in the logs.
The issue is that the condition always passes.

I tried to replace the express (last line, with {{ … }}) by: {{ false }} but still always passing.

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?

2 Likes

Thank you for your answer. It’s indeed easier than the logic I had :slight_smile:

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 don’t understand what you’re doing wrong either.

Paste this into the Template Editor:

start: {{states('input_datetime.cb_extra_heater_start_time') }}
end:  {{ states('input_datetime.cb_extra_heater_end_time') }}
now: {{ now().time() }}

{% 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 }}

It will report True if the current time is between the start and end times, otherwise it will report False.

Here’s what it looks like on my system using my own input_datetime entities:

True

False

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 ?

Use the template code I posted. Your screenshot doesn’t show the start, end, and current time.

1 Like

My bad… Obviously you need values :stuck_out_tongue:

It appears to be working perfectly well. Your current time is 22:45 which is definitely not within the time range of 20:00 to 22:00.

In fact, the actual end time is 15 minutes less than 22:00 (so the time range is from 20:00 to 21:45).

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

If you execute an automation manually, it doesn’t check the condition. :wink: It fires.

You need to let the automation run on its own to see if it works correctly with the conditions.

1 Like

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…

Please read this:

1 Like

To be polite, it was a very long way to get from A to Z. :slight_smile:

That’s true ^^ Thank you for your helps, it’s way better now :slight_smile:

2 Likes