Working with Dataset

Hello,
I need a help on datasets, I have created a Tag type Dataset (tagName = List) and I have series of Boolean tags,
My goal is once any of the Boolean tag is True I want to add that tag path to the Dataset tag and once Boolean tag is False that tag path data from the dataset should we removed.
Any suggestions highly appreciated.

Thanks :slightly_smiling_face:

Hi @nnaik

On each bool tag you need add a event script that will run when the value changes. This script will then add or remove the paths to the dataset tag.

The script will need to:

  • Check the current value of the bool (using “currentValue.value”)
  • read in the dataset from the dataset tag
  • Loop through the dataset and check if the path is or is not already in the dataset
  • Either add or remove the path to the dataset
  • Then finally write the dataset back to the dataset tag

In the below example:

  • The dataset tag is “[default]Global/DatasetTag”
  • For the script to work you will need to manually edit the dataset tag and add a single column of type string to the dataset
  • I am using the function system.tag.readBlocking and writeBlocking, I don’t believe this is available in 7.9 you will need to modify the below to work with the read function and the write function

There are probably much better ways of doing this but this should get you on your way.

	# read current dataset
	dataset = system.tag.readBlocking(["[default]Global/DatasetTag"])[0].value
	
	tagFound = -1
	
	if currentValue.value:
		#Only add the path if its not already in there
		for index in range(dataset.getRowCount()):
			if dataset.getValueAt(index,0) == tagPath:
				tagFound = index
		if tagFound == -1:
			dataset = system.dataset.addRow(dataset, [tagPath])
			system.tag.writeBlocking(["[default]Global/DatasetTag"],[dataset])
			
	else:
		#Only remove the path if it is in there
		for index in range(dataset.getRowCount()):
			if dataset.getValueAt(index,0) == tagPath:
				tagFound = index
		if tagFound > -1:
			dataset = system.dataset.deleteRow(dataset, tagFound)
			system.tag.writeBlocking(["[default]Global/DatasetTag"],[dataset])
1 Like

While you could do this through tag events, I’d not recommend it because you could run into a race condition. Using a gateway timer script to periodically poll all the interesting tags would avoid that.

# Set list of tags to monitor
tagList = [
           '[default]path/to/tag1',
           '[default]path/to/tag2',
           '[default]path/to/tag3',
           '[default]path/to/tag4',
           '[default]path/to/tag5'
          ]

# Set output tag
datasetTag = '[default]path/to/dataset/tag'

# Read the tags. 
qValues = system.tag.readAll(taglist)

# Set dataset framework
header = ['tagPath']
data = []

# Loop though the tag names and qualified values
# zip() let's us iterate through multple things at once.
for tag, qVal in zip(tagList, qValues):
	# If the value of a qualified value is true, append the tag path to data
	if qVal.value:
		data.append([tag])

# Write the results to the dataset tag
system.tag.write(datasetTag, system.dataset.toDataSet(header, data))
3 Likes

@JordanCClark very nice and clean! Also that Zip function is cool, thanks for showing that.

My only gripe is could you have posted this 20 minutes ago?? :grinning_face_with_smiling_eyes:

2 Likes

Thank you @JordanCClark @Chris.Wyllie for your solutions, I will give it a try and get back to you :slight_smile: