How to get to (or template) 'last_seen' on Person or Companion app sensors

trying to replace as much custom components/integrations with core as possible, I am stuck here, using the Composite integration which allows me to use an attribute last_seen and calculate the timezone difference between home and the tracker:

      marijn_tz_offset:
        friendly_name: Marijn time zone offset
        unit_of_measurement: hr
        value_template: >
          {% set state = states.device_tracker.me %}
          {% if state.attributes is not none and
                state.attributes.time_zone is not none and
                state.attributes.time_zone != 'unknown' %}
            {% set n = now() %}
            {{(n.astimezone(state.attributes.last_seen.tzinfo).utcoffset() -
               n.utcoffset()).total_seconds()/3600}}
          {% else %}
            undetermined
          {% endif %}

I’d love to be able to do this with the person integration, or the device_tracker I base that on, the now core companion app. Neither of these have an attribute last_seen though.

Could we use the available attributes to re-create this. Or would a FR for adding last_seen to the companion app sensors attributes be useful.

these are close:

{{ state_attr('device_tracker.me', 'last_seen').astimezone(now().tzinfo) }}
{{ states.device_tracker.calltheboss.last_updated.astimezone(now().tzinfo) }}

but cant be used interchanged


thanks for having a look

been having a thing here, but might have found the answer:

          {% set state = states.device_tracker.me %}
            {% set n = now() %}
            {{(states.device_tracker.me.attributes.last_seen.astimezone().utcoffset() -
               n.utcoffset()).total_seconds()/3600}}

          {{n.astimezone(state.attributes.last_seen.tzinfo).utcoffset()}}
          {{states.device_tracker.life360_marijn.last_changed.astimezone().utcoffset()}}

both bottom templates seem to return the same result:

making:

 {{(n.astimezone(state.attributes.last_seen.tzinfo).utcoffset() -
               n.utcoffset()).total_seconds()/3600}}

be identical to:

            {{(states.device_tracker.me.last_changed.astimezone().utcoffset() -
               n.utcoffset()).total_seconds()/3600}}

or, even better, since it rules out the need for another extra integration:

          {% set state = states.person.marijn %}
          {{(state.last_changed.astimezone().utcoffset() -
               now().utcoffset()).total_seconds()/3600}}

which would be very helpful indeed, hope someone can confirm I am doing this correctly? I can only test this now for all my device_trackers, which are all in the same (Home) timezone…

1 Like

dont use this, it does not do what I hoped it would, and currently there is no other way documented to calculate the timedifference between to trackers crossing timezones than using the CC Composite by Phil, when you are using the iOS app. Android seems to provide a current timezone sensor.

Ive opened a discussion some time ago in the iOS repo, and just added some more info there, hoping we could have these sensors on iOS too