Perspective working with tags using script

Have a group of tags - tags1...tag24. I would like to find the highest value in that group of tags.
I was using this script on an individual tag to see when the value changed for that tag but I dont think that is the best way. I would have to add this to all 24 tags.

current_y = currentValue
		current_max = system.tag.readBlocking("[.]Y_Max") [0].value
		if current_y >= current_max:
			system.tag.writeBlocking(["[.]Y_Max"], [current_y])

You could combine your tags into a single expression tag:

max({tag1},
  {tag2},
  ....
  {tag24})

Then you can just worry about tracking the highest value of that over time.
You may also want to look into this module:

It's got some powerful tools that could make this a bit more efficient.

2 Likes

For fun, I decided to implement this using the abovementioned module. I split it into two tags for readability but there's nothing stopping you from nesting the expressions into a single monstrosity:

Tag 1, getting the highest tag value of our group (assuming the tags are nicely ordered and 1-indexed):

max(
	forEach(
		tags(
			forEach(24, stringFormat("[default]path/to/tag_%d", idx()+1))
		),
	it()[1]
	)
)

Tag 2, getting the max of Tag 1 over time (see Tag Max Value no SQL - #2 by pturmel)

objectscript("args[0] if state.get('max') is None or args[0]>state.get('max') else state.get('max')\nstate['max']=__retv",
	{[~]Tag_1})[0]

It's best if the part inside the tags() expression is just a property reference. The part outside can be a monstrosity. :grin:

1 Like

So it would be better to have the forEach(24, stringFormat("[default]path/to/tag_%d", idx()+1) in its own custom prop/tag and then reference that in the first expression?

Thank you, the max syntax works very well.