Convert sensor.octoprint_time_remaining to hour/minute/seconds

Is it possible to convert sensor.octoprint_time_remaining value from seconds to hour/minute/seconds?

https://community.home-assistant.io/search?q=hours%20minutes

1 Like

I have searched to try to solve this problem.
In the template I missed to add: entity_id: sensor.octoprint_time_remaining
Now the following value_template is working, but the minutes is wrong,

    {% set time = states.sensor.octoprint_time_remaining.state %}
    {% set days = ((time | int /  86400) | string).split('.')[0] %}
    {% set hours = ((time | int /  3600) | string).split('.')[0] %}
    {% set minutes = ((time | int /  60) | string).split('.')[0] %}
    {% set seconds = time | int % 60 %}
    {{days}} dagar {{hours}}:{{minutes}}:{{seconds}}

Could you show us in as many details as possible what exactly iw wrong with minutes?
Btw, Iā€™d suggest something like this:

{% set time = (states.sensor.octoprint_time_remaining.state | timestamp_custom('%j:%H:%M:%S:%z', true)).split(':') %}
{% set days = time[0] | int - 1 %}
{% set hours = time[1] | int - time[4][:3] | int %}
{% set minutes = time[2] | int - time[4][3:] | int %}
{% set seconds = time[3] %}
{{days}} dagar {{hours}}:{{minutes}}:{{seconds}}

Or this one (I like it more tbh):

{% set time = states.sensor.octoprint_time_remaining.state %}
{% set ztime = (0 | timestamp_custom('%j:%H:%M:%S', false)).split(':') %}
ztime={{ztime}}
{% set ctime = (time | timestamp_custom('%j:%H:%M:%S', false)).split(':') %}
ctime={{ctime}}
{% set days = ctime[0]|int - ztime[0]|int %}
{% set hours = ctime[1]|int - ztime[1]|int %}
{% set minutes = ctime[2]|int - ztime[2]|int %}
{% set seconds = ctime[3]|int - ztime[3]|int %}
{{days}} dagar {{hours}}:{{minutes}}:{{seconds}}

Itā€™s not perfect, but will do the job as soon as your time remaining is less than 1 year :wink:

Tested your code, but got an error: data not initiated

    {% set time = states.sensor.octoprint_time_remaining.state %}
    {% set hours = ((time | int /  3600) | string).split('.')[0] %}
    {% set minutes = ((time | int /  60) | string).split('.')[0] %}
    {% set seconds = time | int % 60 %}
    {{hours}}:{{minutes}}:{{seconds}}

With the code above I got this result:
PrimaCreator P120 1:86:56 (5216)

I edited my post, please try one more time.

Tested but got error: UndefinedError: list object has no element 1

I recently demonstrated how to convert seconds to HH:MM:SS in another post.

This displays days followed by HH:MM:SS.

