Script to change alarm priority

Hi everybody,
I’m working with Ignition 8.1.3 on a list of tag in which everyone has an alarm associated with it. I’m trying to find a way to change the priority of the alarms using a dataset where i change the priorities and then use this values to over write them on the alarms inside the tags. the problem i don’t know how to force the value in the alarm to change.
Here some scripting i tried till now.
Thank you everybody in advance for your time and help

def saveAlarmList():   #---retreive the alarm list in the client tag and update the alarm priority in the tag browser
	
	from com.inductiveautomation.ignition.common.alarming import AlarmPriority
	
	dDataset = system.tag.read("[client]System/Alarms/AlarmPriority/dAlarms").value	#read the dataset to save
	#print dDataSet
	iRows = dDataset.getRowCount()		
	#print iRows
	
	for i in range(iRows):
		sPath = str(dDataset.getValueAt(i,2))  #get the path from the dataset
		print "sPath: %s" %sPath
		sPath = sPath.replace("prov:default:/tag:","[default]")   #change the first part of the path
		print "sPathConv: %s" %sPath
		sPath = sPath.split(":/alm:")[0]   #remove the last part of the path
		print "sPathCut: %s" %sPath
		sBasePath = sPath.split("Alarms/")[0]   #remove children folder
		sBasePath = sBasePath + "Alarms"   #add parent previously removed
		print "sBasePath: %s" %sBasePath
		sNewPriority = str(dDataset.getValueAt(i,1))
		
		currentConfig = system.tag.getConfiguration(sPath)   #get the tag configuration
		print "Config: ", currentConfig
		alarmList = currentConfig[0]['alarms']   #extract the alarms contained in the tag
		print "alarmList: ", alarmList
	
		for alarm in alarmList:
			alarm['name'] = alarmList[0]['name']
			print "alarmName: %s" %alarm['name']
			#sOldPriority = str(alarmList[0]['priority'])
			
			#if sOldPriority != sNewPriority:
			if sNewPriority == "Low":
				alarm['priority'] = AlarmPriority.Low
			if sNewPriority == "Medium":
				 alarm['priority'] = AlarmPriority.Medium
			if sNewPriority == "High":
				alarm['priority'] = AlarmPriority.High
			if sNewPriority == "Critical":
				alarm['priority'] = AlarmPriority.Critical	 				
			#else:
				#pass
			
		currentConfig[0]['alarms'] = alarm
		settings = "m"
		system.tag.configure(sBasePath,currentConfig,settings)

Very late but I was able to figure this out in one way and failed in another.

Based on a tag being High priority the value source will change to opc. This works fine.

I failed on trying to change the alarm priority correctly, but none the other 99% of this code works.

EDIT: I got it to work on alarms only if the alarm was put on the UDT itself and not directly put on the tag.

