Time conversion

Hi,

I want to display the estimated time my 3D printer running Mainsail (Klipper) will finish. Natively using the custom mainsail by moonraker addon (HACS store) I get the ETA sensor which calculates print remaining time in minutes eg. 40:01 (MM:SS).

I’ve found this code which almost does what I want, but I believe the code expects the ETA to report as HH:MM:SS, not MM:SS. So when the calculation happens for the estimated finish time, it thinks there are 40 seconds remaining instead of 40 minutes.

Here is the code:

    mainsail_time_remaining_format:
      friendly_name: 'M3D Time Remaining'
      value_template: "{{ states('sensor.m3d_eta') | int | timestamp_custom('%H:%M:%S', 0) }}"
      icon_template: mdi:clock-end
    mainsail_finish_time_format:
      friendly_name: 'M3D Finish Time'
      value_template: >
        {% if not is_state('sensor.m3d_state', 'Operational') %}
          {{ ((now().strftime('%s') | int) + (states.sensor.m3d_eta.state) | int) | timestamp_custom('%H:%M', false)}}
        {% else %}
          N/A
        {% endif %}

Can anyone point me in the right direction as to what needs to be changed in this? I’m sure it’s very simple just can’t figure it out! Not the YAML expert I want to be just yet…

Any help is greatly appreciated!

Just to confirm, is the value of sensor.m3d_eta displayed in MM:SS format? For example: 54:32 meaning 54 minutes and 32 seconds.

Or is it a numeric value?

Hi Taras,

Thanks for your quick reply.

m3d_eta reports as a numerical value of the amount of minutes remaining (eg. 14.4877393177282 is just under 14 and a half minutes).

EDIT: In Home Assistant I have the display precision for m3d_eta set to only 1 decimal place.

The timestamp_custom filter expects to receive a value in seconds. Therefore convert the value of sensor.m3d_eta to float and multiply by 60 to get seconds.

    mainsail_time_remaining_format:
      friendly_name: 'M3D Time Remaining'
      value_template: "{{ (states('sensor.m3d_eta') | float(0) * 60) | timestamp_custom('%H:%M:%S', false) }}"
      icon_template: mdi:clock-end
    mainsail_finish_time_format:
      friendly_name: 'M3D Finish Time'
      value_template: >
        {% if not is_state('sensor.m3d_state', 'Operational') %}
          {{ (now().timestamp() +  (states('sensor.m3d_eta') | float(0) * 60)) | timestamp_custom('%H:%M', false) }}
        {% else %}
          N/A
        {% endif %}

NOTE

For the following value:

14.4877393177282

the decimal value portion represents seconds.

If you want to display the time as HH:MM:SS then we need to retain that portion of the value. It will be lost if we use the int filter because it will convert to an integer (i.e. 14). That’s why the template uses float instead of int.

1 Like