System.tag.querytaghistory return error

When using direct tag in the tag history my dataset returns [7 rows, 2 cols] just as I want. See my setup here:

The problem is that my tag have to be dynamic. When i try to use script transform and fetch the data through queryTagHistory I can not get the same result.

endTime = system.date.now()
startTime = system.date.addDays(endTime,-7)
dataSet = system.tag.queryTagHistory(paths=['[default]P176/Passengers/Location_1_Unit_1/passengers'], startDate=startTime, endDate=endTime, returnSize=-1, aggregationMode="Maximum", returnFormat='Wide', intervalHours=1, rangeHours=24)
return dataSet

The startDate and endDate parameters work as it should, but the intervalHours and rangeHours is not responding at all. Is there a bug with this function or am I not understanding this correctly?

This datasets returns [232 rows, 2 cols]

What are you running the script transform on? Or do you mean you are running a scripted query?


Tip: use the </> code formatting button when pasting code. It preserves indentation and applies syntax highlighting.

Great tip :smiley:
Here you can see my script transform:

OK, so you’re using the Tag History binding just to compare the historical binding (7 rows which you then discard) with the results of the script.

The screengrab shows the tag name starts with [M....
The script you posted says paths=['[default]P176/Passengers/Location_1_Unit_1/passengers']

Are you sure you’re looking at the same tag?

Yes I am sure I am looking at the same tag. I just removed the name of my customer for privacy reasons.

You can make a tag history binding use dynamic tags, with the expression. I do remember I had a hard time making it work, but it's possible.

Few comments here:

  • You're passing startDate, endDate AND rangeHours, and with conflicting values. Your startDate is 7 days before endDate, but rangeHours is set to 24. What are you trying to do, or what did you expect those to do ?
  • I'm not sure how returnSize=-1 and intervalHours=x play together.

@pascal.fragnoud Do you have an example of how you did make the tag dynamic with expression?
It might be that I am not understanding this correctly, what I am trying to do is exactly the same as in the first picture. Show data from the last 7 days and group the data together by 1 day intervals.

When you say the tags need to be dynamic, what does that mean?

To replicate what you’re configuration the function call would be something like this:

endTime = system.date.now()
startTime = system.date.addDays(endTime,-7)
dataSet = system.tag.queryTagHistory(paths=['[default]P176/Passengers/Location_1_Unit_1/passengers'], startDate=startTime, endDate=endTime, aggregationMode="Maximum", intervalHours=24)
return dataSet

Though you could also use rangeHours = 168 as opposed to startTime and endTime

There is an example of using dynamic tag paths at the bottom of this page in the manual.

https://docs.inductiveautomation.com/display/DOC81/Tag+History+Bindings+in+Perspective

1 Like

I need to connect the location number and unit number in the tagpath to a parameter['[default]P176/Passengers/Location_{Parameter_1}_Unit_{Parameter_2}/passengers']

@lrose your example is working :smiley: is this the best way to do this? I tried using expression but I cant figure it out.

I think that the binding without the transform is the correct way to do this. You have to set it up first though. I will assume that you already have properties for Parameter_1 and Parameter_2.

  1. Add a custom prop to the view or coponent that this binding will be on. It must be an Array type.
  2. Add an array element of object type for each tag that you want to collect history for. For this example I’m assuming only 1 tag.
  3. Add three values to each array element they must be named ‘aggregate’, `alias’, and ‘path’.
  4. Set the aggregate to ‘maximum’, and the ‘alias’ to whatever you want, I wet with ‘passengers’, if you leave the alias blank the tag path will be used.
  5. Configure an expression binding on the path with the following expression. Note that the path to your parameters is probably different. Use the property selector to get the correct path.
'[default]P176/Passengers/Location_' + {this.custom.Parameter_1} + '_Unit_' + {this.custom.Parameter_2} + '/passengers'

The completed custom property should now look like this:
image

  1. In the expression section of the Tag History Binding select the custom prop that we just created.