Add and subtract hours to input_datetime


Searched and searched for this, but can’t figure it…

I have a script which needs a time before and after condition.

The time which the condition applies needs to be 2 hours before an input_datetime time and after 3 hours after the same input_datetime time.

So I want the after: to be 2 hours before whatever input_datetime.night_quiet_mode_on_time is set to (and vice versa for before:)

I know this doesn’t work because you can’t put templates after after: (I don’t think), but it does kinda show what I want to do;

          - conditions:
              condition: time
              after: {{(((state_attr('input_datetime.night_quiet_mode_on_time' , 'timestamp')) - 7200) | timestamp_custom('%H:%M', true))}}
              before: "11:00:00"

I think I’m getting all muddled up with int’s and str’s and time formats and how to add and subtract from a time (which doesn’t have a date).



Time Condition doesn’t support templates. You will need to use a Template Condition.

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 }}
1 Like

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


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

Thats really helpful, thank you :slight_smile:

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


That would require a template, which isn’t supported by a Time Condition.

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

You’re right. Edited the code to reflect.