Trouble with IF Statement

Having trouble getting this bit to work properly. This is on a button components “actionPerformed” event.

Parse error for event handler “actionPerformed” SyntaxError: (“no viable alternative at input ‘{’”, (’’, 4, 25, “\tsystem.tag.writeBlocking{[’[default]Upstream/AL_POUR/Forms/Chemistry/trgChemTestComplete.value’], [value]},\n”))

Need to write to a specific tags depending on if a different components state is true or not.

Version 8.1.5

‘’’
value = 1

if(event.source.parent.getComponent(‘ChemOkay’).state,
(system.tag.writeBlocking[’[default]Upstream/AL_POUR/Forms/Chemistry/trgChemTestComplete.value’], [value]),
(system.tag.writeBlocking[’[default]Upstream/AL_POUR/Forms/Chemistry/trgChemTestLog.value’], [value])}
‘’’

Your syntax for a python if statement is a little off, as well as a few other minor syntax issues.
Your calls of the system.tag.writeBlocking function are going to cause another error because you are missing the opening parenthesis of the function call.
Try something like this:

value = 1
if event.source.parent.getComponent('ChemOkay').state:
	system.tag.writeBlocking(['[default]Upstream/AL_POUR/Forms/Chemistry/trgChemTestComplete.value', '[default]Upstream/AL_POUR/Forms/Chemistry/trgChemTestLog.value'], [value, value])

You can consolidate the two writeBlocking function calls into one because it takes in a list of tag paths, and a list of values to write to.
Also as a side note. It looks like you are using single quotes (ascii value 146) rather than apostrophes (ascii value 39) to write string literals in your code. You can either use the apostrophe or the double quote to surround your strings in python code.

This did fix the errors but when executing it is writing to both tags, I need it to write to one if ‘ChemOkay’ is true and the other if it is false.

value = 1
if event.source.parent.getComponent('ChemOkay').state:
	system.tag.writeBlocking(['[default]Upstream/AL_POUR/Forms/Chemistry/trgChemTestComplete'], [value])
else:
	system.tag.writeBlocking(['[default]Upstream/AL_POUR/Forms/Chemistry/trgChemTestLog.value'], [value])

This did the trick, thank you!!

1 Like

One more question, trying to make this dynamic for an extra tag write but it is not working. Getting an illegal character fault. Thoughts on what is wrong?

value = 1
potn = event.source.parent.parent.PotNumber
if event.source.parent.getComponent(‘ChemOkay’).state:
system.tag.writeBlocking([’[default]Upstream/AL_POUR/Forms/Chemistry/trgChemTestComplete’], [value])
else:
system.tag.writeBlocking([’[default]Upstream/AL_POUR/Forms/Chemistry/trgChemTestLog.value’, ‘[default]Upstream/AL_POUR/Tipper_Ladle/Chem_Complete/Pot{potn}.value’], [value],[value])

You're trying to pass the variable 'potn' to the tag address string here?
I belive that the proper way to do this in this version of python is something like this:

value = 1
potn = event.source.parent.parent.PotNumber
if event.source.parent.getComponent('ChemOkay').state:
	system.tag.writeBlocking(['[default]Upstream/AL_POUR/Forms/Chemistry/trgChemTestComplete'], [value])
else:
	system.tag.writeBlocking(['[default]Upstream/AL_POUR/Forms/Chemistry/trgChemTestLog.value', '[default]Upstream/AL_POUR/Tipper_Ladle/Chem_Complete/Pot%s.value'%(potn)], [value],[value])

Change the %s part to %d if your variable is an int.
Also, the characters used to encapsulate your strings threw some errors on my part, maybe try to change that too.

Personally I’d refactor it as

value = 1
isCompleted = event.source.parent.getComponent('ChemOkay').state
tagName='[default]Upstream/AL_POUR/Forms/Chemistry/' + 'trgChemTestComplete.value' if  isCompleted else 'trgChemTestLog.value'
system.tag.writeBlocking([tagName],[value])

IMHO duplicating the the common stuff is a BadThing™.
This also emphasizes that there is only a single write going on and that the selection of the tag names is based on the test being completed.

Of course I also thing that Python conditional statements are a BadThing™

So you could also refactor it as:

value = 1
isCompleted = event.source.parent.getComponent('ChemOkay').state
tagName='[default]Upstream/AL_POUR/Forms/Chemistry/' + {True:'trgChemTestComplete.value',False:'trgChemTestLog.value'}[isCompleted]
system.tag.writeBlocking([tagName],[value])

Which may even look worse in your eyes … lol

If we’re going down that road, I’d have used a single integer to track the status of the process. But, everyone starts somewhere. :wink:

1 Like