As Taras said, the condition itself would be a template. Either seperate templates for a before then after, or a single template for the entire operation.
Based on your description, the following should get you there. You want it to be true for a five hour period, correct?
- conditions:
condition: template
value_template: >
{% set t = states('sensor.time') %}
{% set m = state_attr('input_datetime.night_quiet_mode_on_time','timestamp') | int %}
{{ true if t <= ( m + 10800 ) | timestamp_custom('%H:%M', False) and t >= ( m - 7200 ) | timestamp_custom('%H:%M', False) else false }}
Here’s an example of a Template Condition that checks if the current time (in seconds since midnight) is in between two boundary times (also represented as seconds since midnight). The boundaries are simply the input_datetime’s timestamp value plus or minus the specified number of hours (in seconds).
- condition: template
value_template: >
{% set q = state_attr('input_datetime.night_quiet_mode_on_time', 'timestamp') %}
{% set t = (now() - now().replace(hour=0, minute=0, second=0, microsecond=0)).seconds %}
{{ q - 7200 <= t <= q + 10800 }}
EDIT
I’ll need you to confirm that I’ve correctly interpreted the meaning of your boundary values. Here’s how I understood it:
If quiet_time is at 19:00 then I understand the time range is from 17:00 to 22:00 (2 hours before to 3 hours after quiet_time’s value).
I really thought there’d be a simpler way of doing it, either by using a 2nd input_datetime that was set to the first one but with a timedelta applied - but again, I couldn’t get it to work.
Thanks a lot for replying, thats got things going now
Can you confirm how you want the time range to work? The post you marked with the Solution tag appears to be calculating it differently than you requested (it’s checking if the current time is less than 2 hours after quiet_time, not 3 hours).