Compute integral value with Value Changed scripts

Hello,
I have a small issue with creating a proper script for computing working hours of a pump based on its on/off state:

if previousValue.quality.isGood() and previousValue.value == 2:
		datedif = ((currentValue.timestamp.getTime() - previousValue.timestamp.getTime())/3.6)*10e-7
		path = "[.]Hours"
		path_f = "[.]Hours_f"
		val = system.tag.read(path).value
		val_f = system.tag.read(path_f).value
		delta = val_f+datedif
		if(delta) > 1:
			system.tag.write(path_f,delta-int(delta))
			system.tag.write(path,val+int(delta))
		else:
			system.tag.write(path_f,delta)

The Pump is an UDT with State-OPC Tag RO (above is its Value Changed) script, Hours - Memory Int4 R/W and Hours_f - Memory Float8 R/W.

Value 2 means pump on and i want to hold track of working hours only when comm state is good. I think there might be some overlapping to events, in the sense that I get an ON state at let’s say 2539 working hours (consider this t=0), after approximately 67 hours 2606, after 74 hours i get 2680.

So it’s as if a write(W1) was scheduled but not fired, another tag write was scheduled and executed (W2), and after that W1 was executed as if W2 didn;t generate a proper last change (i.e. didn’t change lastChange timestamp, only value changed).

(t=0 2539) (t=67 2606) (t=74 2680=2606+74)-> it should’ve been 2606+(74-67). The logged data come from another scada, which doesn’t report any state change except the one at t=0 so probably the event fired on bad quality transition. The 2606+74 thing is just something I’ve noticed and it doesn’t happen all the time (I tried on an all-memory UDT - writing from an OPC UA client and the code’s result is accurate to the millisecond). Also I’ve noticed although the Hours tag changes I have 0.00000 on Hours_f which seems extremely unlikely. I just can’t figure out what’s causing this behavior, any nutty idea is welcome, maybe trying synchonous writes (and yes probably i should group reads and also writes in a *All call)?