You could use a trigger-based template sensor rather than an automation, which is conceptually slightly simpler as it only needs one entity (the sensor) rather than two (the automation and the helper).
template:
- trigger:
- trigger: state
entity_id: sun.sun
to: 'below_horizon'
sensor:
- name: Most recent sunset
state: "{{ now() }}"