system.tag.queryTagHistory used from in a gateway event sometimes returns empty

Hi all,

I have a script that I wrote for a customer that runs under a gateway event. It's a scheduled to occur at 6 am every day. This script uses system.tag.queryTagHistory to get the the data for two tags. It does an average with an interval of 5 minutes for the rows. The start and end times are calculated to start at the first day of the month at midnight to the day before the report is sent at 11:59:59 PM. I am using the native ignition functions to calculate the dates.

On top of this I am using interpolation, because the client wants to see values at every 5 minutes. Since the data is logged by change, we don't always have a record for every 5 minutes. Hence the noInterpolation parameter is set to False.

I do another queryTagHistory call to get overall maximum value for the time range because the client wants that value. The two tags in question are counters and are logging kWh so they never go down. My start time for this query is 11:58 PM and my end time is 11:59 PM which insures that I get the max value of these two tags at the end of the range.

After that, I append the two query results together to create one dataset and then convert it to a csv using system.dataset.toCSV. I use the localization parameter on the toCSV function so that it will return all of the data as strings. My client doesn't like it when the data is returned with scientific notation and this seems to have fixed that problem. Once the CSV is generated I use the StringUtil library from the org.python.core.util package to convert the CSV into bytes and then attach it to an email which is sent using system.net.sendEmail.

My problem is that intermittently this report will return nothing but timestamps for the first dataset. The second dataset that returns the max value always returns values. If I run the report from the script console everything works every time. However, when run from the gateway event the data returns empty for my 6 AM emails. If I change the report to run at the next minute so that I can test it, it will return data just fine 90% of the time. The CSV is created just fine and the email is sent with no issues, but the data inside of the CSV when opened is just time stamps for the first dataset.

Not sure what is happening here. I have attached the code below. Any help is appreciated!

from org.python.core.util import StringUtil
	
AVERAGE_MODE = 'Average'
BODY = 'Attached is the Daily Report'
FROM_ADDR = 'noreply@email.com'
INTERVAL_MINUTES = 5
MAX_MODE = 'Maximum'
SMTP_PROFILE = 'EmailProfile'
SUBJECT = 'DAILY_REPORT'
TAG1 = '[HistorianProvider/default:default]HISTORIAN/Substation Tags/MFMT1_AI_Tot_kWH_Del'
TAG2 = '[HistorianProvider/default:default]HISTORIAN/Substation Tags/MFMT2_AI_Tot_kWH_Del'
	
ATTACHMENT_NAMES = ['Daily_Report.csv']
RECIPIENTS = [
	'email1@email.com',
	'email2@email.com'
]
	
TAG_PATHS = [TAG1, TAG2]
	
now = system.date.now()
today = system.date.setTime(now, 0, 0, 0)
end_time = system.date.addSeconds(today, -1)
max_start_time = system.date.addSeconds(today, -2)
days_to_remove = ((system.date.getDayOfMonth(end_time) - 1) * -1)
start_time = system.date.setTime(
	system.date.addDays(end_time, days_to_remove), 
	0, 
	0, 
	0
)
	
results = system.tag.queryTagHistory(
	paths=TAG_PATHS,
	startDate=start_time,
	endDate=end_time,
	intervalMinutes=INTERVAL_MINUTES,
	aggregationMode=AVERAGE_MODE,
	noInterpolation=False
)
	
five_min_averages = system.dataset.toPyDataSet(results)
	
results = system.tag.queryTagHistory(
	paths=TAG_PATHS,
	startDate=max_start_time,
	endDate=end_time,
	returnSize=1,
	aggregationMode=MAX_MODE,
	noInterpolation=False
)
	
totals_at_report = system.dataset.toPyDataSet(results)
totals_row = totals_at_report[0]
	
new_row = [totals_row[0], totals_row[1], totals_row[2]]
	
report_dataset = system.dataset.addRow(
	five_min_averages, 
	five_min_averages.getRowCount(), 
	new_row
)
	
report_csv = system.dataset.toCSV(
	report_dataset,
	True,
	False,
	True
)
	
attachments = [StringUtil.toBytes(report_csv)]
	
system.net.sendEmail(
	fromAddr=FROM_ADDR,
	subject=SUBJECT, 
	body=BODY, 
	to=RECIPIENTS, 
	smtpProfile=SMTP_PROFILE, 
	attachmentNames=ATTACHMENT_NAMES, 
	attachmentData=attachments
)