How to set Time Template from second to hours-min-sec

Hi, I have searched a lot for a solution but have not found anything that satisfied me.
I created an oven thermostat with an ESP32 and through MQTT I send a countdown in seconds (time left to cooking) to HA as well as an elapsed time and various temperatures. I would:

  • transform the seconds into → hours:minutes:seconds 00:00:00 *¹
  • use this value to give me the end time (knowing the current time) end of cook 18:35:00
  • create a timer with HA notification. (how to pass the time info to timer?)

Thanks.

*¹ this is probably the only thing I was able to do but i am not sure how to insert it in mqtt sensor:

{% set t = states('sensor.tempo_rimasto')|int %}
{{'{:02d}:{:02d}:{:02d}'.format((t // 3600) % 24, (t % 3600) // 60, (t % 3600) % 60) }}

You can do your seconds to time conversion a lot simpler:

{{ states('sensor.tempo_rimasto')|int|timestamp_custom('%H:%M:%S') }}

However if all you want is the end time you are better off leaving it as seconds:

{{ now() + timedelta(seconds=states('sensor.tempo_rimasto')|int) }}

I’m not sure what you mean by this:

Do you want a notification when the time is done or a notification to know when the time is set to finish?

Neither of these require a timer because of the information you have in the countdown sensor.

3 Likes

Thanks for the reply. I tried using your template but it adds 1 hour to the time. It is not clear to me why.
Could it be related to UTC? I’m in the Rome +1 area.

timer, it will probably be enough that it does an automation that when the time reaches 0 send me a notification. I thought about it just now :smiley:

Yes, indeed that is what it is. You can stop it doing that like this:

{{ states('sensor.tempo_rimasto')|int|timestamp_custom('%H:%M:%S', false) }}

Yep that is what I was thinking.

3 Likes