Template of nested attributes compared to timestamp

Setup:

An entity of ‘person.name’ has an attribute of ‘source’
The value of ‘source’ is the name of a ‘device_tracker_XYZ’ entity
The entity ‘device_tracker_XYZ’ has an attribute of ‘last_seen’

I’m trying to create a template that will compare the last_seen value to current date/time with the only given knowledge of ‘person.name’.

I’m completely at a loss here.

I don’t have a single device tracker that has a last_seen variable, so I can’t say for sure that a timestamp conversion will or will not be needed… but the value can be templated as follows:

{% set x = state_attr('person.a', 'source') %}
{{ state_attr(x, 'last_seen') }}

You didn’t give much detail about what you are trying to do, but will likely need something like the following for your time comparison. This template will render true as long as it has been at least 10 minutes since the device tracker was last seen.

{% set x = state_attr('person.a', 'source') %}
{{ (now() - timedelta(minutes=10)) >=  state_attr(x, 'last_seen') | as_datetime | as_local }}

Keep in mind that many device tracker integrations do not generate a last_seen variable, so if your person entity’s state is derived from multiple device trackers, your sensor will need an availability and/or additional templating to filter out unwanted states.

Thank you very much - I didn’t realize I could set a variable to ‘source’ and then use the variable as though it were the entity itself.