Export Tag Historian to CSV
Obtaining Historian Data​
Sometimes, it may be useful to export the values from the Tag Historian. There are many ways that this can be done, but the easiest way is to use the built in system.tag.queryTagHistory function. This allows you to specify a list of Tag paths to query history for.
# First we start by creating a start and end time that we will be query history for.
endTime = system.date.now()
startTime = system.date.addMinutes(endTime, -30)
# Next we call our queryTagHistory function, using the start and end dates as well as a Tag path.
# The other parameters listed for this function can be altered to fit your need.
data = system.tag.queryTagHistory(paths=["[default]myTag"],
startDate=startTime, endDate=endTime, returnSize=10, aggregationMode="Average", returnFormat='Wide')
This simple script will query Tag History for a Tag called myTag. It will query the last 30 mins of data, return 10 records, each record will be an average of the time slice, and the return format will be wide. For more information on the function parameters and the different options available, see the queryTagHistory function page. The data returned is then being stored in a variable data, and we can use it later in the script in anyway we want.
Exporting to CSV​
Now that we have our data, it is really easy to export it to a CSV file. Ignition has a built-in function to convert a dataset to CSV data called system.dataset.toCSV. This CSV data can then be written to an actual file using system.file.writeFile.
# First we start by creating a start and end time that we will be query history for.
endTime = system.date.now()
startTime = system.date.addMinutes(endTime, -30)
# Next we call our queryTagHistory function, using the start and end dates as well as a Tag path.
# The other parameters listed for this function can be altered to fit your need.
data = system.tag.queryTagHistory(paths=["[default]myTag"],
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 and not require double backslashes.
system.file.writeFile(r"C:\myExports\myExport.csv", csv)
History Tag Search and Export​
If you have a lot of Tags storing history, it can be difficult to list them all out in the function above. This example makes things easier by specifying a folder of Tags instead of individual Tags. The script first grabs all of the historical Tags from a particular folder and gets the Tag paths using the system.tag.browseHistoricalTags function, runs those through the system.tag.queryTagHistory function, and then exports the results to a CSV file. This script is great, because it can be run from the scripting console, or from a button on the client. However, everything in the script is hardcoded to work with a specific system, though that can be easily changed to fit any system. Below are the list of parts that can be changed with what each of them is used for.
Part of Script | Description |
---|---|
| The path to the parent folder that we are searching through for history Tags. Each part of the string corresponds to a different part of your system.
|
| This is what will actually query for historical data. While the path parameter should be kept the same, the other parameters can be altered to fit your needs. Currently, the function is querying the last 30 mins of history, has a return size of 10, an aggregation mode of Average, and a return format of wide. Of course, all of this can be changed. See system.tag.queryTagHistory for more information on the parameters that can be used here. |
| This is the path that the CSV file will save at. This can be changed to be any location you want. You can even use system.file.saveFile to first ask the user for a save location and filename, and then passing the path returned from that function into the writeFile function. The r at the beginning of the string is important as it allows us to not have to use double backslashes. |
This script can be dangerous! It recursively looks through a folder to find all historical Tags, and then uses all of those Tags to look up Tag history. Looking through a folder that has too many history Tags, or querying history for too large a period of time can potentially result in locking up your system. Use caution when using this function, and never search through too large a set of Tags or query too much history.
# 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:myDB:/drv:myIgnitionGateway:myTagProvider:/tag:myFolderOfTags')
# 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("[myTagProvider]" + 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.addMinutes(endTime, -30)
# 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)