String to Date Conversion Error

I have the following tag change event script :

[color=#0000FF]
[i]if newValue.value == 1:
dateChange = system.db.dateFormat(newValue.timestamp, “yyyy-MM-dd HH:mm:ss”)
intReturn = system.tag.writeToTag (“tsInputB”, dateChange)

    timeElapsed = dateDiff(toDate(tsInputA), toDate(tsInputB), "second")
print "timeElapsed (B-A) =%s" %(timeElapsed )[/i][/color]

tsInputA and tsInputB are dbtag with string type and hold date value.
My goal to get time elapsed between tsInputA and tsInputB.

The problem is the script can’t be compiled.
There is error message :

[color=#FF0000]Parse Error
Traceback (innermost last):
(no code object) at line 0
Syntax Error: (‘inconsistent dedent’,’(’’,5,5,’ timeElapsed = dateDiff(toDate(tsInputA), toDate(tsInputB), “second”)’))
[/color]

Is there anything wrong with my code?
Any suggestion is really appreciated.

Best Regards

I believe that this error:

means that your white spacing is not consistent, i.e. you used a tab on one line and 4 spaces on another or something like that. Try deleting all of your white space and put the same number of spaces in. Also it would help if you wrapped your code in the code /code wrapping so everyone can see the spacing you used.

Yes, you’re right. Thank you
But right now i have new error :

[color=#FF0000]ERROR [TagChangeScriptManager ] [10:11:46,281]: Error executing tag change script: GlassPlant/2nd Input Goods Counter
Traceback (innermost last):
File “<TagChangeScript:GlassPlant/2nd Input Goods Counter>”, line 5, in ?
NameError: dateDiff
at org.python.core.Py.NameError(Py.java)
at org.python.core.PyFrame.getglobal(PyFrame.java)
at org.python.core.PyFrame.getname(PyFrame.java)
at org.python.pycode._pyx35.f$0(<TagChangeScript:GlassPlant/2nd Input Goods Counter>:5)
at org.python.pycode._pyx35.call_function(<TagChangeScript:GlassPlant/2nd Input Goods Counter>)
at org.python.core.PyTableCode.call(PyTableCode.java)
at org.python.core.PyCode.call(PyCode.java)
at org.python.core.Py.runCode(Py.java)
at com.inductiveautomation.ignition.common.script.ScriptManager.runCode(ScriptManager.java:395)
at com.inductiveautomation.ignition.common.script.TagChangeScriptManager$TagChangeScriptHandler$Runner.run(TagChangeScriptManager.java:175)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
at java.lang.Thread.run(Thread.java:619)[/color]

Is there any solution other than using dateDiff function, to compute time elapsed between two date string?

Regards

dateDiff and coercion function like toDate, toInt, toStr, etc. are part of the expression language, and can’t be used in a script.

I haven’t gotten to play with 7.4 yet, but isn’t there a datetime class that can be used now that the Jython version has been upped?

Did this one using the java.util.Calendar module in Ignition 7.2.11 (like I said, haven’t had a chance to play with the newer ones. :slight_smile: )

Using three tags in the Test folder (I mention this so that the code becomes a bit clearer):

[b]Input /b
[b]LastTrue /b
[b]Elapsed /b

[code]from java.util import Calendar

#Create two calendar instances
cal1=Calendar.getInstance()
cal2=Calendar.getInstance()

if newValue.value==1:
#Set cal1 to the tag holding the last time input was true.
cal1.setTime(system.tag.getTagValue(“Test/LastTrue”))

#Set cal2 to the current timestamp
cal2.setTime(newValue.timestamp)

#Find difference in seconds
Elapsed=float(cal2.getTimeInMillis()-cal1.getTimeInMillis())/1000

#Update LastTrue timestamp to current one.
system.tag.writeToTag("Test/LastTrue",cal2.getTime())

#Write elapsed time results to tag (float)
system.tag.writeToTag("Test/Elapsed",Elapsed)

[/code]

I used a momentary button in the attached window to turn Input on and off to test.
Window.vwin (3.96 KB)

Pretty please IA, can you make note of this in the on-line documentation? Don't just say that it's part of expression language; also explicitly say a the top of [toDate - Ignition User Manual 7.9 - Ignition Documentation] (toDate - Ignition User Manual 7.9 - Ignition Documentation) that function is NOT available in gateway scripts.

I wasted several hours on variations of a dead-end.

We typically try to shy away from listing out the things that can’t be done. It is not just that expression function that can’t be used in Gateway Scripts - none of the expression functions can be used in Gateway Scripts. Additionally, it’s not just Gateway Scripts that expression functions can’t be used, but any scripts anywhere. Expressions also can’t be used in any SQL calls, nor can they be used in many other places. Which is why it is typically easier for us to list the places that a function can be used, which in this case is in an expression. I also want to mention that in the future, you can reach the training department at training@inductiveautomation.com for any inquiries into the manual.

5 Likes