Remove leading zero, possible?


This is a part of my code to display the time elapsed since last last updated:

{{(as_timestamp(now())-as_timestamp(states.input_boolean.torktumlaren_running.last_updated)) | timestamp_custom(‘%H hours and %M minutes’, false) }}

But is it possible to remove leading zeros?
I have tried to add a hyphen, e.g. %-H but that doesn’t work.
I have also tried to implement .lstrip but how I try it won’t work.

Does someone know how to do this?
Or do someone has a better way to achieve this?


try this

replace("(0", "(") | replace("/0", "/")

It fixed the same problem in the DarkSky template

so it would look like:

{{(as_timestamp(now())-as_timestamp(states.input_boolean.torktumlaren_running.last_updated)) | timestamp_custom(’%H hours and %M minutes’, false) |replace("(0", "(") | replace("/0", "/") }}

I think

1 Like


Thank you for your answer.
I tried the command and had to change position of some backets and then it removed the leading zeros.
But it also removes the trailing zeros.

For instance:
01h 10 min would be 1h 1min.

I have tried to change the command but with no luck.
Any idea?

{{(as_timestamp(now())-as_timestamp(states.input_boolean.torktumlaren_running.last_updated)) | timestamp_custom(‘%H timmar och %M minuter’, false) |replace(“0”, “”) |replace(“/0”, “”) }}

1 Like

What does the string output like (example) and what do you want it to look like?


Correct last_update time: 03:50 (h:m)

{{(as_timestamp(now())-as_timestamp(states.input_boolean.torktumlaren_running.last_updated)) | timestamp_custom(‘%H hours %M minutes’, false) |replace(“0”, “”) |replace(“/0”, “”) }}

Give the result: 3 hours 5 minutes

{{(as_timestamp(now())-as_timestamp(states.input_boolean.torktumlaren_running.last_updated)) | timestamp_custom(‘%H hours %M minutes’, false) }}

Give the result: 03 hours 50 minutes

I want it to be: 3 hours 50 minutes

More example: 1 hour 7 minutes (01:07) h:m

This works for me in HA on ubuntu venv:

{{ (as_timestamp(now()) - as_timestamp(states.input_boolean.night_time.last_updated)) | timestamp_custom('%-H hours and %-M minutes', false) }}

Returns: 5 hours and 9 minutes

BUT not on, here it returns nothing.

What you can do on hassio, is to convert every single (hour/minute) to int.

{{ (as_timestamp(now()) - as_timestamp(states.sensor.yr_symbol.last_updated)) | timestamp_custom('%H ', false) | int }} hours and {{ (as_timestamp(now()) - as_timestamp(states.sensor.yr_symbol.last_updated)) | timestamp_custom('%M', false) | int }} minutes

Returns: 1 hours and 10 minutes


You’re correct, I’m also running and with your first command nothing is displayed.
It seems that it doesn’t work with hyphens (%-H).

Tried your last command and it’s working the way I want it.

@VDRainer Why’s that?

I don’t know!
I think it has something to do with the hassio docker image.

I’m not sure why - doesn’t work. Just use this instead:

{% set h = as_timestamp(now()) | timestamp_custom('%H', false) |int %}
{% set m = as_timestamp(now()) | timestamp_custom('%M', false) |int%}
{{ h }} hours {{ m }} minutes

Same concept, just returns the actual numbers in the string instead of a number formatted as a string with leading zeros.

Just as_timestamp(now()) with whatever difference that you want.

1 Like

int % or int%?

eh doesn’t matter. That just signifies that the line is ending. If you want it to look nice use a int %.