I haven’t dug into your current logic, but a cursory scan suggests you’re not using a couple of Entity features that could help you.
When you patch an entity it will get a dirty
property that tells you what has change. It will also be able to report what the original values were (described in the same section).
These tools may simplify your task.
You should, in theory, be able to patch your entity, prepare the history, then do both saves as a single transaction to insure there is never a mismatch between the data and the history.