Expression tag - dateDiff error

Hello! I've created an expression tag to keep track of the time once an OPC tag stops updating. However, while the OPC tag is continuously updating it returns a non-zero value larger than it should. Once the OPC tag does stop, it then starts adding time to that non-zero value. This leads to the expression tag returning an incorrect time even when the OPC tag is stopped. Is there a better/different way I can do this?

The expression tag is written as:

dateDiff({[.]tag.LastChange},now(),"seconds")

Some clarification is needed:

I've created an expression tag to keep track of the time once an OPC tag stops updating.

Do you mean the expression tag should report the time since the most recent timestamp?

However, while the OPC tag is continuously updating it returns a non-zero value larger than it should.

Give an example. There will be at least a few milliseconds difference. What is the scan class (update rate) on the OPC tag?

Once the OPC tag does stop, it then starts adding time to that non-zero value.

This part isn't clear.

This leads to the expression tag returning an incorrect time even when the OPC tag is stopped. Is there a better/different way I can do this?

Do you mean "returning an incorrect elapsed time"?

dateDiff({[.]tag.LastChange}, now(), "seconds")

Be aware of the now() function parameter.

now(0): executes once at startup or view load.
now(): executes at the default rate - usually 1000 ms.
now(250): executes every 250 ms.

Please be clear what you are trying to achieve. e.g., "The expression tag should report the number of seconds since the most recent OPC tag timestamp."

I've created an expression tag to keep track of the time once an OPC tag stops updating.

The OPC tag reaches a value of 10000, after that it does not continue. I want to keep track of the time since it reached that value.

However, while the OPC tag is continuously updating it returns a non-zero value larger than it should.

While the OPC tag is somewhere between 1-10000, the expression tag returns a value of anywhere between 100-500. It varies, but let's say it's 500. It will remain at 500 until the tag reaches 10000.

Once the OPC tag does stop, it then starts adding time to that non-zero value.

Once the OPC tag does stop (when it reaches 10000). The expression tag will then start adding to that 500. It will return 501....502....503.... updating every second.

This leads to the expression tag returning an incorrect time even when the OPC tag is stopped. Is there a better/different way I can do this?

Yes, the incorrect elapsed time. The expression tag should now be returning 1....2....3.... but is instead adding that 500 onto it.

I'm going to guess there's a clock error between the source of that OPC tag and the gateway.

You know the value it stops at, so it would be easier to reframe this.

Create a Date tag that you write the current date to, from a tag event script on the OPC tag when value == 10000. When it's not 10000 write a null or some other sentinel value.

You can calculate how long since it's been 10000 any time you need from that tag as long as it's not the null/sentinel value.

2 Likes

Thanks! That makes sense. I'm quite new to this though, and have a couple of questions.

Create a Date tag that you write the current date to

I'm not sure what type of tag this would be? As in an expression, memory, etc. I don't see Date tag as a type.

from a tag event script on the OPC tag when value == 10000

Can you give me an idea of what that tag event script would look like? I would like it to write to the date tag every second once the value == 10000 and would also like the elapsed time to be in seconds.