Time Template Conversion HH:MM:SS to X Days, X Hours, etc

Okay this is probably gonna be super ridiculous and simple but after an hour of checking all the time conversion threads I cannot find anything showing how I can get X hours, X minutes and XX seconds from a value that is just HH:MM:SS…

More specifically it’s a glances uptime rest sensor. I have numerous templates myself that convert actual timestamps to that format but can’t seem to find one that does that conversion… also I’m not sure how it treats days I believe it will be X days, HH:MM:SS

Or if there is a way to get an actual timestamp from glances instead of what this is.

1 Like

I’m assuming the HH, in HH:MM:SS, can exceed 24 hours in order to represent a time period spanning days.

Paste this into the Template Editor and experiment with it:

{% set t = '220:10:32'.split(':') %}
{{t[0]|int // 24}} days, {{t[0]|int % 24}} hours, {{t[1]}} minutes, {{t[2]}} seconds

In your case it will probably be something like:

{% set t = states('sensor.whatever').split(':') %}
{{t[0]|int // 24}} days, {{t[0]|int % 24}} hours, {{t[1]}} minutes, {{t[2]}} seconds
1 Like

Thank you, and if it doesn’t go over 24 and shows the days on it’s own do you know how to handle that? As an example I came across of the glances uptime seems to populate the days on it’s own:

Or would it not matter because the hours won’t go over 24?

I do but you’ll have to post a few examples of the data in order for me to compose an appropriate template.

It would just be:

5 Days, 6:30:20 

For: 5 days, 6 hours, 30 minutes, and 20 seconds

I can also check back once my sensor goes over a day tomorrow… unfortunately “lights and plugs” on every single label in my breaker panel was not the kindest thing the previous owner’s electrician could have done… :grimacing:

OK. Do you want it to show 0 days, or would you prefer it didn’t report that part at all if the elapsed time is less than a day?

In other words, this:

0 days, 6 hours, 30 minutes, and 20 seconds

Or this?

6 hours, 30 minutes, and 20 seconds

I would rather not have that part at all if it hasn’t been a day yet, as that would match the others I have

Is there like I guide you are referring to to know all this… or is it just years of experience? I was searching everywhere but maybe I should be specifying something else when looking

Paste this into the Template Editor to see how it works:

{% set d = '5 Days, 6:30:20'.split(', ') %}
{% set x = 1 if 'day' in d[0]|lower else 0 %}
{% set t = d[x].split(':') %}
{{ '{}{} hours, {} minutes, and {} seconds'.format(d[0]|lower~', ' if x == 1 else '', t[0], t[1], t[2]) }}

Replace ‘5 Days, 6:30:20’ with ‘6:30:20’ and the result will not show 0 days.

Yet again, you will need to modify the template to use your sensor’s value. What is the entity name of the sensor?

2 Likes

It is:

sensor.intelnuc_glances_uptime

In that case, the first line of the template I posted becomes:

{% set d = states('sensor.intelnuc_glances_uptime').split(', ') %}

Will you be using the template in a Template Sensor?

I would do this by converting the input into a unix timestamp, then you can use all the normal functions that operate on those for display.

Yes it’s for the value_template of a rest sensor

Do you know how to do that conversion? I had read in the epic time conversion thread that values like hh:mm don’t work for the normal conversions, also it will be prefixed with the number of ‘X days’ after 24 hours

Unless I just misunderstood what he was referring to

That seems to work perfectly, thank you!

1 Like

This gives an integer unix timestamp in seconds, plus some of the conversions I use:

  {% set t = '220:10:32'.split(':') %}
  {% set ts_xdiff = ((t[0])|int*3600) + ((t[1])|int*60) + (t[2]|int) %}
  {{ ts_xdiff }}
792632

  {{ timedelta(seconds=ts_xdiff) }}
9 days, 4:10:32

  {% if ts_xdiff >= 604800 %}{{ (ts_xdiff // 604800) | int }} w, {{ (ts_xdiff % 604800 // 86400) | int }} d
  {% elif ts_xdiff >= 86400 %}{{ (ts_xdiff % 604800 // 86400) | int }} d, {{ (ts_xdiff % 86400 // 3600) | int }} h
  {% elif ts_xdiff >= 3600 %}{{ (ts_xdiff % 86400 // 3600) | int }} h, {{ (ts_xdiff % 3600 // 60) | int }} m
  {% elif ts_xdiff >= 600 %}{{ (ts_xdiff % 3600 // 60) | int }} m
  {% elif ts_xdiff >= 60 %}{{ (ts_xdiff % 3600 // 60) | int }} m, {{ (ts_xdiff % 60) | int }} s
  {% else %}{{ (ts_xdiff % 60) | int }} s
  {% endif %}

1 w, 2 d

The last one is a friendly time with precision that decreases as the time value increases, so it uses at most 2 values to display

The hours value won’t exceed 24. If the elapsed time exceeds 24 hours it will indicate X Days. The challenge, for your proposed technique, is to convert this to a timestamp:

5 Days, 6:30:20

1 Like

Is there a way to get it to drop the seconds after the first minute? :grimacing:
Putting a Lovelace page together it it seems mushed and like maybe that’s what a different friendly time I have is doing…

And you never answered, is this just from years of experience or do you have some sort of reference for some of this stuff? Is this in jinja templating docs that I could read?

Yes and I’ll leave that to you as an exercise. I suggest you use the Template Editor to carry out your experiments.

Months of practice. My primary references are:

  • The documentation’s section on Templating.
  • The documentation for Jinja (that’s the templating language).
  • Some python.
  • The examples in this community forum.

Hi
Here’s the final code which is ok with uptime sensor get on Glances API
My sensor is a timestamp sensor on HA. So in the card diaply or dashbors HA manage him-self the displaying of 1 days ago, 1 hour ago,…

rest:
  - scan_interval: 300 # seconds
    resource: http://192.168.2.20:61208/api/3/uptime
    sensor:
      - name: "proxmox_uptime"
        unique_id: "proxmox_uptime"
        value_template: >
              {% set d = value_json.split(', ') %}
              {% set x = 1 if 'day' in d[0]|lower else 0 %}
              {% set t = d[x].split(':') %}
              {% set day = 0 if x == 0 else d[0].split(' ')[0]  %}
              {% set sec = (t[2] | int + ( t[1] | int * 60 ) + (( t[0] | int + 24 * day | int ) *  3600) )| int %}
              {{ (now() | as_timestamp - sec) | as_datetime  }}
        device_class: timestamp

Note that glances Integration in HA getting uptime sensor before but for unknown reason, it disapear.
So with Glances API, we can get a lot of information on our proxmox or linux server
https://glances.readthedocs.io/en/latest/api.html#get-uptime

You can get in a web GUI all the information of your glances server
http://IP:61208