I am very new to ignition. Trying to figure out how can I find 3 highest value inside the tags I list and write the tag name to a display with its value to another display. To be more clear;
In my project there are many scrap causes, all of them have a tag value. I am trying to find top 3 and according to top 3 I want to write highest value to another tag, second to another and so on. Also same way I want to write highest value tag name to label and second to another and so on. This way I want to show top 3 scrap causes and their respective values.
Hope I could explain. No idea how can I do it have been searching system.dataset.sort function but it would be very nice if I can get an example script to work with.
I want to always show top 3, not triggered by button. I have each causes and their assigned tags. There are around 20 tags each, different scrap cause. The tag values can change according to production input. I dont have a dataset, it is just the closest thing I have found in my search, maybe I thought I could use it in some way. Solution does not have to be dataset sort.
Sorry, still not clear if the list of 20 tag paths is fixed or dynamic? (is it always those 20 tags that you want to read the values of?)
The most efficient way would be to create a custom property on your View or a component in the view, one for each property. If your tags are all in a single folder, then you could also create a single property and bind it to the tag folder. This will get you a dictionary of all the tags and their values.
Based on @berkay.cinar’s post, it’s sounding like these are 20 static tags that are affected by user input.
Personally, I’d script it to store the results in a dataset tag, as either a gateway timer script of gateway tag change script. All the heavy lifting would stay local to the gateway, and you would only have to use lookup expressions (or similar) on the dataset tag.
Perhaps bind a custom property of the view to the dataset to have just one tag read, and point the labels to it.
tagList = ['[default]test/Scrap Cause 1',
'[default]test/Scrap Cause 2',
'[default]test/Scrap Cause 3',
'[default]test/Scrap Cause 4',
'[default]test/Scrap Cause 5'
]
from operator import itemgetter
headers = ['cause', 'qty']
data = [[tagName, qualified.value] for tagName, qualified in zip(tagList, system.tag.readBlocking(tagList))]
dataset = system.dataset.toDataSet(headers, sorted(data, key=itemgetter(1), reverse=True))
Output:
row | cause | qty
---------------------------------------
0 | [default]test/Scrap Cause 5 | 400
1 | [default]test/Scrap Cause 1 | 250
2 | [default]test/Scrap Cause 2 | 200
3 | [default]test/Scrap Cause 3 | 100
4 | [default]test/Scrap Cause 4 | 50
Thank you for your code when I run the code below in script console I dont get anything no error no result what could I be doing wrong? I copied your code exactly just change the tag paths to mine.
Oh you are right; print data gives me the values in the order it is written not in descending. print dataset on the other hand gives me Dataset [4Rx2C] I dont see the dataset in the output. So I guess dataset is created but I can not see if it is correct order. How can I write 1Rx1C to a another tag? If I can write the dataset values and strings to anytag I want that I would be achieving my goal and will also be able to see if the dataset is correct.
You can get the 'pretty print' of a dataset by using the function found here.
For actual use, write the entire dataset to a tag. Read that tag is into a custom property of the view. Use expressions on your display fields to get the value of a row/column in the custom property.
That way, you have a single tag read from the client.
Hello again Jordan, so everything is ok right now I have been able to write the dataset to a tag and read the column and row I want. Only thing is cause column is consist of tag path right now, which has folder names and so on. Is it possible to bring the tag names to the dataset instead of tag paths?