# Expression tag - dateDiff error

**URL:** <https://forum.inductiveautomation.com/t/expression-tag-datediff-error/87599>\
**Category:** Ignition\
**Tags:** perspective\
**Created:** [March 19, 2024, 11:11pm UTC](https://forum.inductiveautomation.com/t/expression-tag-datediff-error/87599 "2024-03-19T23:11:38Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![SCADA.Guy](https://avatars.discourse-cdn.com/v4/letter/s/838e76/32.png) [@SCADA.Guy](https://forum.inductiveautomation.com/u/SCADA.Guy)\
**Post date:** [March 19, 2024, 11:11pm UTC](https://forum.inductiveautomation.com/t/expression-tag-datediff-error/87599/1 "2024-03-19T23:11:38Z")

</div>

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")

---

<div class="post-metadata">

**Author:** ![Transistor](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/transistor/32/3380_2.png) [@Transistor](https://forum.inductiveautomation.com/u/Transistor)\
**Post date:** [March 19, 2024, 11:22pm UTC](https://forum.inductiveautomation.com/t/expression-tag-datediff-error/87599/2 "2024-03-19T23:22:09Z")

</div>

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."

---

<div class="post-metadata">

**Author:** ![SCADA.Guy](https://avatars.discourse-cdn.com/v4/letter/s/838e76/32.png) [@SCADA.Guy](https://forum.inductiveautomation.com/u/SCADA.Guy)\
**Post date:** [March 19, 2024, 11:43pm UTC](https://forum.inductiveautomation.com/t/expression-tag-datediff-error/87599/3 "2024-03-19T23:43:19Z")

</div>

> 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.

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [March 19, 2024, 11:48pm UTC](https://forum.inductiveautomation.com/t/expression-tag-datediff-error/87599/4 "2024-03-19T23:48:10Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Kevin.Herron](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/kevin.herron/32/7259_2.png) [@Kevin.Herron](https://forum.inductiveautomation.com/u/Kevin.Herron)\
**Post date:** [March 19, 2024, 11:51pm UTC](https://forum.inductiveautomation.com/t/expression-tag-datediff-error/87599/5 "2024-03-19T23:51:58Z")

</div>

> [@SCADA.Guy](#):
>
> 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.

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.

---

<div class="post-metadata">

**Author:** ![SCADA.Guy](https://avatars.discourse-cdn.com/v4/letter/s/838e76/32.png) [@SCADA.Guy](https://forum.inductiveautomation.com/u/SCADA.Guy)\
**Post date:** [March 20, 2024, 12:13am UTC](https://forum.inductiveautomation.com/t/expression-tag-datediff-error/87599/6 "2024-03-20T00:13:39Z")

</div>

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.
