Is it possible to convert sensor.octoprint_time_remaining value from seconds to hour/minute/seconds?
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
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) }}
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) }}
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 -%}
- 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) }}"
OK, Iām confused, where do I put this ātemplateā?
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:
- your indentation is wrong under
sensors
, itās really important - have a look at the example -
octoprint_time_elapsed_friendly
andoctoprint_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!
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 -%}
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) }}"
Nice one, thanks both