# Expression Tag, runScript, and system.tag.queryTagHistory

**URL:** <https://forum.inductiveautomation.com/t/expression-tag-runscript-and-system-tag-querytaghistory/49447>\
**Category:** Ignition\
**Tags:** scripting, ignition81\
**Created:** [August 2, 2021, 6:41am UTC](https://forum.inductiveautomation.com/t/expression-tag-runscript-and-system-tag-querytaghistory/49447 "2021-08-02T06:41:01Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![bradley.falzon](https://avatars.discourse-cdn.com/v4/letter/b/e495f1/32.png) [@bradley.falzon](https://forum.inductiveautomation.com/u/bradley.falzon)\
**Post date:** [August 2, 2021, 6:41am UTC](https://forum.inductiveautomation.com/t/expression-tag-runscript-and-system-tag-querytaghistory/49447/1 "2021-08-02T06:41:01Z")

</div>

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:

```auto
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:

```auto
TagHistory.queryTagHistoryAverageLast1Hourx()

>>>
1534.1507555167063

```

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

```auto
runScript('TagHistory.queryTagHistoryAverageLast1Hourx', 0)

```

The full json export is:

```auto
{
  "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:

```auto
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

---

<div class="post-metadata">

**Author:** ![nminchin](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/nminchin/32/6911_2.png) [@nminchin](https://forum.inductiveautomation.com/u/nminchin)\
**Post date:** [August 2, 2021, 12:40pm UTC](https://forum.inductiveautomation.com/t/expression-tag-runscript-and-system-tag-querytaghistory/49447/2 "2021-08-02T12:40:38Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![bradley.falzon](https://avatars.discourse-cdn.com/v4/letter/b/e495f1/32.png) [@bradley.falzon](https://forum.inductiveautomation.com/u/bradley.falzon)\
**Post date:** [August 2, 2021, 11:34pm UTC](https://forum.inductiveautomation.com/t/expression-tag-runscript-and-system-tag-querytaghistory/49447/3 "2021-08-02T23:34:38Z")

</div>

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:

```auto
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:

```auto
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.
