On Custom UI, I can easily make it so that it will show the last time a sensor has changed.
However, when you restart HA, those counters reset. I’d like to use the last_tripped_time of a binary sensor instead. You can have that value show if you use something like this:
customize_glob:
"binary_sensor.*":
extra_data_template: ${attributes.last_tripped_time}
custom_ui_state_card: state-card-custom-ui
but doing so shows raw seconds, and not a user friendly relative time. Any way to make these seconds show in a user friendly relative time?
I use the code below in the sensor component/section of my configuration file for converting seconds into hours and minutes similar to this:
- platform: template
sensors:
living_room_motion_last_detected:
friendly_name: Living Room Motion Last Detected
value_template: >-
{% set lrm_uptime = states.binary_sensor.living_room_motion.attributes["last_tripped_time"] | int %}
{% set minutes = ((lrm_uptime % 3600) / 60) | int %}
{% set hours = ((lrm_uptime % 86400) / 3600) | int %}
{% set days = (lrm_uptime / 86400) | int %}
{%- if lrm_uptime < 60 -%}
Less than a minute ago
{%- else -%}
{%- if days > 0 -%}
{%- if days == 1 -%}
1 day
{%- else -%}
{{ days }} days
{%- endif -%}
{%- endif -%}
{%- if hours > 0 -%}
{%- if days > 0 -%}
{{ ', ' }}
{%- endif -%}
{%- if hours == 1 -%}
1 hour
{%- else -%}
{{ hours }} hours
{%- endif -%}
{%- endif -%}
{%- if minutes > 0 -%}
{%- if days > 0 or hours > 0 -%}
{{ ', ' }}
{%- endif -%}
{%- if minutes == 1 -%}
1 minute ago
{%- else -%}
{{ minutes }} minutes ago
{%- endif -%}
{%- endif -%}
{%- endif -%}
1 Like