Alarm Scripting - Add alarms to existing tags

I am working on some alarming in perspective. I have a tag provider setup to hold my tags that I would like to create alarms on. I have added my tags to the tag browser via the OPC browse device feature.

I would like to create alarms on specific tag structures. Something along the lines of if the tag names tags this, add this type of alarm.

I started scripting this as I need to do it for lots of these guys. I noticed that the alarm would not create on the tag.

The script I have so far is below. It does not apply the alarm to the tag when I check in the tag browser, I can manually apply the alarm no problem...

def addAlarmToSpecificTag():
    """
    Adds a basic alarm to the tag target tag
    """
    tagName = "[Alarming]Test/ER200_LIT21_TEST/bHAlm"  # The tag we want to update

    try:
        # Get the current tag configuration, including alarms
        tagConfig = system.tag.getConfiguration(tagName, True)

        # Define the alarm name and configuration
        alarmName = "ER200_LIT21 TEST"
        alarms = [
            {
                "name": alarmName,  
                "mode": "AboveValue",  
                "setpointA": 1.0  
            }
        ]

        # Ensure alarms exist in the tag configuration
        if 'alarms' not in tagConfig[0]:
            tagConfig[0]['alarms'] = []

        # Add the alarm to the list of alarms (or replace if needed)
        tagConfig[0]["alarms"].append({
            "name": alarmName,
            "mode": "AboveValue",
            "setpointA": 1.0
        })

        # config tag with the modified alarm settings
        system.tag.configure(tagName, tagConfig, overwrite=True)

        # Check
        updatedTagConfig = system.tag.getConfiguration(tagName, True)
        print("Updated configuration for tag " + tagName + ": " + str(updatedTagConfig))

    except Exception as e:
        print("Error configuring alarm for tag " + tagName + ": " + str(e))


addAlarmToSpecificTag()

I based the structure for this off the system.tag.configure
system.tag.configure

I found another post that discussed writing to the child vs parent tag.

Script for adding tag alarm

I was able to write to a specific tag using the below code.

foo = system.tag.getConfiguration("[Alarming]Test/ER200_LIT21_TEST/bHAlm")[0]

foo['alarms'] = [
    {
    "name": "TEST ALARM",
    "enabled": True,
    "Priority": "Medium",
    "Mode": "Equal",
    "setpointA": 1.0,
    }
]

system.tag.configure("[Alarming]Test/ER200_LIT21_TEST/", foo)

Looks like I may have been over complicating it. I will tinker with the script from here.

the pythonic way to do this is:

alarms = tagConfig[0].setdefault('alarms', [])
alarms.append(...)

However, if your configuration does have an "alarms", then this will append a new alarm which may not be what you want, and simply replacing the alarms kay value with a new list as you do in your second post is the way to go.

Just a caution when using getConfiguration with tags in a UDT instance: this will get all of the overridden prop configuration for the tag(s), including those from the UDT Definition, and writing this back with tag.configure will apply overrides at the instance for those props which you most likely don't want. Using it for standard tags not part of udt instances is fine though

2 Likes