How to trigger a start time minus 30 minutes?

I have a helper where I want to set a time for when I leave my house. That time will then be used in an automation but start my heater 30 minutes before (so the car is warm when I leave)

With this template {{strptime(states(‘input_datetime.leaving_time’), “%H:%M:%S”) - timedelta( minutes = 30 ) }}

I got 1900-01-01 06:30:00 (even that I am using only time in the helper)

So how can I in an automation trig the “leaving_time” - 30 minutes?

Example: I set the helper to 07:00 and I want the automation activate a service 06:30

2 Likes
{{ (state_attr('input_datetime.leaving_time', 'timestamp') - 30*60) | timestamp_custom('%H:%M', false) }}
2 Likes

Thanks a lot!

Maybe a stupid question - how can I use this template to start an automation that time (30 minutes before)?

Use a template trigger.

Do you have a time sensor?

If so:

trigger:
  - platform: template
    value_template "{{ states('sensor.time') == (state_attr('input_datetime.leaving_time', 'timestamp') - 30*60)|timestamp_custom('%H:%M', false) }}"

If you don’t have a sensor.time:

trigger:
  - platform: template
    value_template "{{ as_timestamp(now())|timestamp_custom("%H:%M") == (state_attr('input_datetime.leaving_time', 'timestamp') - 30*60)|timestamp_custom('%H:%M', false) }}"
2 Likes

Thanks - it works perfectly!

With great help from you it works perfectly with an input helper (time) and start 30 minutes earlier

To make it even more complex :wink: I also want to use a dropdown helper for select a leaving time from a list.

Can I use similar template? It seems that I can’t get the timestamp as an attribute so I cant do the calculation ("-30*60). Can I convert like 06:00 to a timestamp format?

Thanks in advance!

Do it the other way around. Convert the time now (plus your offset) to the input_select format and compare them as strings:

trigger:
  - platform: template
    value_template "{{ (as_timestamp(now()) + 30*60)|timestamp_custom("%H:%M") == states('input_select.leaving_time') }}"
1 Like

Ahh - so smart! Thanks!