I'm not good at all in PE, but i can read pseudo code...
I would change the algorithm to add the difference in values between the current and previous value of the source tag to the lifetime counter. But only if the current value fo the source tag > the previous value.
As such (and removing code that do not add functionality):
if 'source' > PrevVal('source', '*') then ' normal accumulation TagVal('lifetime', 'lifetime' + ('source' - PrevVal('source', '*'))) else NoOutput() end if
Normally I would see this as a job for a Totalizer tag, using the InFromTotalizer option to handle the source tag accumulation reset ("Source tag is a DCS integrator" option in the Totalizers plugin of PI System Management Tools). You can achieve a similar result using Performance Equations or PI-ACE code, but I think Totalizers are the better option for this type of task.