Report Data Source Script Runs in the Script Console but not the Report

The script works in both until I try to add the system.tag.queryTagHistory to the Reports Data Sources Script. I'm guessing this is an issue because the report script runs on the gateway?

If I change the tagHeaders variable to remove the historicalValue and comment out the first two rows under the Read historical values section and enable the third one it works again.

def updateData(data, sample):
    endTime = data["EndDate"]
    startTime = data["StartDate"]
    # Define the folder path where historical logging is enabled
    folderPath = ""

    # Get an array of all tags, then save the data we want for the report
    browseTagArray = system.tag.browseTagsSimple(folderPath, "ASC")
    tagDataset = []
    tagHeaders = ['name', 'fullPath', 'type', 'dataType', 'value', 'historicalValue']
    for browseTag in browseTagArray:
        if not browseTag.isFolder():
            tagValue =
            tagValue = str(tagValue)  # Convert value to string
            # Get the last parent folder from the fullPath
            parentFolders = browseTag.fullPath.split("/")
            lastParentFolder = parentFolders[-2] if len(parentFolders) >= 2 else ""
        # Read historical values
        historicalValues = system.tag.queryTagHistory(paths=[browseTag.fullPath], startDate=StartDate, endDate=EndDate, returnSize=1)
        tagDataset.append([, lastParentFolder, browseTag.type, browseTag.dataType, tagValue, historicalValues.getValueAt(0,1)])
        #tagDataset.append([, lastParentFolder, browseTag.type, browseTag.dataType, tagValue])
    # Put our tag data into the script datasource. In the key browser, this will show up as a datasource
    # named 'MyDataset' with the columns found in the tagHeaders variable above.
    data['MyDataset'] = system.dataset.toDataSet(tagHeaders, tagDataset)

Reporting does indeed run on the gateway. Your folderPath = "" should probably at least contain the tag provider such as "[default]". That way the queryTagHistory knows which provider to search.

As a side note, you should always check if the returned dataset has rows before attempting to get the first row.

I've just been manually adding the folderPath string when testing the script.

So you are saying that the script should work when run on the Script Console but not in the Report Data Source?

When testing on the Script Console I use the print command to see if the dataset has data or rows. Is there something similar that would work when running the script elsewhere? A log or popup?

It's too bad there is not a selection on the Script Console to test the script on the gateway and to have more than one Script Console open at a time.

I did not change the name of the startDate and endDate parameters when moving the script over from the Script Console. :frowning:

historicalValues = system.tag.queryTagHistory(paths=[browseTag.fullPath], startDate=startTime, endDate=endTime, returnSize=1)

Now I just need to figure out how to format the report. :slight_smile:

When I run the script in the Script Console I get a dataset with 8 rows and only tags. When I run the script in the Reports Data Sources Script it adds the tag folders and even gives them a value from the previous row.

Also, if I move the 'folderPath' up a few folders then the "for browseTag in browseTagArray" section stops working saying that the parameters like 'lastParentFolder' are being referenced before assignment. Again, this is not an issue for the same script in the Script Console, I can shorten the 'folderPath' to the root tag folder and it still runs.

com.inductiveautomation.ignition.common.script.JythonExecException: Traceback (most recent call last): File "", line 27, in updateData UnboundLocalError: local variable 'lastParentFolder' referenced before assignment

I had some spaces and tabs in the script as I had copied some code from the manual and it came with a 4 space indent and not a tab indent. The script is running now that al indents are the same.