Creating sensor template to convert a time in a string from HHMMSS into HH:MM:SS

I was trying to figure out how to convert a sensor input looking something like this: 123010 to: 12:30:10 but couldn´t find a solution.

What is the best way to do this?

Input “:” into the string or converting the sensor with as_timestamp?

Thank you in advance.

Maybe not the most elegant solution but you could adapt this:

{% set my_time = "123456" %}
{{ my_time[0:2]~":"~my_time[2:4]~":"~my_time[4:6] }}
1 Like

Thank you, your solution worked!

I added my sensors input and it shows it like i want it to.


% set my_time = states('sensor.carport_wallbox_charge_end') %}
{{ my_time[0:2]~":"~my_time[2:4]~":"~my_time[4:6] }}

1 Like

This might be a bit more elegant

{% set mytime = states('sensor.carport_wallbox_charge_end') %}
{{ strptime(mytime, '%H%M%S').strftime('%H:%M:%S') }}

Yet another way to do the same thing:

{% set my_time = "123010" %}
{{ my_time|regex_findall('..')|join(':') }}

In your case it would look like this:

{{ states('sensor.your_sensor')|regex_findall('..')|join(':') }}

Thank you for your reply.

I added your code to the sensor and it works fine.