Calculate time remaining (3D Printer)

Hello!

I have a 3D printer that unfortunately isn’t OctoPrint compatible. I currently have some sensors available exposed via an api to use, but I am missing a ‘estimated time remaining’ sensor. So I would like to make one.

I have ‘sensor.printer_percentage_completed’ and ‘sensor.printer_minutes_elapsed_from_start_time’. Would it be possible to calculate estimated minutes remaining using these 2 values? If so could anybody help with a template.

I understand that this would be 100% accurate but a rough estimated mins remaining would be great.

We have:

percentage = elapsed_time / total_time

or, assuming percentage is not 0:

total_time = elapsed_time / (percentage/100)

and finally:

remaining_time = (total_time - elapsed_time) = elapsed_time * ( 100 / percentage - 1)

You’ll have to test this with the developer template (Link to Developer tools: templates – My Home Assistant), but as a starting point:

{% set printer_percentage_completed = 25 %}
{% set printer_minutes_elapsed_from_start_time = 1 %}

{% if printer_percentage_completed >0 %}
  Total time: {{ printer_minutes_elapsed_from_start_time / (printer_percentage_completed/100) | float }}
  Remaining time: {{ printer_minutes_elapsed_from_start_time * (100/printer_percentage_completed - 1) | float }}
{% else  %}
  Total time: {{ -1 }}
{% endif %}

I’ve set it to -1 if percentage is zero, just to avoid division by zero errors. You should prefix printer_precentage_completed and printer_minutes_elapsed_from_start_time by sensor, it’s just so I could test it.

What you probably want to do is create an Input Datetime - Home Assistant (home-assistant.io) and use an automation to set the value of this. For this you will also have to convert the minutes to format H:M:S.

Edit: Sorry, you wanted remaining time. I’ve modified the reply to account for this.

Thank you for your reply and pointing me in the right direction. That was a perfect solution and exactly what I needed. Really appreciate your help! :grinning:

1 Like