I have a request to export some historical data (21 tags) over a 2 month period in 1 hour increments. What’s the easiest way to export this data?
Make a script that uses system.tag.queryTagHistory
to pull the data for all the tags that you need. Then, convert the returned dataset to a csv using system.dataset.exportCSV
.
For best results, create a project library script, and define the tags to read, as well as all the logic to fetch/convert and export in a function in this library, and call that function from whereever you need (like a button or gateway scheduled event) to export the data.
Thank you for the help. Seems like a lot of work for a one-time export. Do you think it be easier on sql server?
It will be much more difficult. The historian schema is not simple.
Consider dropping this Exchange project into your gateway:
If you have designer access, this would be easy to do in the script console for a one time thing.
tagList = [‘path/to/tag1’,‘path/to/tag2’,‘path/to/tag3’,‘path/to/tag4’]
sDate = system.date.getDate(2025,1,1)
eDate = system.date.getDate(2025,3,1)
retSize = system.date.hoursBetween(sDate,eDate)
dataSet = system.tag.queryTagHistory(paths=tagList, startDate=sDate, endDate=eDate, returnSize=retSize, returnFormat='Wide',includeBoundingValues=0,validateSCExec=0,timeout=60000)
fileStartDate = system.date.format(system.date.parse(sDate, 'yyyy-MM-dd HH:mm'),'yyyyMMddHHmm')
fileEndDate = system.date.format(system.date.parse(eDate, 'yyyy-MM-dd HH:mm'),'yyyyMMddHHmm')
#Adjust file path if needed.
newFilePath = 'C:\\TEMP\\_DataExport_' + fileStartDate + '-' + fileEndDate + '.csv'
data = system.dataset.formatDates(endDS, 'yyyy/MM/dd HH:mm')
csv = system.dataset.toCSV(dataset=data, showHeaders=True)
system.file.writeFile(newFilePath, csv)
Adjust the variables above to match what you need of course, but this should get you started.