I finally found some inspiration from here. This seems to be working well.
template:
- trigger:
- platform: state
entity_id: binary_sensor.living_room_zone_motion
not_from:
- unknown
- unavailable
variables:
to_state: "{{trigger.to_state.state}}"
last_changed: "{{ trigger.to_state.last_changed.isoformat() }}"
binary_sensor:
- name: Test Attribute Overwrite
state: "{{ trigger.to_state.state }}"
attributes:
last_on: |-
{% set current = this.attributes.get('last_on', {}) %}
{{ last_changed if to_state == 'on' else current }}
last_off: |-
{% set current = this.attributes.get('last_off', {}) %}
{{ last_changed if to_state == 'off' else current }}