Have tags where operators enter readings. I want to evaluate the difference in their values in relation to the elapsed time. Have everything else working except how to find the difference in the current time stamp and the previous timestamp. Tried different ways of DATEDIFF and some valueChanged expression but i'm not getting anywhere and would appreciate some advice or direction to examples.
Thanks.
I'm not sure if your tag is holding a timestamp value, or you are trying to use the timestamp from the qualified tag path. Either way
Since you are using, I assume the tag value is the datetime, you should be able to use a valueChanged
script (which is not an expression).
def valueChanged(tag, tagPath, previousValue, currentValue, initialChange, missedEvents):
if not initialChange:
seconds = system.date.secondsBetween(previousValue.value, currentValue.value)
# write to another tag or do something else with the time diff here
If you need to do heavier calculations or expect several of these events, then move the script from the valueChanged
script to a tag change gateway script which can handle more events in fast succession.
That looks AWESOME. Not exactly what i was looking for but what you've written looks perfect.
Won't be able to see till tomorrow unfortunately, but my thanks!
Finally had a chance to try this, this morning and I'm doing something wrong, or more likely, i didn't explain the problem correctly. Forgive me, i just know enough to be dangerous (PLC programmer).
I created tags for values enter by an operator. I have more tags finding the value differences and other information needed. That all works fine. What i am looking for is to find the difference in the timestamps (minutes) for when those values were changed. Reading your advice, will i have to create another folder of tags for the timestamps to be moved into the Value or is there a better way?
If your tag's value is not a datetime, then just use the timestamp portion of the qualified value. Every tag is a qualified value that has a value, timestamp, and quality component to them.
def valueChanged(tag, tagPath, previousValue, currentValue, initialChange, missedEvents):
if not initialChange:
minutes = system.date.secondsBetween(previousValue.timestamp, currentValue.timestamp) / 60.0
# write to another tag or do something else with the time diff here
You can use system.date.minutesBetween(x,y)
but it's less granular, I like using seconds then convert to the units I want when you need a little more precision.
This is perfect! Thank you for excellent assistance.