Manipulating time string in template ideas?

Hi all,
I’m nearly done building an alarm clock but as the final touch I’d like to use my input select with different alarm times to also set up the transition of my hue lights. However, I’d like my hue lights to begin to transition on 30 minutes before the alarm triggers… any idea how I can easily convert a string like “05:15” (the option selected on my input select) and also subtract 30 minutes from it so I can use it as a condition on an automation?

I’m a bit stuck. I’ve tried converting it to a datetime using strptime with success but I’m having difficulty finding a way to easily subtract 30 minutes. I’m assuming I’m missing something simple at this point.

Any help would be greatly appreciated!

I’m doing some time math in a template sensor to alert me 15 minutes before my son’s Tech Shop afterschool club ends. See if this helps. 900 is 15min x 60 seconds.

{% if states.calendar.nathan_tech_shop %}
          {% if is_state("calendar.nathan_tech_shop", "on") and as_timestamp(states.calendar.nathan_tech_shop.attributes.end_time) - as_timestamp(now()) < 900  %}on{% else %}off{% endif %}
{% else %}off{% endif %}
1 Like

What type of variable is being read from end_time? Is it a string? Could you give me an example? I tried it that way before posting but I’m getting “as_timestamp raise ValueError(“not a valid date/time.”)” Having a hard time getting the string to convert to something as_timestamp can use.

end_time is apparently already a date-time. It looks like this: 2017-01-24 15:30:00

You might take a look at the “creating an alarm clock” thread - they do some stuff where they assemble date/times from inputs.

I’ll take a look again. I’ve tried most of the stuff but I may have missed something. Closest I’ve been able to get was to use strptime. I’ll post a solution if I can find one.

As another aside… is there a simple way to test template logic without restarting the service each time? haha

Got it. I was very close before just had to clean it up.

‘{{ strptime(“07:32”, “%H:%M”) - strptime(“30”, “%M”) }}’

The result returns 07:02.00 as it should. I can easily sub in my input select string and then I’ll know the time at which my lights should begin to turn on.

2 Likes

If you don’t mind, could you please add in your example on how you got this to work? I am trying to do exactly the same with a google travel time, but it is failing.

This is what I am trying:
'{{ strptime("17:00", "%H:%M") - strptime("(states.sensor.dad_work_.state)", "%M") }}'

Well I’ll be…

This works!
'{{ strptime("16:50", "%H:%M") - strptime(states.sensor.dad_work_to_.state , "%M") }}'

Now I’ll try to set this as a trigger for notifications.

Haha I was just about to send you a message. Let me know if you need any more help.

Appreciate the offer!

I sent you a message directly and will update this thread if/when I get this working. This is a really cool use case for time templating, and I don’t want to setup calendars just for this.

Thanks again!

This is pretty cool. I’m going to try this to send alerts 15 minutes before a start time instead of creating separate calendars with the built in offset function. The only concern is if this will trigger an automation continuously for as long as the start time is in less than 15 minutes… I guess I will need to test.