Convert sensor.octoprint_time_remaining to hour/minute/seconds

does your proposal mean that

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

produces the same output as

         {% set etime = states.sensor.ender_3_pro_time_remaining.state | int %}
         {% set seconds = etime % 60 %}
         {% set minutes = ((etime % 3600) / 60) | int %}
         {% set hours = ((etime % 86400) / 3600) | int %}
         {% set days = (etime / 86400) | int %}
         {%- if days > 0 -%}
           {%- if days == 1 -%}
             1 day
           {%- else -%}
             {{ days }} days
           {%- endif -%}
           {{ ', ' }}
         {%- endif -%}

?

image

Similar. You donā€™t get days, only HH:MM:SS

So itā€™s not a direct substitutionā€¦

No. For prints that last days, you should use the other code:

image

Could you share the code for above result that have xx days, xx hours, xx minutes?

itā€™s already here
or this:

{% set etime = states.sensor.ender_3_pro_time_remaining.state | int %}
{% set seconds = etime % 60 %}
{% set minutes = ((etime % 3600) / 60) | int %}
{% set hours = ((etime % 86400) / 3600) | int %}
{% set days = (etime / 86400) | int %}
{%- if days -%}
  {%- if days == 1 -%}
    1 day
  {%- else -%}
    {{ days }} days
  {%- endif -%}
  {{ ', ' }}
{%- endif -%}
{%- if hours -%}
  {%- if hours == 1 -%}
    1 hour
  {%- else -%}
    {{ hours }} hours
  {%- endif -%}
  {{ ', ' }}
{%- endif -%}
{%- if minutes == 1 -%}
  1 minute
{%- else -%}
  {{ minutes }} minutes
{%- endif -%}
1 Like

oofā€¦ apologize for overlooked. and thanks!!

A variation that uses inline-if.

{% set etime = states.sensor.ender_3_pro_time_remaining.state | int %}
{% set seconds = etime % 60 %}
{% set minutes = ((etime % 3600) / 60) | int %}
{% set hours = ((etime % 86400) / 3600) | int %}
{% set days = (etime / 86400) | int %}
{%- if days -%}
  {{days}} day{{'s, ' if days > 1 else ', '}}
{%- endif -%}
{%- if hours -%}
  {{hours}} hour{{'s, ' if hours > 1 else ', '}}
{%- endif -%}
{{minutes}} minute{{'s' if minutes > 1 else ''}}
4 Likes
{% set seconds = states('sensor.octoprint_time_remaining') | int %}
{% set minutes = ((seconds % 3600) / 60) | int %}
{% set hours = ((seconds % 86400) / 3600) | int %}
{% set days = (seconds / 86400) | int %}
{%- if days -%}{{days}} day{{'s, ' if days > 1 else ', '}}{%- endif -%}
{%- if hours -%}{{hours}} hour{{'s, ' if hours > 1 else ', '}}{%- endif -%}
{%- if seconds > 59 -%}{{minutes}} minute{{'s' if minutes > 1}}{%- endif -%}
{%- if seconds < 60 and seconds > 0 -%}< 1 minute{%- endif -%}

Added seconds support which shows ā€œ< 1 minuteā€ under a minute and nothing when the timer hits 0. before it was showing ā€œ0 minuteā€ below a minute and when the printer was offline

1 Like

By the way, there is a standard function to convert timestamps into human-friendly relative time strings:

{% set etime = states.sensor.ender_3_pro_time_remaining.state | int %}
{% set now = now() %}
{% set aware_dt = now.fromtimestamp(etime, now.tzinfo) %}
{{ relative_time(aware_dt) }}
1 Like

Hmm is it possible to do this for multiple printers easily?

Hi, Hoping anyone can share some help. Reading this post I tried the code, but Iā€™m still getting seconds and not days, hours and minutes.

  • platform: template
    sensors:
    ender_pro_time_elapsed:
    friendly_name: ā€˜Ender 3 Time Elapsedā€™
    value_template: >-
    {% set etime = states.sensor.ender_pro_time_remaining.state | int %}
    {% set seconds = etime % 60 %}
    {% set minutes = ((etime % 3600) / 60) | int %}
    {% set hours = ((etime % 86400) / 3600) | int %}
    {% set days = (etime / 86400) | int %}
    {%- if days -%}
    {{days}} day{{'s, ā€™ if days > 1 else ', '}}
    {%- endif -%}
    {%- if hours -%}
    {{hours}} hour{{'s, ā€™ if hours > 1 else ', '}}
    {%- endif -%}
    {{minutes}} minute{{ā€˜sā€™ if minutes > 1 else ā€˜ā€™}}
    ender_pro_time_remaining:
    friendly_name: ā€˜Ender 3 Time Remainingā€™
    value_template: >-
    {% set rtime = states.sensor.ender_pro_time_remaining.state | int %}
    {% set seconds = rtime % 60 %}
    {% set minutes = ((rtime % 3600) / 60) | int %}
    {% set hours = ((rtime % 86400) / 3600) | int %}
    {% set days = (rtime / 86400) | int %}
    {%- if days -%}
    {{days}} day{{'s, ā€™ if days > 1 else ', '}}
    {%- endif -%}
    {%- if hours -%}
    {{hours}} hour{{'s, ā€™ if hours > 1 else ', '}}
    {%- endif -%}
    {{minutes}} minute{{ā€˜sā€™ if minutes > 1 else ā€˜ā€™}}`

Hi, starting up this topic again.

I have:

      octoprint_time_elapsed_friendly:
        friendly_name: "Octoprint Time Elapsed"
        value_template: "{{ states('sensor.ender3pro_time_elapsed') | int | timestamp_custom('%H:%M:%S', 0) }}"
      octoprint_time_remaining_friendly:
        friendly_name: "Octoprint Time Remaining"    
        value_template: "{{ states('sensor.ender3pro_time_remaining') | int | timestamp_custom('%H:%M:%S', 0) }}"

works very well.

I would like to have a sensor that shows the ā€œend-timeā€, thus not the remaining time but the time it will be finished.

Something like ā€œnow + time remainingā€, how can I achieve that?

Think I have it:

      octoprint_time_finished_calculated:
        friendly_name: "Octoprint Time Finished"
        value_template: "{{ (states('sensor.ender3pro_time_remaining') | float + now().timestamp() | int) | timestamp_custom('%H:%M:%S', 0) }}"
1 Like

Ok, that last post, bottomā€¦ bouncing into the bloody time shit of homeassistantā€¦ now showing me time readyin UTCā€¦ But I am not ā€¦

{{ utcnow().tzinfo }}
{{ now().tzinfo }}
{{ now().astimezone().tzinfo }}

UTC
Europe/Amsterdam
CEST

How for freak sake can I change this.

Was there already a WTF bout time issues in hass?

2 Likes

Iā€™m very nooby here, but where are all the above snippits meant to go? Iā€™ve yet to see a complete example start to finish of how to insert this into the configuration.yaml to convert seconds to minutes and hours.

Like does it go within ā€˜octoprintā€™ ? does it stand alone as itā€™s own sensor and somehow modify the time displayed by the time remaining and time elapsed sensors created by octoprint? Iā€™m really confused by this. Iā€™ve pasted in your examples from above in various places in the config.yaml file and nothing changes.

1 Like

Interesting, but a complete tutorial would be welcome

Thats what this forum is for :slight_smile:

is there a solution ? Because I have no clue how to transform a timestamp into a remaining minutes

Ill be interested to try this one, working yeah?

(I cant restart right now to test)

solved my problem with time conversion!! thank you!!