Great. I've got a timedelta. Now how do I format it?

I have a timedelta in the since variable.

If I put {{ since }} in the template editor, it displays 4:38:05.361237.

But I can’t find how to access the timedelta as a string, or to format it using a custom string.

To pass it to a card component, I need it to be in the format %H:%M.

since.split(':') and since.strftime('%H:%M') both fail with 'datetime.timedelta object' has no attribute '…'.

I can use since.seconds but not since.minutes or since.hours.

I can’t inspect the object to find what it does support, and Create a time duration: timedelta - Home Assistant is so busy telling people how to create a timedelta that it forgets to tell us how to use it — there’s no link to a page documenting the timedelta object class, if indeed there even is such a page!

WTH isn’t there documentation on the object classes? A timedelta’s no use if you can’t do things with it. If this is just a Python class, it’s not enough to use the standard Pythonista “everyone else is a Pythonista” assumption. It looks like relativedelta — dateutil 3.9.0 documentation would be more helpful, but how can I get that into HA?

Please could someone tell me how to get this thing formatted correctly? It shouldn’t be this hard…

{{ since | regex_replace(':[0-9]+\.[0-9]+$', '') }} seems to do the trick as long as I don’t want leading zeroes on the hours — and fortunately, I don’t.

But is there a cleaner way?

Official documentation

Geek for Geeks

There is HA documentation

same as converting all other objects to strings

{{ since | string }}

This is not possible, that’s why you can’t find it. It’s not a normal thing for coding languages to have.

If you want to format it, you use it like an object.

{{ since.days }} # for days
{{ since.seconds }} # for seconds under 24 hours of seconds

if you just want to do the math yourself, use total_seconds()

{{ since.total_seconds() / 3600 }}  #Duration in hours

Be careful when accessing the split-out attributes of a negative timedelta as they are normalized and may look a bit odd if simply used naively.

See the section starting ‘Note that normalization of negative values may be surprising at first. For example:’ at datetime — Basic date and time types — Python 3.14.6 documentation for the gotcha.

First time I used negative timedeltas I thought I’d found a bug until I grokked that.

@petro’s tip about using the total_seconds() method helps here as it doesn’t have the ‘problem’.

Thanks — and this is the problem with the HA documentation: it doesn’t link to this on it’s own documentation page for the timedelta function. A Python dev would probably have gone straight there — but not everybody using HA is a Python dev!

Ah — I was using as_string, as I’d found some other as_<type> methods that it supported.

Yes, and that’s the source of my confusion — I’d found those throught experimentation, but seconds is the number of seconds under 24 hours! days, hours, minutes and seconds would have made a lot more sense.

Presumably, I’d actually need this to get

{{ (since.total_seconds() / 3600) | int }}

but it seems bizarre to have to do this every time rather than have a built-in way. I guess this is Python-brain vs Ruby/Rails-brain :slightly_smiling_face:

As regexps are working for me, and this seems simpler than doing all the calculations, I’ve ended up with this:

{% if since | string is match('-') %}-{% endif -%}
{{ since | abs | regex_replace(':[0-9]+\.[0-9]+$', '') }}

Thanks for your help — and to @Spamfast for the warning about negative values!

Uh ruby doesn’t have a built in timedelta function either, you have to math it out. Unless you’re using an additional library.

Legitimately, I’ve never seen a language natively support formatting time differences. I’ve mostly stuck with python, c++, C#, languages. They all support formatting times. I usually have to make a function for this, via relative_time or something similar.

At my last company, we made a customer facing software that needed duration formatting. I ended up creating one for them that mimicked time formatting, I suppose I could add something similar here.

Yes, I was thinking more of Rails — I haven’t done any plain Ruby for ages.

Specifically, ActiveSupport::Duration#parts.