Template sensor - store previous attribute value in another attribute

See my solution to the similar question in this post: