This trigger-based template sensor writes the current state of a sensor. I would like to capture the last_changed attributes of both the ‘on’ state and ‘off’ state. In this current example, the previous state ( either last_on or last_off) is overwritten. Is there a way to retain the previous state’s attribute each time the entity triggers?
template:
- trigger:
- platform: state
entity_id: binary_sensor.living_room_zone_motion
not_from:
- unknown
- unavailable
binary_sensor:
- name: Test Attribute Overwrite
state: "{{ trigger.to_state.state }}"
attributes:
last_on: "{{ trigger.to_state.last_changed.isoformat() if trigger.to_state.state == 'on' }}"
last_off: "{{ trigger.to_state.last_changed.isoformat() if trigger.to_state.state == 'off' }}"