Associated Data in tags AlarmActive 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