Automation Time trigger with templated "at"?

Hi! I am trying to figure out how to have a conditional “at” statement for a time trigger. Like this not so very correct pseudo code
trigger:
platform: time
at: “{% if is_state(‘calendar.barnvecka’, ‘off’) %}05:15:00{% else %}05:50:00{% endif %}”

Meaning that depending on a calendare state I have different times to trigger the automation on.
If calendar.barvecka is ‘off’ the automation should trigger at 05.15 AM and if it is not ‘off’ (meaning ‘on’ obviously) it should trigger at 05.50 AM.
The template itself seems to be correct and functional but I get some serious errors when doing it like this. So how should I do it with a trigger of template platform? I am going to have some other conditions as well in the automation but they are or the “regular” kind.
BR

Jari Ivanoff

The at parameter does not accept a template. You need to enable sensor.time and then use a template trigger, something like:

trigger:
  platform: template
  value_template: >
    {{ is_state('calendar.barnvecka', 'off') and is_state('sensor.time', '05:15') or
       not is_state('calendar.barnvecka', 'off') and is_state('sensor.time', '05:50') }}

Or even:

trigger:
  platform: template
  value_template: >
    {{ is_state('sensor.time',
       '05:15' if is_state('calendar.barnvecka', 'off') else '05:50') }}
1 Like

Wonderful! Thanks! I will test this immediately! You got the idea of what I wanted to accomplish! As I said, it was kind of pseudo code.I am trying to learn this template thing of Hass since I know it will improve things!

I guess this is still true? I’ve been trying for an hour now with:

    at: >
      {%- if states("binary_sensor.workday") == "on" -%}
        {%- if states.input_datetime.stop_night.attributes.minute < 10 -%}
          0{{states.input_datetime.stop_night.attributes.hour}}:0{{states.input_datetime.stop_night.attributes.minute}}
        {%- else -%}
          0{{states.input_datetime.stop_night.attributes.hour}}:{{states.input_datetime.stop_night.attributes.minute}}
        {%- endif -%}
      {%- else -%}
        {%- if states.input_datetime.stop_night.attributes.minute < 10 -%}
          0{{states.input_datetime.stop_night.attributes.hour+1}}:0{{states.input_datetime.stop_night.attributes.minute}}
        {%- else -%}
          0{{states.input_datetime.stop_night.attributes.hour+1}}:{{states.input_datetime.stop_night.attributes.minute}}
        {%- endif -%}
      {%- endif -%}

But I guess it was useless from the start?

Yes. The documentation for Time String indicates it’s a string and there’s no example showing the possibility of templating it.

What has changed in the 3 years since this topic was created is that a Time Trigger now supports using an input_datetime or a timestamp sensor (refer to documentation for details). However, those improvements are not applicable for your case because it isn’t simply triggering based on an input_datetime but also depends on the state of the Workday binary_sensor. You’ll probably need to use a Template Trigger.

1 Like