set value = states('sensor.uptime')
set last_updated = as_timestamp(strptime(value,...
sensor.uptime is a value in minutes not a Unix datetime object. Thus the timestamp function wonāt work.
Thereās definitely an easier way to do this ( no need to convert minutes to seconds then back again) butI donāt have time to rewrite it just now. Untested but this should work:
There are a couple of other discussions around this on here and we (well @petro mainly if I remember correctly) ended up with the following for uptime.
ha_uptime:
friendly_name: HA Uptime
value_template: >
{% if states('sensor.uptime') == '0.0' %}
Just restarted...
{% else %}
{% set up_time = (states('sensor.uptime') | float * 86400) %}
{% 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(',') %}
{% if last_comma != -1 %}
{% set text = text[:last_comma] + ' and' + text[last_comma + 1:] %}
{% endif %}
{{ text }}
{% endif %}
I was using Simoneās suggestion (thanks), which worked perfectly up until last week.
Now unfortunately The sensor value resets to zero after reaching the first minute.
The only thing I changed was updating to Home Assistant Core 0.117.1.
Iām guessing this is linked to one of the breaking changes listed for 0.0117.1. #13Templates - Auto-updating now()
The entity_id: attribute was recently removed from template entities because the
templates are now able to find all referenced entities automatically. This removal
introduced a new problem where templates using the current time could no longer use entity_id: sensor.time to ensure periodic updates.
With apologies to those who have spent the last couple of releases adding
workarounds to their templates, we are now introducing an auto-refresh feature to
time-based templates.
It is thus no longer necessary to reference sensor.time , sensor.date or manually
update template entities when now() or utcnow() is present in the template.
The template will automatically be updated when:
A referenced entity changes state.
At the start of each minute when now() or utcnow() is present in the template.
Please note, if you have a time-based template where you do not want it to update
periodically it will now have to be reworked to not use now() .
Any ideas please, as I donāt really understand the syntax of this value template.
Thanks.
EDIT
Have now managed to get around this by using another template.
sensors:
uptime_clearer:
friendly_name: "Clearer Uptime"
value_template: >-
{% set uptime = states.sensor.ha_runtime.state | int %}
{% set days = (uptime / 86400) | int %}
{%- if days > 0 -%}
{{ days }} days, {{ (uptime - (days * 86400)) | int | timestamp_custom('%-H Hours, %-M Minutes', false) }}
{%- else -%}
{{ uptime | int | timestamp_custom('%-H Hours, %-M Minutes', false) }}
{%- endif -%}