Using the trigger entity in a template sensor

I’m trying to establish a sensor that tells me how many people are at home, and which person triggered the change in the count. I assumed I could use the trigger entity within an attribute template but it always returns null. Is there some documentation on what attributes you can find in the trigger object when using it in a template?

This is what I have:

- platform: template
  sensors:
    people_home_count:
      entity_id:
        - person.xxx
        - person.yyy
        - person.zzz
      friendly_name: Number of People Home
      value_template: >-
        {{ states.person|selectattr('state','equalto','home')|list|length }}
      attribute_templates:
        person: >-
          {{ trigger.entity_id }}

I should say thats in my sensors.yaml file which is included as part of the sensor: entry in configuration.yaml (hence why its missing the top level sensor:)

The main value works as expected, but the person attribute does not update.

trigger.entity_id can only be used in automations, not template sensors (there is no trigger in a sensor).

See: https://www.home-assistant.io/docs/automation/templating/#trigger-state-object

Also entity_id is no longer a valid option for templates.

Thanks for that. You say a sensor does not have a trigger, but clearly they must as one of the entity_id specified changes and that triggers an update of the sensor value. So maybe a trigger entity does not exist or is not exposed to the template but internally there must be something that exists to trigger the change.

Is there any other way to get the entity that triggered the sensor to update its value?

There is a routine that watches the entities present in the template for changes (see “listeners” in developer tools / events / available events). If a change is detected the template is re-evaluated. This is not exposed to the end user.

I guess you could inspect the last changed attribute for each of the persons and pick the one most recently updated.