Updating OPC Item path with a script need help!

Super new to Ignition I have a few scripts that where gifted to me by someone else that I have slight modified before.

But I need a script to change the OPC Item path on 2400 tags. Almost in go live and the guys doing Kepware side found out they forgot to split up all the PLCs to singles so every Kepware address needs updated. Is there an easy way to change this with a script like a find this replace with this that i can use for the OPC item path?

Thanks for any help
Jim

Yes.

Start with system.tag.query() to get a list of all the tags that need changing. Note that it is the script equivalent of the tag report tool, so you can play in the GUI until you get all of the right search criteria.

Then you iterate through the returned list of tag dictionaries, building a list of writes to each tag's .opcItemPath property with the corrected value. Using system.tag.writeBlocking.

Thanks for the reply it probably means something to you but as I said I don't do scripting. Using that start you gave me all I can get it to do is Execute and show as an answer or if i put nothing in it shows a few suggestions of folders to search for. I'll see if i can find another way to do this without scripting.

As noted in the docs for system.tag.query(), this is really the same stuff as the tag report GUI tool. Which you really should be using to produce a list of tags that need to be changed. The tag report tool includes an option to "copy as script" that writes a script stub for you.

You asked for how to write a script. :man_shrugging:

Don't be scared off by having to follow some documentation.

3 Likes

I dont even know what the tag report tool is I'll just do it the way i know my documentation doesnt work in ignition because they have the internet blocked and I dont have it on my main machine cause we cant just load anyhting. Thanks for the helps.

Depending on what needs changed, an easier approach might be to export the tags and edit externally with an editor like notepad ++, then import back in.

2 Likes

Yeah thats what I'm doing for the big ones others are singles so i just change them not worth the effort. Was just hoping someone had an example script for changing OPC item path can't be the first time its happened on a project. I only had 2 days to complete it so i don't have time to learn how to write a brand new script when i've written zero scripts in python or ignition haha

Definitely not the first time someone had to edit tags via script. The issue is every project is different, so someone's script might not do you any good.

How would a script do you any good? The script would need to know what you have and what it needs changed to. If you already have that defined you can just as easily do it externally.

1 Like

So here is a script i use to copy the a source and paste it to destination and then it will iterate through the copied folder/tag and update the OPC item path.

I use the readBlocking function instead of the query in unison with the browseTags function.

you could easily modify this to script to not do the copy and rename and only do the update to the OPCItemPath

import system

# Define the tags to be copied, the destination path, and the new tag name
tags = ["[default]MQTT Tags/alignIAD138/DH240/PDU2-40B-2-1_RPP2-40B-2-1"]
destination = "[default]TempMove"
new_tag_name = "PDU2-40A-1-1_RPP2-40A-1-1"  # Define your desired new tag name here
new_device_name = "DH240_PDU2-40A-1-1_RPP2-40A-1-1"  #Define your desired new device name

try:
    # Copy the tags to the destination path
    copied_tags = system.tag.copy(tags, destination)
    
    # Extract the copied tag name from the original tag path
    copied_tag_name = tags[0].split("/")[-1]
    
    # Construct the new tag path with the new tag name
    new_tag_path = destination + "/" + new_tag_name
    
    # Rename the copied tag to the new path in the destination folder
    system.tag.rename(destination + "/" + copied_tag_name, new_tag_name)
    
    # Get a list of tags in the new_tag_path folder
    tags_in_folder = system.tag.browseTags(parentPath=new_tag_path)
    
    # Iterate through the tags in the folder and update their OPCItemPath properties
    for tag in tags_in_folder:
        # Read the current OPCItemPath property for the tag
        tag_path = tag.path + ".OPCItemPath"
        current_opc_item_path = system.tag.readBlocking([tag_path])[0].value
        
        # Check if current_opc_item_path is not None
        if current_opc_item_path is not None:
            # Modify the current_opc_item_path as needed
            new_opc_item_path = "[" + new_device_name + "]" + current_opc_item_path.split("]")[1]
            
            # Update the OPCItemPath property with the new value
            system.tag.writeBlocking([tag_path], [new_opc_item_path])
            
            # Print both the current and updated OPCItemPath to the console
            print("Tag: {0}".format(tag.name))
            print("Current OPCItemPath: {0}".format(current_opc_item_path))
            print("Updated OPCItemPath: {0}".format(new_opc_item_path))
    
    print("Tags copied, renamed, and OPCItemPath properties updated successfully in '{0}'.".format(destination))
except Exception as e:
    print("An error occurred: {0}".format(str(e)))