Hi,
So I followed the post here (How to edit sensor printout) and have got my time being formatted ok. My problem is that the time since last motion detection doesn’t seem to update until the “next” motion that is detected weirdly. Can anyone see what I’m doing wrong here?..
- platform: template
sensors:
last_motion_time:
friendly_name: 'Time since last motion'
value_template: >-
{% set sensors = [states.binary_sensor.upstairs_motion, states.binary_sensor.downstairs_motion, states.binary_sensor.bedroom_motion, states.binary_sensor.kitchen_motion, states.binary_sensor.lounge_motion] %}
{% for sensor in sensors %}
{% if as_timestamp(sensor.last_changed) == as_timestamp(sensors | map(attribute='last_changed') | max) %}
{{ now() - sensor.last_changed }}
{% endif %}
{% endfor %}
- platform: template
sensors:
last_motion_time_templated:
friendly_name: 'Time since last motion templated'
value_template: >-
{%- set slb = states.sensor.last_motion_time.state.split(' ') -%}
{%- set count = slb | length -%}
{%- set hms = slb[count - 1] -%}
{%- set hms_trimmed = hms.split('.')[0] -%}
{%- set hms_split = hms_trimmed.split(':') -%}
{%- set hours = hms_split[0] | int -%}
{%- set minutes = hms_split[1] | int -%}
{%- set seconds = hms_split[2] | int -%}
{%- if count == 3 -%}
{{ slb[0] ~ ' ' ~ slb[1] ~ ' ' }}
{%- endif -%}
{%- if hours > 0 -%}
{%- if hours == 1 -%}
1 hour
{%- else -%}
{{ hours }} hours
{%- endif -%}
{%- endif -%}
{%- if minutes > 0 -%}
{%- if hours > 0 -%}
{{ ', ' }}
{%- endif -%}
{%- if minutes == 1 -%}
1 minute
{%- else -%}
{{ minutes }} minutes
{%- endif -%}
{%- endif -%}
{%- if seconds > 0 -%}
{%- if hours > 0 or minutes > 0 -%}
{{ ', ' }}
{%- endif -%}
{%- if seconds == 1 -%}
1 second
{%- else -%}
{{ seconds }} seconds
{%- endif -%}
{%- endif -%}
{{ ' ago' }}