Since_last_boot gone, now how to?

HI,

Since the latest HA.84+ since_last_boot on system_monitor is deprecated. see https://github.com/home-assistant/home-assistant-polymer/pull/2087 for details on how this is now implemented in Lovelace with device_class timestamp.

How should we do this in Non-lovelace?

Can we set a sensors device_class: timestamp? And if so, how do we set the type relative, total, date, time and datetime?

I know we can template the regular sensor.last_boot, and I do:

  since_last_boot_template:
    friendly_name: 'Since last boot'
    value_template: >
      {{states('sensor.since_last_boot').split('.')[0]}}

Not sure how we can achieve the other options?

{{ relative_time(states.sensor.last_boot.last_changed)}} isn’t the same as the new options in Lovelace.

1 Like

Haven’t tried it yet, but this entry under ‘Breaking Changes’ would have made me think to simply use last_boot instead of since_last_boot in your sensor configuration:

Thanks,
yes I’ve done that, after all, I knew it was coming…

doesn’t yield the same result yet, but I can’t tell anymore, since my 2 systems both use 84.1 now.

need to reread this: Template help to improve on this? and will followup there…

This is what the sensor used to look like - what does it show now?

image

my current template is:

I can use the macro template on that:

havent got ‘days’ yet, since I rebooted the system, but think the template simply isn’t the same as before, so using that as base for the macro template won’t work.

how to get from last_boot to the old since_last_boot format…

I’m not sure I follow exactly what you are asking/doing but I have this:

image


  rpi_uptime:
    friendly_name: RPi Uptime
    value_template: >
      {% set up_time = as_timestamp(now()) - as_timestamp(states('sensor.last_boot')) %}

      {% set days = (up_time // (60 * 60 * 24)) | int %}

      {% set weeks = (days // 7) | int %}

      {% set hours = (up_time // (60 * 60)) | int %}
      {% set hours =  hours - days * 24 %}

      {% set minutes = (up_time // 60) | int %}
      {% set minutes = minutes - (days * 24 * 60) %}

      {% set days = (days | int) - (weeks * 7) %}

      {% macro phrase(value, name) %}
                {%- set value = value | int %}
                {%- set end = 's' if value > 1 else '' %}
                {{- '{} {}{}'.format(value, name, end) if value | int > 0 else '' }}
      {%- endmacro %}
                
      {% set text = [ phrase(weeks, 'week'), phrase(days, 'day'), phrase(hours, 'hr'), phrase(minutes, 'min') ] | select('!=','') | list | join(', ') %}
      {% set text = text.replace(', ', ' and ', -1) %}

      {{ text }}

  ha_uptime:
    friendly_name: HA Uptime
    value_template: >
      {% if states('sensor.uptime') == '0.0' %} 
        Just restarted...
      {% else %}
        {% macro phrase(value, name) %}
        {%- set value = value | int %}
        {%- set end = 's' if value > 1 else '' %}
        {{- '{} {}{}'.format(value, name, end) if value | int > 0 else '' }}
        {%- endmacro %}
        
        {% set weeks = (states('sensor.uptime') | int / 7) | int %}
        {% set days = (states('sensor.uptime') | int) - (weeks * 7) %}
        {% set hours = (states('sensor.uptime') | float - states('sensor.uptime') | int) * 24 %}
        {% set minutes = (hours - hours | int) * 60 %}

        {{ [ phrase(weeks, 'week'), phrase(days, 'day'), phrase(hours, 'hr'), phrase(minutes, 'min') ] | select('!=','') | list | join(', ') }}
      {% endif %}

  last_boot_date_time:
    friendly_name: Last Boot
    value_template: >
      {% set date = as_timestamp(states('sensor.last_boot')) | timestamp_custom('%d') %}
      {% set date = '{:01}'.format(date | int) %}
      {% if date in ('1', '21', '31') %}
        {% set date = date ~ 'st' %}
      {% elif now().day in ('2', '22') %}
        {% set date = date ~ 'nd' %}
      {% elif now().day in ('3', '23') %}
        {% set date = date ~ 'rd' %}
      {% else %}
        {% set date = date ~ 'th' %}
      {% endif %}

      {{ as_timestamp(states('sensor.last_boot')) | timestamp_custom('%H:%M on %a') }} {{ date }} {{ as_timestamp(states('sensor.last_boot')) | timestamp_custom('%b %Y') }}
1 Like

hi thanks!
wast is your last_boot sensor. Is it the last_boot directly from the system_monitor??

mine shows as: 08

so the macro isn’t useful anymore, if we can’t get that last_boot to show days hours and minutes…

I’m using systemmonitor as before…

  # Last boot time, Disk, memory & CPU usage,
  - platform: systemmonitor
    resources:
      - type: last_boot
      - type: disk_use_percent
        arg: /
      - type: memory_use_percent
      - type: processor_use

indeed, and the macro sensor was used to transform the since_last_boot, which is now gone.

I liked that though so would love to recreate the since_last_boot somehow, to be able to use the macro again…
see @chairstacker s post Since_last_boot gone, now how to? - #4 by chairstacker

I’m not sure I follow
I’m using the new last_boot. I was using since_last_boot but changed my template.

However,
I just noticed that mine hasn’t updated in my frontend. If I copy and paste the into the template editor gives the right figures.

And then I remmebered…. you can’t use now() in a template!!!
I need to do some editing!!

you can use it in a template, simply add entity_id: sensor.time

btw your rpi uptime sensor gives me
36
lol.

Yes. Thanks.

and, err, ok, so maybe it needs some tweaking :slight_smile:

EDIT:
I just ticked over to weeks, hours and minutes and got your problem of more than 60 minutes too…

image

This might be the offending line…

      {% set minutes = minutes - (days * 24 * 60) %}

I changed it to this and it works (for now :slight_smile:)

      {% set minutes = minutes - (days * 24 * 60) - (hours * 60) %}

image


EDIT2:

This doesn’t work as I hoped:

{% set text = text.replace(', ', ' and ', -1) %}

Is there any way to replace only the last occurrence?

yes, saw that too, and took it out for now. will check when all is installed :wink:

Simple solution:

Replace

{% set text = text.replace(', ', ' and ', -1) %}

with

{% set last_comma = text.rfind(',') %}
{% set text = text[:last_comma] + ' and' + text[last_comma + 1:] %}

Try the remainder math symbol ‘%’. I use the following :

{% set up_time = as_timestamp(now()) - as_timestamp(states('sensor.last_boot')) %}

  {% set minutes = (up_time // 60) | int %}
  {% set hours = (minutes // 60) %}
  {% set days = (hours // 24) %}
  {% set weeks = (days // 7) %}
  {% set minutes = (minutes % 60) %}
  {% set hours = (hours % 24) %}
  {% set days = (days % 7) %}


  {% macro phrase(value, name) %}
       {%- set value = value %}
       {%- set end = 's' if value > 1 else '' %}
       {{- '{} {}{}'.format(value, name, end) if value | int > 0 else '' }}
  {%- endmacro %}
            
  {% set text = [ phrase(weeks, 'week'), phrase(days, 'day'), phrase(hours, 'hr'), phrase(minutes, 'min') ] | select('!=','') | list | join(', ') %}

{% set last_comma = text.rfind(',') %}
{% set text = text[:last_comma] + ' and' + text[last_comma + 1:] %}


  {{ text }}

Edit: Added parentheses to make it easier to read on second minutes/hours/days.

1 Like

Yes, not having a Python background I only recently discovered // and % and I have already changed to using them in this script.
However I somehow overlooked the fact that ordering the set statements your way simplifies everything.

Thanks!

Also, I added this to account for when the uptime is less than an hour (i.e. there is no comma),

      {% if last_comma != -1 %}
        {% set text = text[:last_comma] + ' and' + text[last_comma + 1:] %}
      {% endif %}

I’ve done a lot of time display functions for uptimes on esp8266’s in the arduino IDE. The less operations the better. That’s how I found the % operator.

[This template contains no entities that will trigger an update, so we add an entity_id: line with an entity that will force an update - here we’re using a date sensor to get a daily update:](https://www.home-assistant.io/components/sensor.template/)

so add a sensor:

- platform: time_date
    display_options:
      - 'time'

or create a automation that update custom mins:

automation:
  - alias: 'nonsmoker_update'
    trigger:
      - platform: time_pattern
        minutes: '/5'
    action:
      - service: homeassistant.update_entity
        entity_id: sensor.nonsmoker

HI,

don’t want to bump this, but I think you might be able to help me.

have a related question for calculating the time between to last_changed’s, please have a look here Why is this timestamp template an hour off?

the macro provided in this thread doesn’t help…

Could I please ask for your help? I’m not a Python programmer, I’ve just blatantly stolen @khan3962’s code earlier in this thread to make an uptime sensor. However, as you noted, it doesn’t quite work right when the uptime is less than an hour, as it shows:

RPi%20Uptime

I’m not quite sure how to add the three lines of code you have in your post. I tried this:

  {% set up_time = as_timestamp(now()) - as_timestamp(states('sensor.last_boot')) %}

  {% set days = (up_time // (60 * 60 * 24)) | int %}
  {% set weeks = (days // 7) | int %}
  {% set hours = (up_time // (60 * 60)) | int %}
  {% set hours =  hours - days * 24 %}
  {% set minutes = (up_time // 60) | int %}
  {% set minutes = minutes - (days * 24 * 60) - (hours * 60) %}

  {% set days = (days | int) - (weeks * 7) %}

  {% macro phrase(value, name) %}
            {%- set value = value | int %}
            {%- set end = 's' if value > 1 else '' %}
            {{- '{} {}{}'.format(value, name, end) if value | int > 0 else '' }}
  {%- endmacro %}
  
  {% set text = [ phrase(weeks, 'week'), phrase(days, 'day'), phrase(hours, 'hr'), 
                  phrase(minutes, 'min') ] | select('!=','') | list | join(', ') %}
  {% set last_comma = text.rfind(',') %}
  {% set text = text[:last_comma] + ' and' + text[last_comma + 1:] %}
  
  {% if last_comma != -1 %}
    {% set text = text[:last_comma] + ' and' + text[last_comma + 1:] %}
  {% endif %}

  {{ text }}

thinking maybe it would replace the erroneous text with the correct text, but I still get the same result. I would really appreciate if you’d help me out. Thanks in advance!

(I used to program in FORTRAN about 30 years ago. In the years since I’ve taught myself a lot, and I’m learning all kinds of stuff since starting Home Assistant. Unfortunately solving this one is beyond me at the moment. So many languages, so little time! :smile: )