{% set t = states('sensor.octoprint_time_remaining') | int %}
{{ '{:d} dagar {:02d}:{:02d}:{:02d}'.format(t // 86400, (t // 3600) % 24, (t % 3600) // 60, (t % 3600) % 60) }}
2 Likes

Thank you your example seems to do the job.

How can I change it so it shows only HH:MM:SS ?

Edit:
This seems to work:

{{ ā€˜{:02d}:{:02d}:{:02d}ā€™.format((t // 3600) % 24, (t % 3600) // 60, (t % 3600) % 60) }}

The template I provided is designed to display days, hours, minutes, and seconds. As a consequence, it will never show more than 23 hours in the HH field. 24 hours represents one day so, at 24 hours, it will show 00 in the HH field.

If you donā€™t intend to display days then the HH field should be allowed to display 24+ hours (up to 99). To make it work this way, change the calculation for hours from this:

(t // 3600) % 24

to this:

t // 3600

Hereā€™s the modified template:

{{ '{:02d}:{:02d}:{:02d}'.format(t // 3600, (t % 3600) // 60, (t % 3600) % 60) }}
2 Likes

I use this

- platform: template
  sensors:
    octoprint_time_elapsed_format:
      friendly_name: 'Printing Time Elapsed'
      value_template: >-
        {% set etime = states.sensor.aneta8_time_elapsed.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 -%}
        {%- if hours > 0 -%}
          {%- if hours == 1 -%}
            1 hour
          {%- else -%}
            {{ hours }} hours
          {%- endif -%}
          {{ ', ' }}
        {%- endif -%}
        {%- if minutes > 0 -%}
          {%- if minutes == 1 -%}
            1 minute
          {%- else -%}
            {{ minutes }} minutes
          {%- endif -%}
        {%- endif -%}
    octoprint_time_remaining_format:
      friendly_name: 'Printing Time Remaining'
      value_template: >-
        {% set rtime = states.sensor.aneta8_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 > 0 -%}
          {%- if days == 1 -%}
            1 day
          {%- else -%}
            {{ days }} days
          {%- endif -%}
          {{ ', ' }}
        {%- endif -%}
        {%- if hours > 0 -%}
          {%- if hours == 1 -%}
            1 hour
          {%- else -%}
            {{ hours }} hours
          {%- endif -%}
          {{ ', ' }}
        {%- endif -%}
        {%- if minutes > 0 -%}
          {%- if minutes == 1 -%}
            1 minute
          {%- else -%}
            {{ minutes }} minutes
          {%- endif -%}
        {%- endif -%}
2 Likes
- platform: template
  sensors:
    octoprint_time_elapsed_friendly:
      friendly_name: "Octoprint time elapsed"
      value_template: "{{ states('sensor.octoprint_time_elapsed') | int | timestamp_custom('%H:%M:%S', 0) }}"
5 Likes

OK, Iā€™m confused, where do I put this ā€˜templateā€™?

1 Like

in configuration.yaml, for example (but there are other options as well).
itā€™s template sensor

Tried all sorts of indentations etc and cannot get it to work
The 2 examples are also a different format from the official docs (Sensor and platform swapped)

sensor:
  - platform: template
    sensors:
  -  octoprint_time_elapsed_friendly:
  -  friendly_name: "Octoprint time elapsed"
  -  value_template: "{{ states('sensor.octoprint_time_elapsed') | int | timestamp_custom('%H:%M:%S', 0) }}"
  -  octoprint_time_remaining_format:
  -  friendly_name: 'Printing Time Remaining'
  -  value_template: >-
        {% set rtime = states.sensor.aneta8_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 > 0 -%}
          {%- if days == 1 -%}
            1 day
          {%- else -%}
            {{ days }} days
          {%- endif -%}
          {{ ', ' }}
        {%- endif -%}
        {%- if hours > 0 -%}
          {%- if hours == 1 -%}
            1 hour
          {%- else -%}
            {{ hours }} hours
          {%- endif -%}
          {{ ', ' }}
        {%- endif -%}
        {%- if minutes > 0 -%}
          {%- if minutes == 1 -%}
            1 minute
          {%- else -%}
            {{ minutes }} minutes
          {%- endif -%}
        {%- endif -%}

gives an error

Invalid config for [sensor.template]: expected dictionary for dictionary value @ data['sensors']. Got None. (See ?, line ?). Please check the docs at https://home-assistant.io/components/sensor.template/
Invalid config for [sensor]: required key not provided @ data['platform']. Got None. (See /config/configuration.yaml, line 123). Please check the docs at https://home-assistant.io/components/sensor/
Invalid config for [sensor]: required key not provided @ data['platform']. Got None. (See /config/configuration.yaml, line 123). Please check the docs at https://home-assistant.io/components/sensor/
Invalid config for [sensor]: required key not provided @ data['platform']. Got None. (See /config/configuration.yaml, line 123). Please check the docs at https://home-assistant.io/components/sensor/
Invalid config for [sensor]: required key not provided @ data['platform']. Got None. (See /config/configuration.yaml, line 123). Please check the docs at https://home-assistant.io/components/sensor/
Invalid config for [sensor]: required key not provided @ data['platform']. Got None. (See /config/configuration.yaml, line 123). Please check the docs at https://home-assistant.io/components/sensor/
Invalid config for [sensor]: required key not provided @ data['platform']. Got None. (See /config/configuration.yaml, line 123). Please check the docs at https://home-assistant.io/components/sensor/

thatā€™s because at least:

  1. your indentation is wrong under sensors, itā€™s really important - have a look at the example
  2. octoprint_time_elapsed_friendly and octoprint_time_remaining_format donā€™t need dashes in front of them, check out the docs again.

[quote=ā€œslipx06, post:11, topic:115060ā€]
Thanks, finally got it working, for completeness:

sensor:
  - platform: template
    sensors:
     octoprint_time_elapsed_friendly:
      friendly_name: "Octoprint time elapsed"
      value_template: "{{ states('sensor.ender_3_pro_time_elapsed') | int | timestamp_custom('%H:%M:%S', 0) }}"
  - platform: template
    sensors:
     octoprint_time_remaining_friendly:
      friendly_name: "Octoprint time remaining"    
      value_template: >-
        {% 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 -%}
        {%- if hours > 0 -%}
          {%- if hours == 1 -%}
            1 hour
          {%- else -%}
            {{ hours }} hours
          {%- endif -%}
          {{ ', ' }}
        {%- endif -%}
        {%- if minutes > 0 -%}
          {%- if minutes == 1 -%}
            1 minute
          {%- else -%}
            {{ minutes }} minutes
          {%- endif -%}
        {%- endif -%}

What tripped me up was the sensor names!

3 Likes

hurray!
Iā€™m almost certain that you can make your sensors a bit more compact by having then under one sensors (havenā€™t checked it though):

sensor:
  - platform: template
    sensors:
     - octoprint_time_elapsed_friendly:
       friendly_name: "Octoprint time elapsed"
       value_template: "{{ states('sensor.ender_3_pro_time_elapsed') | int | timestamp_custom('%H:%M:%S', 0) }}"
     - octoprint_time_remaining_friendly:
       friendly_name: "Octoprint time remaining"    
       value_template: >-
         {% 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 -%}
2 Likes

You can simplify that even more

> - platform: template
>   sensors:
>     octoprint_time_elapsed_format:
>       friendly_name: 'Printing Time Elapsed'
>       value_template: "{{ states('sensor.ender_3_pro_time_elapsed') | int | timestamp_custom('%H:%M:%S', 0) }}"
> 
>     octoprint_time_remaining_format:
>       friendly_name: 'Printing Time Remaining'
>       value_template: "{{ states('sensor.ender_3_pro_time_remaining') | int | timestamp_custom('%H:%M:%S', 0) }}"
2 Likes

Nice one, thanks both :slight_smile: