window2_last_changed:
entity_id: sensor.time
value_template: >-
{%- set time = (as_timestamp(now()) - (state_attr('sensor.window2', 'lastUpdated') | int / 1000 )) | int %}
{%- set minutes = ((time % 3600) // 60) %}
{%- set hours = ((time % 86400) // 3600) %}
{%- set days = (time // 86400) %}
{% if time <= 60 %}
{# minimun or = 59 sec #}
0m
{% elif time <= 3600 %}
{# minimum or = 59 min #}
{{ minutes }}m
{% elif time <= 86400 %}
{# minimum or = 23h and 59 min #}
{{ hours }}t
{% elif time > 86400 %}
{# more or = 1 day #}
{{ days }}d
{% else %}
{% endif %}
Here is a error
ValueError: Template error: int got invalid input 'None' when rendering template 'window2_last_changed:
entity_id: sensor.time
value_template: >-
{%- set time = (as_timestamp(now()) - (state_attr('sensor.window2', 'lastUpdated') | int / 1000 )) | int %}
{%- set minutes = ((time % 3600) // 60) %}
{%- set hours = ((time % 86400) // 3600) %}
{%- set days = (time // 86400) %}
{% if time <= 60 %}
{# minimum or = 59 sec #}
0m
{% elif time <= 3600 %}
{# minimum or = 59 min #}
{{ minutes }}m
{% elif time <= 86400 %}
{# minimum or = 23h and 59 min #}
{{ hours }}t
{% elif time > 86400 %}
{# more or = 1 day #}
{{ days }}d
{% else %}
{% endif %}' but no default was specified
Wrong lastUpdated value when use value " int (0) "
entity_id is not a valid configuration key for template sensors
lastUpdated is not a valid attribute… in fact last_updated is not an attribute at all. As such it cannot be accessed using the state_attr(), you must use the state object method:
When using the state object method it is necessary to assign an availability.
You have included an else that returns nothing…
Additionally, you are using the legacy format instead of the current one and you are re-calculating values when you could just be using the variables you have defined.
template:
- sensor:
- name: "window2_last_changed"
state: >-
{%- set time = (now() - states.sensor.window2.last_updated).total_seconds() %}
{%- set minutes = ((time % 3600) // 60) %}
{%- set hours = ((time % 86400) // 3600) %}
{%- set days = (time // 86400) %}
{% if days | bool %}
{# more or = 1 day #}
{{ days }}d
{% elif hours | bool %}
{# minimum or = 23h and 59 min #}
{{ hours }}t
{% elif minutes | bool %}
{# minimum or = 59 min #}
{{ minutes }}m
{% else %}
0m
{% endif %}
availability: >
{{ states('sensor.window2') not in [None, 'unavailable', 'unknown'] }}
If you don’t mind having the value displayed with a slightly different appearance, you can simply use the relative_time() function. Try it and see if it’s satisfactory.
template:
- sensor:
- name: "shell_mortensrud_last_changed"
state: >-
{% set dt = strptime(state_attr('sensor.shell_mortensrud_95', 'lastUpdate'), '%d.%m.%Y %H:%M:%S') | as_local %}
{% set n = now() %}
{{ relative_time(dt) }}
availability: >
{{ states('sensor.shell_mortensrud_95') not in [None, 'unavailable', 'unknown'] }}
I want remove .0 in time format and tried {{ timetext.strip('.0') }} but .0 not removed. I want time like 1t, 16t, 7m, 30m( t called hours and m called minutes )