# system.tag.queryTagHistory aggregationMode="Range" with shift tags

**URL:** <https://forum.inductiveautomation.com/t/system-tag-querytaghistory-aggregationmode-range-with-shift-tags/63214>\
**Category:** Ignition\
**Tags:** database, scripting, ignition81, perspective\
**Created:** [August 19, 2022, 5:12pm UTC](https://forum.inductiveautomation.com/t/system-tag-querytaghistory-aggregationmode-range-with-shift-tags/63214 "2022-08-19T17:12:53Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![jay.meinke](https://avatars.discourse-cdn.com/v4/letter/j/ce7236/32.png) [@jay.meinke](https://forum.inductiveautomation.com/u/jay.meinke)\
**Post date:** [August 19, 2022, 5:12pm UTC](https://forum.inductiveautomation.com/t/system-tag-querytaghistory-aggregationmode-range-with-shift-tags/63214/1 "2022-08-19T17:12:54Z")

</div>

I need the “range” between 2 OPC tag values (value difference between Min and Max) in a Numerical Field or a LED for the shifts. Is there a least path of resistance or can someone help me over the hump? I can read the tags with system.tag.readBlocking(paths) in the script console from the example in the manual, but I am stuck on how to pass the values and get the “Range” from the outputs.

The code below works and I like the aggregationMode=“Range”. I don’t want to use the date time picker/input perspective component for the specific start and end times of a shift or system.date.xxx(). I would like to use my current shift tags from my Line Instances from UDT.

```auto
def transform(self, value, quality, timestamp):
	endTime = system.date.now()
	startTime = system.date.addMinutes(endTime, -300)
	dataSet = system.tag.queryTagHistory(paths=['[default]P3L3/L3accumB'], startDate=startTime, endDate=endTime, returnSize=1, aggregationMode="Range", returnFormat='Wide')
	return dataSet.getValueAt(0,1)

```

```auto
'[default]P3L3/L3accumB' is the opc tag with history enabled and recording. 

......but I would like to use these tags as the start and end for time.

'[default]P3L3/Line7Summary/Shift9/ShiftStartString}' tag value = 2022-08-19 5:30
'[default]P3L3/Line7Summary/Shift9/ShiftEndString}' tag value = 2022-08-19 15:30

```

---

<div class="post-metadata">

**Author:** ![JordanCClark](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jordancclark/32/83362_2.png) [@JordanCClark](https://forum.inductiveautomation.com/u/JordanCClark)\
**Post date:** [August 19, 2022, 5:51pm UTC](https://forum.inductiveautomation.com/t/system-tag-querytaghistory-aggregationmode-range-with-shift-tags/63214/2 "2022-08-19T17:51:58Z")

</div>

You will need to read the tags and parse them into Dates.

```python
tagList = ['[default]P3L3/Line7Summary/Shift9/ShiftStartString}', '[default]P3L3/Line7Summary/Shift9/ShiftEndString}']

# Get the tag Values
stringValues = [tag.value for tag in system.tag.readBlocking(tagList)]

# parse to Date
startTime, endTime = [system.date.parse(value, 'yyyy-MM-dd HH:mm') for value in stringValues]

dataSet = system.tag.queryTagHistory(paths=['[default]P3L3/L3accumB'], startDate=startTime, endDate=endTime, returnSize=1, aggregationMode="Range", returnFormat='Wide')

valueOut = dataSet.getValueAt(0,1)

```

---

<div class="post-metadata">

**Author:** ![lrose](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/lrose/32/62724_2.png) [@lrose](https://forum.inductiveautomation.com/u/lrose)\
**Post date:** [August 19, 2022, 5:53pm UTC](https://forum.inductiveautomation.com/t/system-tag-querytaghistory-aggregationmode-range-with-shift-tags/63214/3 "2022-08-19T17:53:42Z")

</div>

There are a few ways to do this, I will show them here in what is IMO best to worse case.

**1.) Use a Tag Histroy Binding.**

- You will need to create two properties on either the view or the same component that is displaying the result. A property for each beginning and end of shift.
- Then bind the properties as appropriate to each of the tags. Since your tags are Strings you will want to use an expression binding. (Best case would be to store the dates as actual dates, but I understand if there is some limitation keeping you from that.)

```auto
toDate({[default]P3L3/Line7Summary/Shift9/ShiftStartString})

```

- Then you will create a Tag History Binding to get the actual value you are looking for.

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/e/e/eeb7c9f66c80afdb18da3081c6966a8196969af9.png)

**2.) Use custom properties in the script as you have it**

- Same as in #1 you will want to create a custom property for each the start date and the end date. With an expression binding.
- Then modify your script to use those properties

```auto
def transform(self,value,quality,timestamp):
    endTime = self.custom.endTime
    startTime = self.custom.startTime
    dataSet = system.tag.queryTagHistory(paths=['[default]P3L3/L3accumB'],startDate=startTime,endDate=endTime,returnSize=1,aggregationMode="Range",returnFormat='Wide')
    return dataSet.getValueAt(0,1)

```

\*\*3.) Read that tags in the script.  
As @JordanCClark has shown.

---

<div class="post-metadata">

**Author:** ![jay.meinke](https://avatars.discourse-cdn.com/v4/letter/j/ce7236/32.png) [@jay.meinke](https://forum.inductiveautomation.com/u/jay.meinke)\
**Post date:** [August 19, 2022, 6:21pm UTC](https://forum.inductiveautomation.com/t/system-tag-querytaghistory-aggregationmode-range-with-shift-tags/63214/4 "2022-08-19T18:21:04Z")

</div>

I am getting a null on a transform or in the script console.

```auto
tagList = ['[default]P3L3/Line7Summary/Shift7/ShiftStartString', '[default]P3L3/Line7Summary/Shift9/ShiftEndString']
	# Get the tag Values
stringValues = [tag.value for tag in system.tag.readBlocking(tagList)]
	# parse to Date
startTime, endTime = [system.date.parse(value, 'yyyy-MM-dd HH:mm') for value in stringValues]
dataSet = system.tag.queryTagHistory(paths=['[default]P3L3/L3accumB'], startDate=startTime, endDate=endTime, returnSize=1, aggregationMode="Range", returnFormat='Wide')
ValueOut = dataSet.getValueAt(0,1)

```

---

<div class="post-metadata">

**Author:** ![JordanCClark](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jordancclark/32/83362_2.png) [@JordanCClark](https://forum.inductiveautomation.com/u/JordanCClark)\
**Post date:** [August 19, 2022, 6:26pm UTC](https://forum.inductiveautomation.com/t/system-tag-querytaghistory-aggregationmode-range-with-shift-tags/63214/5 "2022-08-19T18:26:12Z")

</div>

Put some print statements in there to help you troubleshoot. Make sure the values going in are correct.

How much I can help is going to be limited without access to what you have. 😉

---

<div class="post-metadata">

**Author:** ![jay.meinke](https://avatars.discourse-cdn.com/v4/letter/j/ce7236/32.png) [@jay.meinke](https://forum.inductiveautomation.com/u/jay.meinke)\
**Post date:** [August 19, 2022, 7:58pm UTC](https://forum.inductiveautomation.com/t/system-tag-querytaghistory-aggregationmode-range-with-shift-tags/63214/6 "2022-08-19T19:58:52Z")

</div>

I have not got either suggestion to work yet, but when i figure out my problem, I will update this. I tried a few ways with the custom properties and could not get anything to work yet. TY for the guidance.

![customparameters](https://us1.discourse-cdn.com/inductiveautomation/original/3X/8/d/8d6fd14e79073a2f3f638f1767a575d6e549a650.png)

---

<div class="post-metadata">

**Author:** ![lrose](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/lrose/32/62724_2.png) [@lrose](https://forum.inductiveautomation.com/u/lrose)\
**Post date:** [August 19, 2022, 8:23pm UTC](https://forum.inductiveautomation.com/t/system-tag-querytaghistory-aggregationmode-range-with-shift-tags/63214/7 "2022-08-19T20:23:54Z")

</div>

You have set the value of the custom property to a string representation of an expression. You need to make an expression binding to the property.

[https://docs.inductiveautomation.com/display/DOC81/Expression+Bindings+in+Perspective](https://docs.inductiveautomation.com/display/DOC81/Expression+Bindings+in+Perspective)

---

<div class="post-metadata">

**Author:** ![jay.meinke](https://avatars.discourse-cdn.com/v4/letter/j/ce7236/32.png) [@jay.meinke](https://forum.inductiveautomation.com/u/jay.meinke)\
**Post date:** [August 23, 2022, 2:49pm UTC](https://forum.inductiveautomation.com/t/system-tag-querytaghistory-aggregationmode-range-with-shift-tags/63214/8 "2022-08-23T14:49:57Z")

</div>

when I print the values in the Script Console, I get this:  
u’2022-08-25 5:30’, u’2022-08-25 15:30’

I have a support ticket open with Ignition Support. TY!  
@Irose
