Associated Data in tags AlarmActive event

How can I access associated data from my alarm tag ‘Alarm Active’ event script?
Currently I have this, but it’s not working:

Associated Data defined

Script in ‘Alarm Active’ event

and the error

It seems that alarmEvent.get(“associated data name”) is not working in alarm event…?

Hi zxcslo, there appears to be a bug with the alarmEvent.get() function in the context of Tag Event scripts. However there is a workaround. Please copy the unicodeDataToDict() and getActiveData() function into your script and call getActiveData() with alarmEvent as the function parameter. This will return a dictionary object in which you can access your associated data with the [] operator. See below for an example.

	# Converts the unicode string returned by activeData.toString() into a python dictionary
	def unicodeDataToDict(unicode):
		import ast
		
		# Makes sure the unicode conforms to dictonary format by doing various string replacements
		unicode = unicode.replace("{", "{'")
		unicode = unicode.replace("}", "'}")
		unicode = unicode.replace('=', "':'")
		unicode = unicode.replace("," , "','")
		unicode = unicode.replace("' " , "'")
		
		# Converts the unicode string to a dictionary object
		return ast.literal_eval(unicode)
	
	# Returns various alarm properties including associated data as a python dictionary
	def getActiveData(alarmEvent):
		activeData = alarmEvent.getActiveData()
		
		# We can't explicilty access the data inside activeData so perform a workaround here.
		# Calling the print() function on an activeData object will return a unicode string containing
		# various alarm property names mapped to its corresponding values.
		# E.g. {name=myAlarm}
		# We will then format this unicode string so that it conforms with the syntax of a python dictionary
		# and then convert into a dictionary object 
		activeDataDict = unicodeDataToDict(activeData.toString())
		return activeDataDict
	
	
			
	activeDataDict = getActiveData(alarmEvent)
	# You can access alarm properties by doing: activeDataDict['NAME_OF_ASSOCIATED_DATA']
	# Note that all values in the dictionary are in string format so it may be necessary 
	# to do some casting.
	deviceName = activeDataDict['deviceName']
	hostname = activeDataDict['hostname']
	
1 Like

Cool… It’s working. Thank you. :clap: :+1:
Can we expect this bug to be resolved soon?

I have submitted a ticket for this bug, but unfortunately there is currently no estimate for when it will be resolved. I will update this thread when it has been fixed.