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.
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
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.