In this script I want to find a given entry in the schedule that applies when the script is run. (The trigger is not based on time.)
sequence:
- variables:
index: 0
schedule:
- name: Morning
after: "6:30"
before: "10:00"
- name: Midday
after: "10:00"
before: "15:00"
- name: Evening
after: "15:00"
before: "21:30"
- repeat:
for_each: "{{ schedule }}"
sequence:
- condition: time
after: '15:45'
before: '2:30'
- action: notify.persistent_notification
metadata: {}
data:
message: "{{ repeat.item.name }}"
What I’d like to do, of course is:
- condition: time
after: '{{ repeat.item.after }}'
before: '{{ repeat.item.before }}'
- action: notify.persistent_notification
...
But, it does not seem like after:
and before:
accept a template.
I found a few posts about this, such as this one. (Although I don’t understand the need to convert to a timestamp vs. comparing datetime objects.)
Nice thing about the the time
condition is you can be a bit sloppy on the time format (no leading zero, no seconds) AND it figures out if it crosses a midnight boundary.
Best I can think of is somthing like a template condition like:
{% set cur_day = now().strftime("%Y-%m-%d ") -%}
{% set cur_day = '2023-02-28 ' -%}
{% set fmat = "%Y-%m-%d %H:%M" -%}
{% set start_dt = strptime( cur_day ~ '15:00', fmat ) -%}
{% set end_dt = strptime( cur_day ~ '2:30', fmat ) -%}
{{ start_dt }} < {{ end_dt }} == {{ start_dt < end_dt }}
{% if start_dt > end_dt -%}
{% set end_dt = end_dt + timedelta(days=1) -%}
{% endif -%}
{{ start_dt }} < {{ end_dt }} == {{ start_dt < end_dt }}
Resulting in:
2023-02-28 15:00:00 < 2023-02-28 02:30:00 == False
2023-02-28 15:00:00 < 2023-03-01 02:30:00 == True
(I’m not worried about DST changes.)
The time
condition configuration accepts a string time, so curious if there’s a way to use a variable for the string. Or do I need to go the template route?
Thanks,
Update: I’ll try the template solution by @Didgeridrew