Quick sensor/template question - Last active motion sensor

I’ve been using this for a while and it’s pretty good

      last_motion:
        friendly_name: 'Last Motion'
        icon_template: 'mdi:walk'
        value_template: >
          {%- set sensors = 'binary_sensor.first_floor_motion_sensor', 'binary_sensor.outdoor_motion_sensor', 'binary_sensor.great_room_motion_sensor', 'binary_sensor.bedroom_stairs_motion_sensor', 'binary_sensor.bedroom_motion_sensor', 'binary_sensor.closet_motion_sensor' %}
          {{ expand(sensors) | sort(reverse=true, attribute="last_changed") | map(attribute='name') | list | first | default(none) }}

it tells me which motion sensor last changed. But what is not the best is that when a motion sensor goes inactive, that also counts as a change. Any suggestions for a way to determine which motion sensor last went active, even if one of them is still active

Example
Bedroom to On
then
10 seconds later Hallway to on
then 10 seconds later Bedroom to Off
I’d like the sensor to say “Hallway”

Thanks in advance

try this:

{%- set sensors = 'binary_sensor.first_floor_motion_sensor', 'binary_sensor.outdoor_motion_sensor', 'binary_sensor.great_room_motion_sensor', 'binary_sensor.bedroom_stairs_motion_sensor', 'binary_sensor.bedroom_motion_sensor', 'binary_sensor.closet_motion_sensor' %}
{{ expand(sensors) | selectattr('state', 'eq', 'on') | sort(reverse=true, attribute="last_changed") | map(attribute='name') | list | first | default(none) }}
1 Like

Close, this does do what I asked, but I realize it’s not quite what I want. I’d like it to stay with the last sensor, even when the last active sensor turns off. This one shows “Null” when no sensors are active.

1 Like

OK, then try this one:

{%- set sensors = 'binary_sensor.first_floor_motion_sensor', 'binary_sensor.outdoor_motion_sensor', 'binary_sensor.great_room_motion_sensor', 'binary_sensor.bedroom_stairs_motion_sensor', 'binary_sensor.bedroom_motion_sensor', 'binary_sensor.closet_motion_sensor' %}
{% set sensors_on = expand(sensors) | selectattr('state', 'eq', 'on') | map(attribute='name') | list %}
{% if sensors_on | count > 0 %}
  {{ expand(sensors) | selectattr('state', 'eq', 'on') | sort(reverse=true, attribute="last_changed") | map(attribute='name') | list | first | default(none) }}
{% else %}
  {{ expand(sensors) | sort(reverse=true, attribute="last_changed") | map(attribute='name') | list | first | default(none) }}
{% endif %}
3 Likes

Thank you! works perfectly

1 Like

Hi and sorry to bump this old topic…
I’m using this also, but I also would like to see how long ago the last sensor was active. I’ve tried the last_changed attribute on the template, but then it will tell when the template changed. So if first the garage sensor is active and then the living room, and the living room remains active, it will only tell how long ago it was the living room became active.
Any suggestions are welcome!

1 Like

Hi there, since yesterday I also use this nice solution to seee the last activated motion sensor. And I am also curious how to display the activation time of the last active motion sensor.

Also, I want to use some replace string function, because the mushroom tamplate card shows the complete friendly name of the last active motion sensor and this does not fit into the tile, therefore I would like to crop the word "Bewegung " at the begining. I know there is a regex function to do so, but in this particular case I don’t know how to implement that.

In Node Red I use this in a function and it works well:

 var patt = new RegExp(" Bewegung ", "g");
 msg.payload = msg.payload.replace(patt, "");

How to use it within here

{{ expand(sensors) | selectattr('state', 'eq', 'on') | sort(reverse=true, attribute="last_changed") | map(attribute='name') | list | first | default(none) }}

BR
TurboKanne

ok, i got it:

{{ expand(sensors) | selectattr(‘state’, ‘eq’, ‘on’) | sort(reverse=true, attribute=“last_changed”) | map(attribute=‘name’) | list | first | default(none) | regex_replace(find=‘Bewegung ‘, replace=’’, ignorecase=False) }}

answered it myself :slight_smile:
being happy

1 Like