If Condition in Tag Change Script issue

Hi All,

Sorry for a newbie question. I need to modify parameters via reference tags in the UDT using Tag Change script.
It works fine without ‘if’ conditioning, however once ‘if cur.value is True: ’ is enabled in the script ifs not writing to the tag. Of course Im testing in with value changed from 0 to 1. Tried examples from manuals/forum and in general every example with IF failed to write to the tag. Any ideas ?

def valueChanged(tag, tagPath, previousValue, currentValue, initialChange, missedEvents):

pathA = system.tag.read("[.]PLC_Path_A").value
pathB = system.tag.read("[.]PLC_Path_B").value

cur = currentValue
prv = previousValue


if cur.value is True:    
system.tag.writeBlocking("[.]PLC_Path_Ref", pathA )

This should be:

if cur.value:
	system.tag.writeBlocking(["[.]PLC_Path_Ref"], [pathA])

Notes:

  • Don't compare booleans to True or False. Just use them, perhaps with not.

  • Python/Jython requires indentation to delimit blocks of code.

  • .writeBlocking() is designed to handle multiple writes at once, and therefore needs lists of paths and values.

2 Likes

Sorted. Many thanks.

Just wanted to add this, since Phil didn't mention it, but system.tag.read() has been deprecated and you should be using system.tag.readBlocking() which is designed to read multiple paths at once and so accepts a list of tag paths and returns a list of qualified values.

I'm not sure why you're reading pathB in this script since you don't show it being used but if you did need both values it should be read like this.

tagA,tagB = system.tag.readBlocking(['[.]PLC_Path_A','[.]PLC_Path_B'])

#Or
#tagA = system.tag.readBlocking(['[.]PLC_Path_A','[.]PLC_Path_B'])[0].value

if currentValue.value:
     system.tag.writeBlocking(['[.]PLC_Path_Ref'],[tagA.value])

I'm also surprised that @pturmel didn't caution you about running blocking scripts in tag value change scripts. Do a search for the pitfalls, particularly if this UDT will have many instances.

1 Like

Huh. The leading def isn't in the code block and rest is not indented--threw me off. +1

1 Like

The intention of this scripting is to replace OPCItempath in the UDT instances for redundant S7400 comms based on Master/Slave status PLC tag (bool). This tag only change and execute UDT scripts once in year, or even less often. Should I be aware of any Ignition performance issues etc.. ?

Tag Change scripting looks bit neater solution comparing to gateway script.

I wouldn't bother with a script at all then, I would just create a UDT parameter and change it manually if it needed it. Oh well. Either way, I guarantee that it runs more than that. So while it may seem harmless to you, you should definitely be aware of the rope you're walking on. The thread pool available for tag scripts is very small, and if you're not careful you'll have random failures due to chewing up that pool. It will be very difficult to find when you notice it (I have been there, and done that, not fun.)

Unfortunately manual change is not an option i.e. client would not accept it. Needs to be automated somehow.

I think this is a good example where the wrong question was asked. It seems this is more of a redundancy question than anything else.

If that's correct take a look at this recent post, perhaps it will be of some assistance.

1 Like

Too bad I can't help with Siemens....

{ /shudder }

4 Likes