How to change date and other info display format?

I am new to HA and am finding it to be an amazing platform with a great community. I have some troubles using Template Sensor component. I want to change the way System Monitor component displays “time since last boot” and “last boot” .

Now it looks like that:

When it’s more than 24 hours it is displayed like this:

4 days, 16:53:45.125478

and that dosesnt look too pretty. I would like “time since last boot” and “last boot” to be displayed like that:

4 days 16 h 53 min 43 s
2.12.2016

I was trying my best by using sensor template component but didn’t figure it out so far.

A bit off-topic now. How can I change my “ACTIVATE” text to a button like @brusc has it in his example?

Any help would be greatly appreciated

Check this thread for your first question.

Intrested to see if there is someone who knows how to do your second question.

Is it because it calls a script?

Hey Ben! @brusc poke poke How did you get the play button for your Restart action?

Thank you for your link @silvrr, I got it working for “time since last boot”, have to figure out the date format now. I used code from the link you posted and am now getting the following error in my log:

homeassistant.components.sensor.template: UndefinedError: 'None' has no attribute 'state'

but it’s all displaying correctly…

Yeah, in my case button “activate” calls a script. I was looking through Ben’s files but couldn’t figure out how he has done it…he is using a different method for restarting HA though. I couldn’t replicate it because his shell commands didn’t work for me.

This can be caused by your template code looking for the value of a sensor that hasn’t been valued yet. You can avoid the errors with something like

value_template: '{% if states.sun.sun %}{{ ((as_timestamp(states.sun.sun.attributes.next_rising)) | timestamp_local) [11:16] }}{% endif %}'

That’s my sensor that shows when the next sunrise is. The “if states.sun.sun” makes sure it exists before trying to get the value.

1 Like

Hello! Yeah, so my understanding is that things that have a defined on off state get a toggle. Scrips typically just have the activate text unless they contain a delay.

The play icon was phased out and replaced with the activate text a while back. I just need to update my config. Sorry about that!

I got stuck with my code. Home Assistant fails to load sensor.template component with the following error:

homeassistant.bootstrap: Invalid config for [sensor.template]: invalid template (TemplateSyntaxError: unexpected '%') for dictionary value @ data

My code looks like 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 -%}
        {%- set seconds = hms_split[2] | int -%}
        
        {%- if count == 3 -%}
          {%- set days_num = slb[0] -%}
          {%- set days = days_num | int -%}
          {%- set days_days = slb[1] -% -%}
          {{ slb[0] ~ ' '}}
          {%- if days > 0 -%}
            {%- if days == 1 -%}
              dan
            {%- elif days == 2 -%}
              dneva
            {%- elif days == 3 -%}
              dnevi
            {%- elif days == 4 -%}
              dnevi
            {%- else -%}
              {{ days_days }} dni
            {%- endif -%}
          {%- endif -%}
        {%- 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 -%}
        
        {%- if minutes > 0 -%}
          {%- if hours > 0 -%}
            {{ ' ' }}
          {%- endif -%}
          {%- if minutes == 1 -%}
            1 min
          {%- else -%}
            {{ minutes }} min
          {%- endif -%}
        {%- endif -%}

Can someone point me into the right direction?

You’ve got an error somewhere in that template. Use the template dev tool to diagnose.

Looks like it’s your set expressions. Those need to be in {{ }}, not {% %}

Thank you! Found my error, it’s all working nicely now. I didn’t even know there is template dev tool to diagnose. It’ll be much easier now to find errors in the future.

Hello, how do I get the captions that are everywhere under the name of the sensors in @bruhautomation example?