Unable to add scripting to tags

Hello,
I am trying to code where I would like to create tags which has alarm info and custom script(on value changed). I was able to create tags with alarm without any issues but when I want to create tags with alarm and scripting info I'm getting issues. Can anyone please help me?

  1. Post your code as code, via the preformatted text </> button above the post.
  2. Post any actual error messages you're seeing, because "issues" is not nearly enough information to provide meaningful help.
		customscript = """
			currentVal =currentValue.value
			if currentValue.value != previousValue.value:
				eventType = 'Lane 100% Full'
				description = 'EE4401-1 100% FULL'
				if currentVal == 1:
					parameters = {'area':tagPath.split('/')[-2],'site':tagPath.split('/')[1],'eventType':eventType,'startTime':system.date.now(),'device':tagPath.split('/')[-1],'description':description}
					system.db.runNamedQuery('AddNewDowntimeEvent',parameters)
				elif currentVal == 0:
					parameters = {'area':tagPath.split('/')[-2],'site':tagPath.split('/')[1],'eventType':eventType,'endTime':system.date.now(),'device':tagPath.split('/')[-1],'description':description}
					system.db.runNamedQuery('UpdateLatestDowntimeWithEndtime',parameters)
		"""
		alarm = [
		            {
		                "name":alarmName,
		                "displayPath": alarmPath,               
		                "mode":"AboveValue",               
		                "setpointA":0,
		                "inclusiveA":False
		            }
		        ]  
		tag = {
		            "name": tagName,           
		            "opcItemPath" : opcItemPath,
		            "opcServer": opcServer,
		            "valueSource": valueSource,
		            "sampleMode" : sampleMode,
		            "tagGroup" : tagGroup,
		            "alarms": alarm,
		           "eventScripts":  [{"eventGroupName": "valueChanged", "script": customscript}]
		        }
		system.tag.configure(finalTagPath, tag, collisionPolicy)

I'm not getting errors while executing this code, but no tag is getting created.
If comment out eventScripts ,I was able to create tag with alarm info

You've put the script in triple quotes turning the entire thing into one string.

Triple quotes are only needed for e.g. multiline SQL statements.

When I use ' ' or " " for customscript shown in above, I'm getting errors while executing script

What errors?

image
If I use " " for custom script

		customscript = "
			currentVal =currentValue.value
			if currentValue.value != previousValue.value:
				eventType = 'Lane 100% Full'
				description = 'EE4401-1 100% FULL'
				if currentVal == 1:
					parameters = {'area':tagPath.split('/')[-2],'site':tagPath.split('/')[1],'eventType':eventType,'startTime':system.date.now(),'device':tagPath.split('/')[-1],'description':description}
					system.db.runNamedQuery('AddNewDowntimeEvent',parameters)
				elif currentVal == 0:
					parameters = {'area':tagPath.split('/')[-2],'site':tagPath.split('/')[1],'eventType':eventType,'endTime':system.date.now(),'device':tagPath.split('/')[-1],'description':description}
					system.db.runNamedQuery('UpdateLatestDowntimeWithEndtime',parameters)
		"
		
		alarm = [
		            {
		                "name":alarmName,
		                "displayPath": alarmPath,               
		                "mode":"AboveValue",               
		                "setpointA":0,
		                "inclusiveA":False
		            }
		        ]  
		tag = {
		            "name": tagName,           
		            "opcItemPath" : opcItemPath,
		            "opcServer": opcServer,
		            "valueSource": valueSource,
		            "sampleMode" : sampleMode,
		            "tagGroup" : tagGroup,
		            "alarms": alarm,
		           "eventScripts":  [{"eventGroupName": "valueChanged", "script": customscript}]
		        }

		system.tag.configure(finalTagPath, tag, collisionPolicy)

Already told you, you are turning your script into a string.

It should look something like this:

            currentVal =currentValue.value
			if currentValue.value != previousValue.value:
				eventType = 'Lane 100% Full'
				description = 'EE4401-1 100% FULL'
				if currentVal == 1:
					parameters = {'area':tagPath.split('/')[-2],'site':tagPath.split('/')[1],'eventType':eventType,'startTime':system.date.now(),'device':tagPath.split('/')[-1],'description':description}
					system.db.runNamedQuery('AddNewDowntimeEvent',parameters)
				elif currentVal == 0:
					parameters = {'area':tagPath.split('/')[-2],'site':tagPath.split('/')[1],'eventType':eventType,'endTime':system.date.now(),'device':tagPath.split('/')[-1],'description':description}
					system.db.runNamedQuery('UpdateLatestDowntimeWithEndtime',parameters)
		
		
		alarm = [
		            {
		                "name":alarmName,
		                "displayPath": alarmPath,               
		                "mode":"AboveValue",               
		                "setpointA":0,
		                "inclusiveA":False
		            }
		        ]  
		tag = {
		            "name": tagName,           
		            "opcItemPath" : opcItemPath,
		            "opcServer": opcServer,
		            "valueSource": valueSource,
		            "sampleMode" : sampleMode,
		            "tagGroup" : tagGroup,
		            "alarms": alarm,
		           "eventScripts":  [{"eventGroupName": "valueChanged", "script": customscript}]
		        }

		system.tag.configure(finalTagPath, tag, collisionPolicy)

I understand. You are trying to configure the tag with an event script. But the event scripts on a tag are a single element for all scripts on the tag.

You should carefully examine the results from system.tag.getConfiguration() for an existing tag that has the script(s) you want. You will need to produce a matching structure for system.tag.configure().

1 Like

customscript is the script being added to the tag. It will need to be triple quoted.

Looking at one of my test tags with a script of:

x = 1
y = 2

yields:

{
  "valueSource": "memory",
  "eventScripts": [
    {
      "eventid": "valueChanged",
      "script": "\tx \u003d 1\n\ty \u003d 2"
    }
  ],
  "dataType": "Boolean",
  "name": "TestBool",
  "value": false,
  "tagType": "AtomicTag"
}

So, "eventid" instead of "eventGroupName" may be enough to fix it.

3 Likes

Thanks everyone for your responses. Thanks Jordon, changing to "eventid" instead of "eventGroupName" has fixed my issue having triple quotes to my custom script """ """