How to start an automation 30 minutes before input time helper value

I want to use the input time helper (input_datetime.tidtest) and then use this as a trigger in an automation BUT start 30 minutes earlier. I can calculate as below but how can I use that as trigger in automation?

{{ strptime(states(‘input_datetime.tidtest’), “%H:%M:%S”) - strptime(‘00:30’, “%H:%M”) }}

Thanks in advance!

easier to use now():

{{ (state_attr('input_datetime.tidtest', 'timestamp') - as_timestamp(now()) ) < 1800 }}

this can be used in a template trigger:

    - platform: template
      vaslue_template: "{{ (state_attr('input_datetime.tidtest', 'timestamp') - as_timestamp(now()) ) < 1800 }}"

the above assumes that your input_datetime has both a data and time.

Thanks, but I get true all the time. I have only time as input if that can be problem.

So the idea is to use the helper (input_datetime.tidtest and as time only) to set a time (example 23.00)

Then I want the automation to start 30 minutes earlier (22.30)

{% set date = states('input_datetime.tidtest')%}
{{as_timestamp('date') - 1800}}

1800 seconds equal 30 minutes

The problem is that the input_datetime they are using won’t work with the as_timestamp function. It will only work with a valid datetime string.

to the OP -

try this then:

{{ strptime(states('input_datetime.tidtest'), '%H:%M:%S') - strptime('30', '%M') }}

Seems to get correct value in developer tools but it will not trigger in automation…

Well, yeah, you’re right. I totally lost track of what you were actually asking.

Sorry about that.

you will need to compare the time value to the time now and if they are equal then make the trigger return “true”.


Ok, I’ve tried to manipulate this every way I know to do and I can’t seem to get any comparison to return a true for the trigger.

Unless someone else has any ideas I think the best (and only way I know) to do this is to change your input_datetime to contain the date and then just use the trigger template I gave in the beginning.

I think it has to do with the native data type changes in the latest versions but I can’t even e sure of that.

It can be done with an input_datetime with time only but you have to add today’s date to it first. See my binary sensor below that compares a couple of input_datetimes to now() :

      friendly_name: "Lounge AM Automation Time Active"
      value_template: >
        {% set d = now().strftime("%Y-%m-%d ") %}
        {% set t = now().timestamp() %}
        {% set am_start = strptime(d + states('input_datetime.lounge_ac_am_on_time'), '%Y-%m-%d %H:%M:%S').timestamp() %}
        {% set am_end = strptime(d + states('input_datetime.lounge_ac_am_off_time'), '%Y-%m-%d %H:%M:%S').timestamp() %}
        {{ am_start <= t <= am_end }}
1 Like

Thanks a lot. I used your example with minor modifications and now it works :slight_smile:

Here’s a simpler method I just read about (the second one):

Might change my templates to use this. I’m not sure the direct comparison of time strings like that was available when I wrote my original template.

1 Like