Template: round(2) only works occasionally (or: seconds to minutes and/or hours?)

Hi everybody,

I am using the OctoPrint integration to display several 3d printer statuses in Home Assistant. Unfortunately, octoprint will give the ETA in seconds; I have not found a way to configure this, so I was going to create a template sensor for this:

sensor:
  - platform: template
    sensors:
      mk3s_minutes:
        friendly_name: "Prusa MK3S Time Remaining Minutes"
        unit_of_measurement: "Minuten"
        value_template: "{{ states('sensor.prusa_mk3s_time_remaining') | float / 60 | round(2) }}"

Sometimes, this will display something like 2.25 Minuten, which is the output I expected. Then again, some other times I’ll get 2.12222222222222223 Minuten, which means, the round(2) has not been executed. Right? Why is that?

Also, are any of you using a template like this, and perhaps even have one that will not only convert to minutes, but hours (if ETA is more than one hour)?

Thank you for your ideas :slight_smile:

Apparently this is due to an epsilon error in python, it’s just that round() makes it glaringly obvious.
There is a text equivalent that always works however, for example I use this to get distances from home to 3 dp

"{{ '%.3f' | format(distance(states.device_tracker.life360_mutt)) }}"

If you have ‘seconds’ you can treat it with timestamp and convert it to HH:MM with : -

{{ (((5*60) + 17)*60) | timestamp_custom('%R', false) }}

gives 05:17 , have a play with it

2 Likes

That is awesome! Exactly what I needed, thank you so much.

It doesn’t seem to work for me. My code:
{{ states('sensor.ender3v2_time_elapsed') | timestamp_custom('%R' , false) }}
It still gives seconds. What could be wrong?
I’m using it in a markdown card.