How to edit sensor printout

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 had the same issue.

Haven’t implemented it yet but it should work. Just need to find some time to add it to my config file.

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:

If you just want h:mm:ss instead of h:mm:ss.######, then use this for a template:

states.sensor.since_last_boot.state.split('.')[0]
6 Likes

Am I missing something? I added the template code but don’t get the sensor. how would I add the actual sensor?

https://gist.github.com/CCOSTAN/9934de973a293b809868

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.

Thanks! I added the template that you shared and it is much nicer to look at now.

2 Likes

Thanks for this code, how can I drop the seconds from the readout and just have days hours minutes.

You can simply strip it down to this:

- 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 -%}

        {%- 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 -%}
5 Likes

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.

Here is my sensor:

- platform: command_line name: CPU Temp command: "cat /sys/class/thermal/thermal_zone0/temp" unit_of_measurement: "°F" value_template: '{{ value | multiply(0.001) * 1.8 + 32 }}'

It displays anywhere from 4-10+ decimal points.

I tried round(0), with pipes and no pipes and brackets and no brackets, but all to no avail. What am I doing wrong?

Try this: {{ (value | multiply(0.001) * 1.8 + 32) | round(0) }}

2 Likes

Thanks, David! It worked. I have a lot of learning still to do. It was that parenthesis after 32 that I was missing.

1 Like

Thank you @fanaticDavid for your code. I tried to modify it a bit, but unfortunately I cannot figure it out. I want my output to look like:

5 dni, 6 ur 55 min

With my code Home Assistant starts up, but “Time since last boot” is “unknown” (I have translated words day (dni) and hour (ur) to my native language)

My code:

- platform: template
  sensors:
    since_last_boot_templated:
      value_template: >-
        {%- set slb = states.sensor.since_last_boot.state.split(' ') -%}
        {%- set count = slb | length -%}
        {%- set days_temp = slb[count - 0] -%}
        {%- set days_split = days_temp.split(' ')[0] -%}
        {%- set days = days_split[0] | int -%}
        {%- 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 days > 0 -%}
          {%- if days == 1 -%}
            1 dan
          {%- elif days == 2 -%}
            2 dneva
          {%- elif days == 3 -%}
            3 dnevi
          {%- elif days == 4 -%}
            4 dnevi
          {%- else -%}
            {{ days }} dni
          {%- endif -%}
        {%- endif -%}
        
        {%- if hours > 0 -%}
          {%- if days > 0 -%}
            {{ ' ' }}
          {%- endif -%}
          {%- if hours > 0 -%}
            {%- if hours == 1 -%}
              1 ura
            {%- elif hours == 2 -%}
              2 uri
            {%- elif hours == 3 -%}
              3 ure
            {%- elif hours == 4 -%}
              4 ure
            {%- else -%}
              {{ hours }} ur
            {%- endif -%}
          {%- endif -%}
        {%- endif -%}

        {%- if minutes > 0 -%}
          {%- if hours > 0 -%}
            {{ ' ' }}
          {%- endif -%}
          {%- if minutes == 1 -%}
            1 min
          {%- else -%}
            {{ minutes }} min
          {%- endif -%}
        {%- endif -%}

I am getting the following error in my log:

homeassistant.components.sensor.template: UndefinedError: list object has no element 1

Hello,

I’m new to Home Assistant and also trying to template the system monitor uptime.

I tried the code suggested here but get an error in the log, that the code isn’t expected at that location, so where should I place this template.

This is how I created it in de config:

sensor:
      ## Systemstate
      - platform: systemmonitor
        resources:
          - type: last_boot
          - type: since_last_boot
          - type: processor_use
          - type: disk_use_percent
            arg: /
          - type: memory_use_percent
      
     ## Last boot Template
      - platform: template
          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 -%}
         
              {%- if count == 3 -%}
                {{ slb[0] ~ ' ' ~ slb[1] ~ ' ' }}
              {%- endif -%}
              {%- if hours > 0 -%}
                {%- if hours == 1 -%}
                  1 hour
                {%- else -%}
                  {{ hours }} hours
                {%- endif -%}
              {%- endif -%}

Can someone help me with this?

Thank you!

//Edwin

Your indenting needed fixing, and the line sensors: was missing under the line - platform: template:

sensor:
  ## Systemstate
  - platform: systemmonitor
    resources:
      - type: last_boot
      - type: since_last_boot
      - type: processor_use
      - type: disk_use_percent
        arg: /
      - type: memory_use_percent
      
  ## Last boot Template
  - 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 -%}
          
          {%- if count == 3 -%}
            {{ slb[0] ~ ' ' ~ slb[1] ~ ' ' }}
          {%- endif -%}
          {%- if hours > 0 -%}
            {%- if hours == 1 -%}
              1 hour
            {%- else -%}
              {{ hours }} hours
            {%- endif -%}
          {%- endif -%}

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.

  value_template: >
    {% if states.sensor.since_last_boot.state.count("day") > 0 %}
    {{states.sensor.since_last_boot.state.split(",") [0] + 
    "," + 
    states.sensor.since_last_boot.state.split(",") [1].split(":") [0] + 
    " hours, " + 
    states.sensor.since_last_boot.state.split(",") [1].split(":") [1] +
    " minutes" }}
    {% else %}
    {{ "0 days, " + 
    states.sensor.since_last_boot.state.split(",") [0].split(":") [0] + 
    " hours, " + 
    states.sensor.since_last_boot.state.split(",") [0].split(":") [1] +
    " minutes" }}
    {% endif %}

Hi,

It is solved now!.
I missed the rule about the sensor: but it looked like it dit the trick.

The code from jcroed is really nice, and short. I’m using this one now,

I’ll try to dive into the code so I hopefully can write some of my own in the future.

Thank you both fanaticDavid and jcroed.

1 Like