"last time sensor turned on" sensor

I have a MQTT sensor for monitoring when my sump pump comes on. I would like to display the last time it came on in the dashboard. I’ve tried using “state.last_changed” which mostly works except that the “last_changed” time will update every time HA is restarted.

Is there a better way to get the last time that a sensor went from state X to state Y?

I do see that I could do an influxdb sensor that queries for this but was wondering if there was a better way.

I found no better way than a query on the database. Maybe you can use it.