Adding delay in a tag event in UDT

We have 75 instances of a UDT having around 20 OPC Tags and 20+ other tags.
For an OPC Boolean tag , we have added time.sleep(5) in its tag event script to turn another memory Boolean tag True, which is used as a trigger in a transaction group to log some values. However, we are observing a delay of 1-2 Minutes to turn the memory tag True. Sometimes it is not even turning True and events are being missed.
Could someone suggest any different approach to add a delay.

sleep() is dangerous to use. And you look to be using it a lot. :wink:

  • Remove the script
  • Make the memory tag as an expression tag instead, with the expression (assuming 5 seconds after the OPC tag is true (modify to point to the location of the OPC tag):
{[.]OPCtag} &&
secondsBetween({[.]OPCtag.Timestamp}, now()) >= 5

Thank you Jordan for the suggestion. I will surely try this approach. However, the OPC tag turns False within 5 seconds so I doubt that the memory tag will never turn True. I need to add some more condition in the expression.

There are two ways, for now.
First is a Timer gateway evnet script. every 5 second,read all tags, check the value and then write the memory bool tags. That still have some delay, but less then 5s. I’m using this in my gateway.
another is use low level alarm. Alarms have original time delay, and bind your memory bool tag to alarm state. Considering that’s alarm, I was a little scared ,so I chose the first.


Our standard ways of doing timed execution’s in a UDT are something like this:

If you need to execute at a fairly precise time:

# a integer expression tag in the UDT called "trigger"

# could also be getSecond(now()) and use:
if currentValue.value % 5 == 0

If there is only the need to execute on regular intervals but the exact time of execution start is not important, you can make “trigger” a reference tag to “[System]Gateway/CurrentDateTime”. The good thing about this method is that if you instantiate your UDTs at different times, it keeps the executions from happening all at once. Because if you try to do too much at the same time with UDT’s, they can start to act strange including missing events that you expect to happen.

Then in the tag change script you have to look at the current vs. previous and record the previous when the execution happens:

# executes every 2 minutes
lastUpdate ="[.]lastUpdate").value
if, currentValue.value) >= 120:

    # do something

    system.tag.write("[.]lastUpdate", currentValue.value)

Hope it helps.


Thank you @JordanCClark and @nicholas.robinson !! both of the tricks helped me in different cases.

1 Like