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:
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:
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') }}
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.
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:
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! )