Hello,
I'm trying to simply duplicate a subset of tags. Essentially copy and paste tags into the same folder, appending some chars to the end of the name and tweak the opcItemPath. I have the script mostly working, but I'm hung up on the system.tag.configure line, which is generating a NullPointerException error, even though when I print out the tagconfig in question, everything looks good. Can someone please point out what I'm missing? Code and applicable Tag Config as follows...
Code:
# Parameters
sourceFolderPath = "[Default]1021203109W5XXXX" # Replace with your source folder path
substring = ".r4." # Substring to search for in opcItemPath
replacement = ".r1." # Replacement substring for the new tags
# Initialize a stack to simulate recursion
folders_to_process = [sourceFolderPath]
while folders_to_process:
# Get the next folder to process
current_folder = folders_to_process.pop()
try:
# Browse tags in the current folder
tags = system.tag.browse(current_folder).getResults()
for tag in tags:
# Debug: Print the full path of the tag
print("Found tag/folder:", tag['fullPath'])
if tag['hasChildren']:
# Add subfolders to the stack only if the tag has children
folders_to_process.append(tag['fullPath'])
else:
# Get the tag configuration for leaf tags
try:
# Correct usage: Pass the fullPath as a string
tagConfig = system.tag.getConfiguration(tag['fullPath'], recursive=False)
except Exception as e:
print("Error fetching tag configuration for '{}': {}".format(tag['fullPath'], str(e)))
continue
if tagConfig:
tagConfig = tagConfig[0] # Extract the first (and only) tag config
# Check if it has an OPC item path with the desired substring
if 'opcItemPath' in tagConfig and substring in tagConfig['opcItemPath']:
oldOpcPath = tagConfig['opcItemPath']
newOpcItemPath = oldOpcPath.replace(substring, replacement)
# Modify the tag name for the copied tag
newTagName = tagConfig["name"] + "_r4" # Append "_r4" to the tag name
# Modify the tag configuration for copying
tagConfig["name"] = newTagName
tagConfig["opcItemPath"] = newOpcItemPath
# Remove properties that are not valid for new tags
#tagConfig.pop("tagType", None) # Remove 'tagType' if present
tagConfig.pop("path", None) # Remove 'path' if present
# Determine the destination folder (same as the original folder)
destinationFolder = "/".join(str(tag['fullPath']).split("/")[:-1])
print("Destination folder:", destinationFolder)
# Paste the tag into the original folder
try:
# Debug: Print the tag configuration
print("Tag configuration being copied:", tagConfig)
# Update tag configuration and paste
system.tag.configure(destinationFolder, [tagConfig], collisionPolicy = "a")
print("Successfully copied tag to:", destinationFolder)
except Exception as e:
print("Error copying tag to '{}': {}".format(destinationFolder, str(e)))
except Exception as e:
print("Error browsing folder '{}': {}".format(current_folder, str(e)))
Tag Config:
('Tag configuration being copied:', {u'tagGroup': u'Field', u'historyEnabled': True, u'historicalDeadband': 0.0, u'dataType': Boolean, u'readOnly': True, u'historyProvider': u'Ignition_Historian_Splitter', u'opcItemPath': u'nsu=urn:WellTrak;s=c113.r1.shutin', u'tagType': AtomicTag, u'name': u'SHUTIN_r4', u'opcServer': u'WellTrak OpcUa', u'valueSource': u'opc', u'historyMaxAge': 12, u'value': False})