Convert integer to hours and minutes

I got a sensor that returns time in minutes. The number I get is a single integer, for example 352.

I need to convert this number to hours and minutes, so in this case it would be 5 hours and 52 minutes. The goal is passing the result to a tts announcement in a human readable format.

Skimming through similar questions I didn’t find something that works.

Would appreciate any help!

Use int devision in a template //, and divide by 60. That will give you hours. Next use mod, %, on the same number to get minutes.

{{value | float // 60}} hours {{value | float % 60}} minutes
1 Like

Worked perfectly. Thanks!

Or well, with a small tweak actually for anyone else reading this. Had to add a rounding to the calculated values to get the hours and minutes as integers. So final code was:

{{ (value | float // 60) | round(0) }} hours and {{ (value | float % 60) | round(0) }} minutes

Another way to do the same thing:

{{ (value|int * 60)|timestamp_custom('%-H hours and %-M minutes', false) }}
6 Likes