This uses two function to either change an alarm priority or setpoint value base on what the user does with the view's components. I used random condition logic that can be altered as needed.
Components are:
• two radio buttons(one for each function call) • Numeric field(to set a new setpoint value), three labels(labels the differences between components), and • one text field(To grab teh folder we want to start in)
Here is my full code:

	
	# Setpoint change function
	def changeSetPoint(UDTPath, tagPath, tag, alarm,  newSetpoint = 0):
		"""
		• UDTPath = The UDT instance; used as the path to update the configs for each tag
		• tagPath = The tags full path from folder to indivudal tag
		• tag = The tag that is being iterated through on the Nth loop
		• alarm = The current tags alarm info
		• newSetpoint = Setpoint value: if nothing is set will make it 0
		"""
		# get a list of alarm names so we can access each tag individually
		alarName = str(alarm['name'])
		priority = str(system.tag.readBlocking(alarms + alarName + ".Priority")[0].value)
		tagName = str(tag['name'])
		
		system.perspective.print("The UDTPath is: %s" % (UDTPath))
		try:
			system.perspective.print("The current Set point value is: " + str(alarm['setpointA']))
		except:
			system.perspective.print("This tag has an alarm setpoint to 0 or none at all")

							
		if priority == 'High':
			
			# EXAMPLE ONLY----------------------------------------------
			# do something here based on tag alarm being on high

			
			# before set point change
			system.perspective.print("Tag full TAG path is: " + str(tagPath))
			system.perspective.print("Tag %s with alarm named %s with the OLD setpointA value" % (tagName, alarName))
			try:
				system.perspective.print("Old setpoint value: " + str(alarm['setpointA']))
			except:
				system.perspective.print("This tag has an alarm setpoint to 0 or none at all")
			system.perspective.print("This tags priority is " + str(priority) + "\n")			
			
			
			alarm['setpointA'] = newSetpoint
			

					
			system.tag.configure(UDTPath, tag, 'o') 
			system.perspective.print("BEFORE-------------------------AFTER\n")

			# after setpoint change
			system.perspective.print(str(tagPath))						
			system.perspective.print("Tag %s with alarm named %s with the NEW setpointA value" % (tagName, alarName))
			system.perspective.print("New setpoint value: " + str(alarm['setpointA']))
			system.perspective.print("This tags priority is " + str(priority))
			return system.perspective.print("Process of updating tag %s's SETPOINT is completed.\n" % (tagName))
	
	# Function 2: Change alarm priorty
	def changeAlarmPriority(UDTPath, tagPath, tag, alarm, newPriority):
		"""
		• UDTPath = The UDT instance; used as the path to update the configs for each tag
		• tagPath = The tags full path from folder to indivudal tag
		• tag = The tag that is being iterated through on the Nth loop
		• alarm = The current tags alarm info
		• priority = the priority that we are changing to
		"""
		# get a list of alarm names so we can access each tag individually
		alarName = str(alarm['name'])
		priority = str(system.tag.readBlocking(alarms + alarName + ".Priority")[0].value)
		tagName = str(tag['name'])
		
		# do something here based on tag alarm being on high
		system.perspective.print("The UDTPath is: %s" % (UDTPath))
		system.perspective.print("Tag full TAG path is: %s" %  (tagPath))
		system.perspective.print("Tag %s with alarm named %s with the OLD Alarm priority which is: %s \n" % (tagName, alarName, priority))
	
		# change this property
		alarm['priority'] = newAlarmPriority		
		system.tag.configure(UDTPath, tag, 'o') 
		
		newPriority = str(system.tag.readBlocking(alarms + alarName + ".Priority")[0].value)
		system.perspective.print("BEFORE-------------------------AFTER\n")
		
		system.perspective.print("The UDTPath is: %s" % (UDTPath))
		system.perspective.print("Tag full TAG path is: " + str(tagPath))
		system.perspective.print("Tag %s with alarm named %s with the NEW Alarm priority which is: %s" % (tagName, alarName, newPriority))
		return system.perspective.print("Process of updating tag %s's PRIORITY is completed.\n" % (tagName))
	
	
	# main folder
	MainTagFolder = str(self.getSibling("folderPath").props.text)
	
	# goes through each folder until the type is UDTtype/UdtInstance/AtomicTag/folder based on what we need/put here
	getTagPaths = system.tag.browse(MainTagFolder, filter = {"tagType": "AtomicTag","recursive":True}) # "tagType": "AtomicTag",,UdtType, AtomicTag, UdtInstance, folder

	
	# to update the tag I needed the UDTinstance path for the group of tags that will be looked at. This is how I get that.
	# DO NOT DO THIS TO MANY FOR LOOPS
#	UDTPath = system.tag.browse(MainTagFolder, filter = {"tagType": "UdtInstance", "recursive":True})[0]['fullPath']

	for tags in getTagPaths:
		# grab full path of all folder/UDTs/AtomicTags whwavter we are looking into above
		if str(tags['tagType']) == "AtomicTag":
			tagPath = str(tags['fullPath'])
			UDTPath = tagPath.replace("/"+str(tags['name']), "")
			

		
## another way to grab alarm property info, there seems to be at least two way to get different info. 
## E.G: Alarms and value need this way, but tagType can be used like a key in a dict tag['tagType']
#		# get our alarm path
		alarms = "%s/Alarms/" % (tagPath)

		# get the configs of each tag instance

		configs = system.tag.getConfiguration(tagPath, True)
		
		#If we get a UDT insatnce this part will need to look like this to go further into the dict/json
		# for tag in configs[0]['tags']:
		
		userSelectedChoice = self.getSibling("UserChoice").props.index

		for tag in configs:
			# check for user chaoice and run that function based on that. uses compoent index
			if userSelectedChoice == 1:
				# get our set point value based on compoent input
				setpointValue = int(self.getSibling("SetPointValue").props.value)
				try:
					for alarm in tag['alarms']:
						changeSetPoint(UDTPath, tagPath, tag, alarm,  newSetpoint = setpointValue)	
				except:
					pass
			else:
				try:
					for alarm in tag['alarms']:
						newAlarmPriority = str(self.getSibling("AlarmPriority").props.value)
						changeAlarmPriority(UDTPath, tagPath, tag, alarm,  newAlarmPriority)
				except:
					pass

The main problem I see in @federico.schinetti's code is that he's not creating an instance of the class.
alarm['priority'] = AlarmPriority.Low should probably be this:

@federico.schinetti Oops. I got that one completely wrong. My apologies.

1 Like

Posterity: Those two are equivalent. AlarmPriority is an enum, so the Java language guarantees there will only ever be as many instances as there are members of the class. The fromIntValue function just returns one of the already existing instances.

2 Likes

Thank you @PGriffith! I never delt with Java that much, first time starting to use jython2, and this is helping a lot. I recently ran my code on a few different kind of tag setups, 1 folder, 6 folders, etc. then the UDT Instance itself followed by the tags.
E.g:
• MainFolder/Folder1/UDTinstance
• MainFolder/Folder1/Folder2/Folder3/Folder4/Folder5/Folder6/UDTinstance

That said, if the alarm is set up in the UDT, my code will work and the alarm property will change. Yet, if the alarm was put directly on a tag itself it will not change.
I was curious as to why this is happening? I feel like there is a concept I am missing.