Code does not work Automatically, but works Manually

The code below is on a tagChange script. When the it runs on tag change, it adds all values to the table except for lineID. (lineID and opID are both uniqueidentifiers; opID is added with no problems). However, if I change the value of the tag manually, the every value is added to the table as it should be. Why would lineID not be added to the table automatically, but it will get added on manual tag change.

When I refer to automatic and manual, automatic means I am getting the value from the PLC, whereas in manual, I am overwriting the PLC path with just an manual integer input.

tags = system.tag.readBlocking(["[.]zMyOpID","[.]Scrap_Description", “[.]Scrap_Status”, “[.]zLineName”])
opID = tags[0].value
description = tags[1].value
timestamp = system.date.now()
code = tags[2].value
lineName = tags[3].value
tags2 = system.tag.readBlocking([lineName + “/” + lineName + “/History/SavedLocalLineID”])
lineID = tags2[0].value
params = {“codeValue”:code, “descriptionValue”: description, “op_idValue”: opID, “timestampValue”:timestamp, “lineID”: lineID}
if initialChange == 0 and currentValue.value != previousValue.value and previousValue.value != None:
system.db.runNamedQuery(“ScrapInsert”, params)

I’m not sure of the problem here, but I cleaned up your script a bit. Notice that you had curly quotes instead of straight quotes in some places (may have been copy/paste that did this). Plus, you need the project name for runNamedQuery() as tagChange scripts execute in gateway scope. Did you mean to use lineName twice in the second tag read?

if not initialChange and currentValue.value != previousValue.value and previousValue.value != None:
	tags = system.tag.readBlocking(["[.]zMyOpID","[.]Scrap_Description", "[.]Scrap_Status", "[.]zLineName"])
	opID = tags[0].value
	description = tags[1].value
	code = tags[2].value
	lineName = tags[3].value
	
	timestamp = system.date.now()
	tagPath = "%s/%s/History/SavedLocalLineID" % (lineName,lineName)
	tags2 = system.tag.readBlocking([tagPath])
	lineID = tags2[0].value
	
	params = {"codeValue":code, "descriptionValue": description, "op_idValue": opID, "timestampValue":timestamp, "lineID": lineID}
	system.db.runNamedQuery("<InsertProjectNameHere>","ScrapInsert", params)

Thanks for the response, but that does not work. The script runs, but it will only add the code, description, opID and timestamp to the table, but does not include the lineID. However, if I change the value of the tag manually, everything gets added to the table, as it should.

Why are you using readblocking to just read the lineID value?
Do you find the id if you use
lineID = system.tag.read(tagPath).value

Also i think you are constructing the “tagPath” wrong. You should check if its actually the right path that it is making

Edit:
oh the tagPath wasnt your code, but still in your code it seems wrong aswell

Add your tag provider to the tag path also.

'[default]tagPath'

Read and readBlocking do exactly the same thing. read has been deprecated in Ignition 8, so best to use readBlocking.

@awarunek187

As @dkhayes117 has already pointed out, since this script is running in gateway scope you must provide the project name to system.db.runNamedQuery(), and your tag path must include the provider.

Since this is a Tag Change script it doesn’t make any since to filter for

currentValue.value != previousValue.value

That is by definition true.

I also don’t understand filtering for

previousValue.value == None

Wouldn’t you want to insure the current value wasn’t empty?

That being said, you aren’t actually using either the current value or the previous value in the script so why does it matter?

Try this:

if not initialChange:
	tags = system.tag.readBlocking(["[.]zMyOpID","[.]Scrap_Description", "[.]Scrap_Status", "[.]zLineName"])
	tagPath = "[<InsertProviderName>]%s/%s/History/SavedLocalLineID" % (lineName,lineName)
	tags2 = system.tag.readBlocking([tagPath])
	
	params = {"codeValue":tags[2].value, "descriptionValue": tags[1].value, "op_idValue": tags[0].value, "timestampValue":system.date.now(), "lineID": tags2[0].value}
	system.db.runNamedQuery("<InsertProjectNameHere>","ScrapInsert", params)

I thought the same thing, but I wasn’t sure about some weird corner case they were looking at.

Thank you all for the advice, however, it works just fine when I manually change the tag value. When I allow the script to run with a PLC tag change, everything is added to my table except for the lineID (uniqueidentifier). My question is, why does the script work when I manually change the tag (override the OPC tag), but the script does not add lineID (but adds everything else) when I allow the script to run on OPC tag value change?

If that is happening, I would say it’s time to get support to look over your shoulder.

A tag change is a tag change regardless of how it is triggered.

The only explanation I can think of is that when the PLC triggers the change something is also changing the LineID so it is Null. Especially if there are no errors.

1 Like

Ya the problem with that is, is that the LineID is a reference tag and it never changes value and it is never null. I will try a logger on it and see if a null value is returned and I will update the thread.