Embed a timer inside the UDT

I have a pump UDT. I want to have a tag called DutyTime in my pump UDT that show me how long each pump instance is running.
The best way to implement it that by embedded script inside the UDT which trigger every 1 min and increment DutyTime tag by 1.
The problem is there is no tag event which trigger by timer.
Is anyone can help me with a trip to do this?
I don’t want to manually write an timer script in gateway to check each pump status and plus related DutyTime. What I want this task run automatically for ech UDT instance.


Off the top of my head, I would setup a Tag Events >> Value Changed script on the pump run status tag in the UDT. Create two memory tags in the UDT, one for the last start datetime and another for the last stop datetime. When the run status tag changes to “running” have the value changed script write the current datetime to the start memory tag and vice versa for when the pump stops. Now create a 3rd tag that is an expression tag with an integer/float/double data type (whatever you want the result to be). Inside the expression tag, check to see if the start datetime is greater than the end datetime. If it is greater, use the date expression functions to subtract the start datetime from now() - in the units you want. If the end datetime is greater than or equal to the start, set the value to 0.

Hope this helps,

1 Like

To do similar things I use an expresion tag using the datediff to calculate the time the running flag has been active.


This will give me the time in seconds the tag has been in the current state, depending on the application I will use the you could add an if statement to do this calculation only when the tag = 1.

something like


I would add an expression tag to the udt. Set the expression to

{[.]running_tagName} * (dateExtract(now(0), 'minute') + 1)

This expression should return a number (1 - 60) if the pump is running and a 0 if the pump is not running. If the pump is running, the number will change every minute. You could loose a few seconds at the beginning and end of each pump cycle.

Add a value change script to this tag that increments DutyTime if the newValue does not equal 0.

1 Like

Thank you all for your solution
Thanks great.

@mcgheeiv - You may be aware of this already but wanted to point this out in case others read this trying to make a tracker for run time since last pump start instead of total run time.

If you are looking for the run time since the last pump start, this method could miss pump stop events that are less than a minute and, as a result, not reset the run time counter. It’s not clear to me if DutyTime in the original post is referring to total runtime or runtime since last start, so I thought I would point this out in case it is the latter.

You could modify this method by adding a tag value change script to the “running_tagName” too that resets the run time since last start value if the pump stops. This would make sure you capture any pump stops (assuming the stop is longer than one scan).

1 Like

Thanks William.
That’s really help