Time difference - based on an datetime helper and template

Hi,
Is it possible to determine the time difference between the two in a template?

the first:
{{ as_datetime(states('input_datetime.test')).strftime('%d %B %y - %H:%M') }}

the second:
{{ now().strftime('%d %B %y - %H:%M') }}

so the first is based on a start time of an automation, when the automation stops the second comes into play. What I’m after is the time difference between them, so for instance an output like: 36 minutes
or 1h and 12 minutes.

Or is it easier to create a second helper that stores the date/time?

All you ever wanted to know about time manipulation in templates (and more):

1 Like
{{ time_since(as_datetime(states('input_datetime.test'))) }}

input_datetimes do not store a timezone. So if your datetime is local, you need convert it to local time.

{{ as_local(as_datetime(states('input_datetime.test'))).strftime('%d %B %y - %H:%M') }}

1 Like

hm I’ve tested your example @Troon but I get an output like:
3 hours
the test input datetime sensor is set on: 2024-06-10 13:00:00
so I would expect an output like 2.5 hours?
Is it rounding the output?

@koying I saw the page and I’m still testing.
When I create another helper (datetime) for the end time, I get the output I expected but don’t ask me why:
{{states('input_datetime.test_end')|as_datetime - states('input_datetime.test')|as_datetime}}

output:
2:30:00

So I need to minus the end of the automation with the start to get the time the total automation ran?

You seem to be allergic to reading the documentation that I linked. Here’s the link again, and a screenshot of the relevant information that clearly explains to you how to do what you want:

image

It’s a bit annoying that I’ve had to explain all this when you had exactly the same answer from me recently:

yes you’re totally right, it’s a maze at times with all kinds of templates that do all kinds of things (on my side) to find the right one for this situation. but thanks for the reminder anyway.