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)
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:
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/
{{ 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 -%}