Group trigger - get entity which triggered group change

Hi,

I have an automation which triggers when a group of people changes from ‘home’ to ‘not home’. Essentially works as a last to leave trigger. However I want to the the name of the person whom is last to leave.

Below is the automation’s YAML:

alias: Auto Alarm
description: Alarm not set reminder
trigger:
  - platform: state
    entity_id:
      - group.family
    from: home
    to: not_home
    for:
      hours: 0
      minutes: 10
      seconds: 0
condition:
  - condition: and
    conditions:
      - condition: state
        entity_id: alarm_control_panel.alarm
        state: disarmed
      - condition: state
        entity_id: input_boolean.guest_mode
        state: 'off'
action:
  - device_id: ********
    domain: mobile_app
    type: notify
    message: No-one is home and the alarm isn't set!
    title: Alarm reminder
mode: single

I want to update the message so that it can read - Person 1 was the last to leave and the alarm hasn’t been set, but I can’t seem to get the name of the person from the group which caused the state to change. I believe this information is available as the logbook entry for the group reports who caused the state to change.

The ultimate plan is to use the name to send a first reminder to the last person to leave then a message to the rest of the group.

I have tried {{ trigger.to_state.attributes.friendly_name }} but this gives me the name of the group not the person who triggered the group update.

Hope this makes sense, and any help and advice will be greatly appreciated.

Thanks in advanced.

James

You would have to reverse sort the expanded group by last_changed and pick the first member of the group. Like this:

action:
  - device_id: ******** # there's no need to hide device ids. No one can use them against you.
    domain: mobile_app
    type: notify
    title: Alarm reminder
    message: >
      No-one is home and the alarm isn't set!

      Last person to leave was:

      {{ ( expand('group.family')
        | sort(reverse=true, attribute='last_changed')
        | map(attribute='name')
        | list )[0] }}

      Please admonish them severely!

Is there potential that someone else moving from one zone to another in the 10 minute window would find themselves at the top of the list despite not being the last to leave home?

Yes indeed. This will only work if the one Home zone is defined and no other zones.

    message: >
      No-one is home and the alarm isn't set!

      Last person to leave was:

      {{ ( expand('group.family')
        | sort(reverse=true, attribute='last_changed')
        | map(attribute='name')
        | list )[0] }}

      But it might have been:

      {{ ( expand('group.family')
        | sort(reverse=true, attribute='last_changed')
        | map(attribute='name')
        | list )[1] }}

      Punish them both just in case!

Lol.

Actually I think it should be ok. The state trigger runs this automation right after the last person leaves home. So that should be the last updated entity in 99.999% of cases (all statistics are made up).