There's no way to use variables in time's "after" and "before"?

I have tons of automations that should fire only during certain time interval - from 18:00 till 6:00.
In all of them I have to use time checks with After and Before. It would be great if I could tune this time interval everywhere simultaneously without editing each automation by hand.

I tried using templating for Time helper there, but it doesn’t seem to work

condition: time
after: "{{ ... }}"
before: "{{ ... }}"

So is there some way to just insert some time variables there or do I have to use this monstrous construction generated by chatGpt just to check a time interval?

    condition:
      condition: template
      value_template: >
        {% set start_time = strptime(states('input_datetime.start_time'), '%H:%M') %}
        {% set end_time = strptime(states('input_datetime.end_time'), '%H:%M') %}
        {% set current_time = now().strftime('%H:%M') %}
        {% if end_time >= start_time %}
          {{ start_time <= strptime(current_time, '%H:%M') <= end_time }}
        {% else %}
          {{ strptime('00:00', '%H:%M') <= strptime(current_time, '%H:%M') <= end_time or start_time <= strptime(current_time, '%H:%M') <= strptime('23:59', '%H:%M') }}
        {% endif %}

Correct this is not supported. See here for supported options: https://www.home-assistant.io/docs/scripts/conditions/#time-condition

It shows you can however use input_datetimes, and you don’t even need templates.

condition: time
after: input_datetime.my_after_time
before: input_datetime.my_before_time

You can then use automations or dashboard cards to change the input_datetimes however you wish.

Also please don’t use ChatGPT. It was trained on out of date data.

2 Likes

A schedule helper may also be effective.

1 Like

It works! Thank you very much. I though I tried that first. I think I typed in input_daytime instead of input_datetime >…< It doesn’t let you copy that part of variables from UI for some reason

Thanks Tom, really saved me :slight_smile:

1 Like