Cannot coerce value into type: class java.util.date

I'm very new to ignition and I'm trying to simulate the start and end of a process and logging the data in-between and exporting to a csv.
I have 2 buttons, one to start and one to stop. The start button assigns the current start time to a memory DateTime tag with the following script:

system.tag.writeBlocking(["[IsoTreat]StartTime"],[system.date.now()])

The second button then needs to use this value to find how far back to query to create the dataset, it uses the following code:


EndTime = system.date.now()

DataSet = system.tag.queryTagHistory(paths =[
        "[IsoTreat]Studio5000/AddititveScaled",
        "[IsoTreat]Studio5000/PowderScaled"], 
        startDate = ["[IsoTreat]StartTime"], 
        endDate = EndTime, 
        columnNames = 'Pot 1''Pot 2', 
        returnSize = 0
)

csv = system.dataset.toCSV(DataSet)

filePath = system.file.saveFile("myExport.csv", "csv", 
        "Comma Separated Values")

if filePath:
	system.file.writeFile(filePath, csv)

When I press the button I get the following error:

java.lang.ClassCastException: java.lang.ClassCastException: Cannot coerce value '['[IsoTreat]StartTime']' into type: class java.util.Date

caused by ClassCastException: Cannot coerce value '
    ['[IsoTreat]StartTime']' into type: class java.util.Date

any help would be greatly appreciated. Thanks.

Hi, Kieren and welcome.

Please have a quick read of Wiki - how to post code on this forum and then edit your post using the pencil icon. All your indentation is lost otherwise. Thanks.

Adding some air into your code makes it more readable and shows up a few issues.

EndTime = system.date.now()
DataSet = system.tag.queryTagHistory(paths = [
      "[IsoTreat]Studio5000/AddititveScaled", 
      "[IsoTreat]Studio5000/PowderScaled"
    ], 
    startDate = ["[IsoTreat]StartTime"],       <-- !
    endDate = EndTime, 
    columnNames = 'Pot 1''Pot 2',              <-- !
    returnSize = 0
)

In the line,
startDate = ["[IsoTreat]StartTime"]
you are supplying a string where a datetime is required. You need to retrieve that first as a separate operation.

tagStartDate = system.tag.readBlocking(["[IsoTreat]StartTime"])
DataSet = system.tag.queryTagHistory(paths = [
      "[IsoTreat]Studio5000/AddititveScaled", 
      "[IsoTreat]Studio5000/PowderScaled"
    ], 
    startDate = tagStartDate[0].value,
    endDate = EndTime,
    columnNames = ['Pot 1', 'Pot 2'],
    returnSize = 0
)

Note the fix on the columnNames list.

There may be more!


Edit: Fixed the line startDate = tagStartDate[0].value
The readBlocking function returns a list with a qualified value for each tag requested. The qualified value includes the actual value, the quality and the timestamp. Since we only read one tag it will be returned as tagStartDate[0] and the value can be read using tagStartDate[0].value. Thank you @Ujwal_Wankhede.

3 Likes

Thank you,

I've added in the changes you said, now I'm getting a similar error:

cannot coerce value '[[Mon Jun 03 10:27:56 AEST 2024, Good, Mon Jun 03 10:27:56 AEST 2024 (1717374476887)]]' into type: class java.util.Date

Is there maybe something wrong with the StartTime tag data type?

Please read this tread

You should make following modification to code work
startDate = tagStartDate[0].value

2 Likes

I made that change and it works now. Thanks to both of you