Export Tag Historian to CSV

I am trying to use the script shown on the Ignition user manual for exporting tag history to a CSV, but I am missing something. The version is slightly older being 8.0.1.7, so I am not sure if that is what is making the difference or if there is some other configuration that is happening. On the project on my computer, it works perfectly fine (I am using 8.1.43), but when I try to use it on the customers computer it comes up with the following error:

Traceback (most recent call last):
  File "<input>", line 20, in <module>
  File "<input>", line 7, in browse
TypeError: 'NoneType' object is not iterable

Here is the script that is being ran, I have tried including the gateway in the path but that did not change anything. I am able to see data on trends, so I know there is data there. Any help with this would be greatly appreciated, thank you!

# Our browse function that will browse for historical tags.
# By setting this up as a function, it allows us to recursively call it to dig down through the specified folder.
# Pass in an empty list that we can add historical paths to, and the path to the top level folder.
def browse(t, path):
 
    # Loop through the results of the historical tag browse, and append the path to the empty list.
    for result in system.tag.browseHistoricalTags(path).getResults():
        t.append(result.getPath())
         
        # If the result is a folder, run it through the browse function as well.
        # This will continue until we are as deep as possible.
        if result.hasChildren():
            browse(t, result.getPath())
 
# Start with an empty list to store our historical paths in.
historyPaths = []
 
# Call the browse function, passing in an empty list, and the folder that we want to browse for historical tags.
# This path is a placeholder. It should be replace with your valid path.
browse(historyPaths, path='histprov:Hixton_SQLServer:/drv:default:/tag:GreenhouseIII')
 
# Create another empty list to store our tag paths that we will pull out of the historical paths.
tagPaths = []
 
# Loop through the list of historical tag paths, split out just the tag path part,
# and push it into our tag path list.
for tag in historyPaths:
    tagPaths.append("[default]" + str(tag).split("tag:")[1])
 
# Now that we have a list of tag paths, we need to grab the historical data from them.
# Start by creating a start and end time.
endTime = system.date.now()
startTime = system.date.addDays(endTime, -5)
 
# Then we can make our query to tag history, specifying the various parameters.
# The parameters listed for this function can be altered to fit your need.
data = system.tag.queryTagHistory(paths=tagPaths, startDate=startTime, endDate=endTime, returnSize=10, aggregationMode="Average", returnFormat='Wide')
 
# Turn that history data into a CSV.
csv = system.dataset.toCSV(data)
 
# Export that CSV to a specific file path. The r forces it to use the raw path with backslashes.
system.file.writeFile(r"C:\myExports\myExport.csv", csv)

Blockquote

This would be my guess. That version is extremally outdated (I assume you mean 8.1.7, but if you meant 8.0.7 this is even more true) and missing several bug fixes. You should be recommending an upgrade path to your client.

From what I can tell, there is nothing in the script that would cause the error you're seeing.

All of that said, there are a few things you can do to help your performance. I know you copied the script straight from the manual, but for whatever reason the author of the script didn't take advantage of things which help performance. If you're not looping through a huge number of tags, probably wont be a huge difference, but it will help.

def browse(t, path):

    for result in system.tag.browseHistoricalTags(path).results:
        t.append(result.path)

        if result.hasChildren():
            browse(t, result.path)

historyPaths = []
browse(historyPaths, path='histprov:Hixton_SQLServer:/drv:default:/tag:GreenhouseIII')

tagPaths = ['[default]{}'.format(str(tag).split('tag:')[1]) for tag in historyPaths]
 
endTime = system.date.now()
startTime = system.date.addDays(endTime, -5)
data = system.tag.queryTagHistory(paths=tagPaths, startDate=startTime, endDate=endTime, returnSize=10, aggregationMode="Average", returnFormat='Wide')
csv = system.dataset.toCSV(data)
system.file.writeFile(r"C:\myExports\myExport.csv", csv)

You could try to verify if the path is correct? I think the error is trying to say that you are getting 'None' back when you try to use system.tag.browseHistoricalTags().

Might be worth double checking that history path with something simple from the manual like

# This script will browse for any history tags at the specified historical path and print out all of their Historical Tag Paths to the console.
  
path='histprov:DB:/drv:controller:default:/tag:simulator/turbine 3' #replace this with your paths
browse = system.tag.browseHistoricalTags(path) #We call the function and place the BrowseResults that get returned into a variable called browse.
results = browse.getResults() #We can then call getResults() on the BrowseResults variable, and store that in a variable called results.
for result in results: #We can now loop through the results in a for loop.
    print result.getPath() #We then call .getPath() on the individual objects to get the Tag Path.

just to verify that it's accessing the path correctly.

Turns out that the gateway name is required in the older version, I thought I had tried it with/without, but it worked fine once it was put in. Just stupid mistakes over here. Thank you guys for your suggestions!

1 Like