Strptime question

hi All,
I am almost pulling my hair trying to get some basic time calculations in HA. I made quite some progress by reading through the EPIC post on time, but I am simply blocked right now.

I am trying to get a template to make a string in format HH:MM:SS

Here is my code:

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

If the 'input_datetime.coffee_start_workday is let’s say 06:37:00 (no date on it), then the result is 6:07:00

While calculation is correct, the format is not what I need: I was expecting 06:07:00

What am I doing wrong/ how shall I do it?

Thanks in advance

I suggest doing it this way (shorter and easier):

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

It subtracts 30 minutes (represented in seconds) from the input_datetime’s timestamp attribute then uses the timestamp_custom filter to display it in the format you want (preserving leading zeros).

Thanks! that for sure did it

1 Like