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

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.

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)
'[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

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

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)

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.)
toDate({[default]P3L3/Line7Summary/Shift9/ShiftStartString})
  • Then you will create a Tag History Binding to get the actual value you are looking for.

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

1 Like

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

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)

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. :wink:

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

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

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