Time of arrival and departure of person

I have person entities which shows home and away states of person and even stores history of timestamps when person was home or away.
Now, similar to:

value_template: >
            {{ relative_time( states.person.person_name.last_changed ) }}

which shows the time when the last time the state changed, I want first time some one was detected home and the the last time when they left i.e detected ‘away’ in a day. Is it possible? Thanks in advance

template:
  - trigger:
      - platform: state
        entity_id: person.a
        to:
          - home
        not_from:
          - unknown
          - unavailable
    sensor:
      - name: First Arrived Home
        device_class: timestamp
        state: |
          {% set current = this.state | as_datetime if this is defined else now() %}
          {{ current if now().date() == current.date() else now() }}

  - trigger:
      - platform: state
        entity_id: person.a
        not_to:
          - unknown
          - unavailable
        from:
          - home
    sensor: 
      - name: Left Home Time
        device_class: timestamp
        state: "{{ now() }}"

  - trigger:
      - platform: time
        at: "23:59:59"
    sensor:
      - name: Last left home
        device_class: timestamp
        state: "{{ states('sensor.left_home_time') }}"

I tried it with replacing it with the actual person entity but it doesn’t show any time and shows unknown instead.

The information you want isn’t recorded by the system.

Drew’s sensor will start recording it when the person entity next changes state, but it will be unknown until then.

As Troon explained the trio of sensors will be unknown until each of their triggers occur. There is likely a way to get the information using a SQL query but that’s outside my wheelhouse…

okay, I waited for state to change so that it can record the time, but as soon as the state changed to home, it went from unknown to unavailable. :grimacing:
Note that i tweaked the code little bit and have removed the not_from and not_to parts as my state often goes to unknown (I’m using HA container and it happens when the pod restarts). So that it can record time when person becomes home, doesn’t matter if its transitioned from unknown or away state. Here’s my code:

template:
  - trigger:
    - platform: state
      entity_id: person.khushal_mer
      to:
        - home
    sensor:
      - name: Khushal Arrived
        device_class: timestamp
        state: |
          {% set current = this.state | as_datetime if this is defined else now() %}
          {{ current if now().date() == current.date() else now() }}

  - trigger:
      - platform: state
        entity_id: person.khushal_mer
        not_to:
          - home
    sensor: 
      - name: Khushal Left Time
        device_class: timestamp
        state: "{{ now() }}"

  - trigger:
      - platform: time
        at: "23:00:00"
    sensor:
      - name: khushal left
        device_class: timestamp
        state: "{{ states('sensor.khushal_left_time') }}"