Template help to improve on this?

Wait a minute…
Are you saying just by adding unit_of_measurement: you can change the actual unit of measurement?

I thought it merely used it to suffix the value on the front end. But then I’ve only ever included it on input_number and template sensors.

What would happen if you put an inappropriate unit, such as unit_of_measurement: mm ?

no, this one actually changes what it counts in. ie: setting it to minutes means it will show a value of ‘2160 minutes’ after an uptime of 36 hours.

1 Like

1 Like

HI @klogg,

your second macro template (based on system_monitors since_last_boot works perfectly:

46

It updates nicely per minute, and the 2 sensors are beautifully in sync in the frontend.

the one based on uptime doesn’t:

52
see how it reports 14 minutes, while in fact it is only online for 4 minutes, (or 0.16 hours, or 0.01 day :wink: )

its update frequency per 14 minutes is killing its accuracy, which is of course silly for a sensor like this. I tried to update it with the homeassistant.update_entity service, but that is not having any effect.

EDIT: HOLD ON!!! this has some stupid testing code… I’ll update shortly
EDIT2: Updated now, correctly I think !

Actually it doesn’t. Not if the up time is less than a day!

But I think this does :slight_smile:

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

{% if ‘day’ in states(‘sensor.since_last_boot’) %}
{% set days = states(‘sensor.since_last_boot’).split(’ ')[0] %}
{% else %}
{% set days = 0 %}
{% endif %}

{% set minutes = states(‘sensor.since_last_boot’)[-12:-10] %}
{% set hours = states(‘sensor.since_last_boot’)[-15:-13] %}
{% set weeks = (days | int / 7) | int %}
{% set days = (days | int) - (weeks * 7) %}

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

And of course credit still goes mostly to @petro

As for this one… I am starting to wonder if it is worth the effort!!!
The 14 minutes update is silly and I am not sure why it was chosen to be so imprecise?

you said it yourself:

The sensor counts days to 2 decimal places. 0.01 of a day is 14 minutes.

not just yet, shorttime is undefined, you havent set it yet.

It seems to work for times less than a day…

image

btw did you notice the sensor.since_last_boot updates every 30 secs, and not every minute?
As far as I can tell, its the only one with this update frequency. Of course the new Macro sensor does so too now.

That might be nice to know, and come in handy, if one needs an entity_id in an template without entity_id on which HA can update.

No I didn’t notice, good spot and good idea for another use of it!
Have you changed to the latest version I posted? Can I assume it shows correctly for times greater than one day?

29 yes it does:

1 Like

@klogg
too bad dev’s/#balloob decided to do this, coincidence doesn’t exist…:

hope the new alternative will prove to be of help for the sensor…

ha!
All that work for nothing :slight_smile:
Except education :wink:

Looks like you can still get runtime out of a change that was linked.