Skip to main content
Version: 7.9

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.

Python - Grabbing History Data
# 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.

Python - Export History to CSV
# 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.

  1. path='histprov:myDB:/drv:myIgnitionGateway:myTagProvider:/tag:myFolderOfTags'
    1. 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.
      1. histprov:myDB:/ - This is the name of your Historical Tag Provider that you are searching in, simply replace myDB with the name of your historical Tag Provider.
      2. drv:myIgnitionGateway:myTagProvider:/ - The first thing is the name of your Ignition Gateway. Simply replace myIgnitionGateway with the name of your Ignition Gateway, which can be found by going to the Gateway Configure Webpage and navigating to System > Gateway Settings, and it is the System Name. The second thing is the name of the Realtime Tag Provider where the Tags reside. Simply replace myTagProvider with the name of your Tag Provider.
      3. tag:myFolderOfTags - This is the name of the folder you want to search for historical Tags. Simply replace myFolderOfTags with the name of a folder in your Tag Browser to search through.
  2. system.tag.queryTagHistory(...)
    1. 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.
  3. r"C:\myExports\myExport.csv"
    1. 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.
danger

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.

Python - Recursively Browse for History and Export to CSV
# 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)