Hi, I’m new to HA and I am trying to figure out how to customize my setup a little bit.
I added a system monitor “since_last_boot” sensor to my system. It lists the system uptime in the following format: “h:mm:ss:######”
I don’t really need to know how long my system has been up down to the millionth of a second. I don’t really need seconds either for that matter. Is there any way to trim it down to just hours and minutes in the configuration.yaml?
I made a template sensor based on that same sensor to make it more readable.
Under sensor: I have the following code:
- platform: template
sensors:
since_last_boot_templated:
value_template: >-
{%- set slb = states.sensor.since_last_boot.state.split(' ') -%}
{%- set count = slb | length -%}
{%- set hms = slb[count - 1] -%}
{%- set hms_trimmed = hms.split('.')[0] -%}
{%- set hms_split = hms_trimmed.split(':') -%}
{%- set hours = hms_split[0] | int -%}
{%- set minutes = hms_split[1] | int -%}
{%- set seconds = hms_split[2] | int -%}
{%- if count == 3 -%}
{{ slb[0] ~ ' ' ~ slb[1] ~ ' ' }}
{%- endif -%}
{%- if hours > 0 -%}
{%- if hours == 1 -%}
1 hour
{%- else -%}
{{ hours }} hours
{%- endif -%}
{%- endif -%}
{%- if minutes > 0 -%}
{%- if hours > 0 -%}
{{ ', ' }}
{%- endif -%}
{%- if minutes == 1 -%}
1 minute
{%- else -%}
{{ minutes }} minutes
{%- endif -%}
{%- endif -%}
{%- if seconds > 0 -%}
{%- if hours > 0 or minutes > 0 -%}
{{ ', ' }}
{%- endif -%}
{%- if seconds == 1 -%}
1 second
{%- else -%}
{{ seconds }} seconds
{%- endif -%}
{%- endif -%}
The code looks a little complicated because the original output includes the amount of days once the last boot was more than 24 hours ago.
The original output in the example below would be: 2 days, 17:34:9.######. The output of the template sensor looks like this:
You still need to add the actual system monitor to your configuration, with the since_last_boot resource as a bare minimum. My template sensor is based on it. Without including the system monitor component, my template sensor has nothing to work with.
One more question on this subject. I have spent several hours looking at the templating page and examining templating examples, but I cannot get my template to take.
Starting from your observation that the sensor state does not includes the “days” count of the first 24 hours, I built the following template with an if-else logic to address that issue. It seems to work.