I was trying to spread things out timewise. Totalwell comes from the controller and is only written/changed at midnight. At Wonderware the totalprev is overwritten by totalwell at 11 pm. Totalwell then changes at 12 and the subtraction happens at 1 am. The strange part is that the difference the script has been coming up with is close to correct. Some days it is off by 20k , the next day it is off by 43k. The difference is always less than what it should be. When I look at he values of Totalprev in wonderware it is correct and the Totalwell value is correct so it has to be related to the subtraction in the script or the format of the number. I think I am going to focus on performing this step in the controller.
Thanks for your help.