Computing duration using gateway tag value change script

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.

This topic might give you some ideas: