Expression Tag, runScript, and system.tag.queryTagHistory

Hi,

We’re trying to create a tag, which is just some historical average over some time period. For example, SiteTotal is the current instantaneous value (with appropriate tag history enabled), but SiteTotal_1havg is just the last 1-hour average.

We’re able to write a python script to return the correct value, which basically looks like:

def queryTagHistoryAverageLast1Hourx():
	dataSet = system.tag.queryTagHistory(
			paths=['[default]test/SiteTotal'], 
			startDate=system.date.addHours(system.date.now(), -1),
			endDate=ystem.date.now(), 
			returnSize=1,
			aggregationMode='Average',
			returnFormat='Wide')

	return dataSet.getData()[1][0]

This script appears to work fine in the designer script console:

TagHistory.queryTagHistoryAverageLast1Hourx()

>>>
1534.1507555167063

But using this same function in an expression tag like follows, just gives “Error_ExpressionEval”:

runScript('TagHistory.queryTagHistoryAverageLast1Hourx', 0)

The full json export is:

{
  "valueSource": "expr",
  "expression": "runScript(\u0027TagHistory.queryTagHistoryAverageLast1Hourx\u0027, 0)",
  "dataType": "Int4",
  "name": "SiteTotal_1havg",
  "executionMode": "FixedRate",
  "executionRate": 1000,
  "tagType": "AtomicTag"
}

I can’t see why this wouldn’t work, I can add some debug to my python script to return the tag result and that works fine. Eg:

def queryTagHistoryAverageLast1Hourx():
	return system.tag.readBlocking(['[default]test/SiteTotal'])[0].getValue()

I’m unsure what other configuration may be necessary to fix this (Gateway Settings → Gateway Scripting Project is configured to the current and only project). Version 8.1.5.

How can I get an expression tag to run my script which uses the queryTagHistory?

Thanks

You’ll need to specify the database in the history function if you’re calling this from outside of a project scope, pretty sure that’s your issue

1 Like

Yeah, that’s my thoughts on this too, but I couldn’t see anywhere in the docs on exactly how to do this?

I’ve tried using QualifiedPathUtils to make a qualified path that looks like histprov:sql_ignition:/prov:default:/tag:test/SiteTotal and this works in script console but I still get Error_ExpressionEval. Eg:

def queryTagHistoryAverageLast1Hourx():
	from com.inductiveautomation.ignition.common import QualifiedPathUtils
	qualifiedPath = QualifiedPathUtils.toPathFromHistoricalString("test/SiteTotal", "sql_ignition", "", "default")
	
	dataSet = system.tag.queryTagHistory(
			paths=[qualifiedPath], 
			startDate=system.date.addHours(system.date.now(), -1),
			endDate=system.date.now(), 
			returnSize=1,
			aggregationMode='Average',
			returnFormat='Wide')

	return dataSet.getData()[1][0]

However, swapping the function from queryTagHistory to queryTagCalculations seems to resolve this issue using a standard tag path. Eg:

def queryTagHistoryAverageLast1Hourx():
	data = system.tag.queryTagCalculations(["[default]test/SiteTotal"], ["SimpleAverage"], system.date.addHours(system.date.now(), -1), system.date.now())
	return data.getValueAt(0,1)

Doesn’t error and returns an expected result as an expression tag and script console.

2 Likes