# Computing duration using gateway tag value change script

**URL:** <https://forum.inductiveautomation.com/t/computing-duration-using-gateway-tag-value-change-script/19959>\
**Category:** General Discussion\
**Tags:** scripting\
**Created:** [October 3, 2018, 3:26pm UTC](https://forum.inductiveautomation.com/t/computing-duration-using-gateway-tag-value-change-script/19959 "2018-10-03T15:26:17Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![filipg](https://avatars.discourse-cdn.com/v4/letter/f/a88e4f/32.png) [@filipg](https://forum.inductiveautomation.com/u/filipg)\
**Post date:** [October 3, 2018, 3:26pm UTC](https://forum.inductiveautomation.com/t/computing-duration-using-gateway-tag-value-change-script/19959/1 "2018-10-03T15:26:17Z")

</div>

Am learning gateway scripting, specifically tag value change. I am trying to do a compute a basic “duration”, in my case, how long a particular pump has been running to empty a fixed volume (in a large system).

Can someone post something to get me started?

Here is what I want:

When pump1\_on’s value changes to “1”, I want to grab & save newValue.timestamp to a pump1\_start. Then, when pump1\_on changes to “0”, I want to get old timestamp from pump1\_start and subtract that from “current” newValue.timestamp and save difference in pump1\_duration.

Here’s what I’ve done:

My script compiles but then fails (gateway logs) for a whole multitude of (beginner) things (for last 3 weeks). Latest version uses dateExtract(newValue.timestamp, “ms”) to save the timestamps. Like so:

when pump1\_on changes to 1  
system.tag.write("[default]pump1\_start", dateExtract(newValue.timestamp, “ms”) )

later, when pump1\_on changes back to 0  
started = system.tag.read("[default]pump1\_start")  
system.tag.write("[default]pump1\_duration", system.date.minutesBetween(newValue.timestamp,toDate(started.value)) )

But that just gets me TypeError: minutesBetween(): 2nd arg can’t be coerced to java.util.Date

I checked on-line docs, forums, and university, but didn’t see anything.

Thank you.

P.S. To get at my data, I wrote a perl script to read gateway logs, extract when this particular tagchange script ran/crashed, and from these I computed durations that I was after. Feeling dirty, I broke down and posted question here.

---

<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:** [October 3, 2018, 9:14pm UTC](https://forum.inductiveautomation.com/t/computing-duration-using-gateway-tag-value-change-script/19959/2 "2018-10-03T21:14:24Z")

</div>

This topic might give you some ideas:

> [@Timer Component](http://forum.inductiveautomation.com/t/timer-component/17125):
>
> Is there a way to have the timer count in Hours/Min/seconds?
