If combined condition

Hi all
I try to learn the ignition script and I have a problem with IF and condition when I use combined condition it doesn’t work.

if door==0 and automat==1:
		if value == 1 :
			time = time+1
			system.tag.write('[default]CM_Merker/cas_prostoj', time)
	
		else : 
			time = system.tag.read('[default]CM_Merker/cas_prostoj')

image

I don’t see an issue with that part of the script. I assume the variables are initiated somewhere above the part you showed us.

Could you tell us what’s going wrong (do you get an error message, or does the code give a wrong result), and what part you got working already?

Preferably also share the full script next time, so we have more insight in what’s happening.

time = system.tag.read('[default]CM_Merker/cas_prostoj').getValue()
	automat = system.tag.read('[default]CM21/Machine/Data/ModeAutomat.value')
	door = system.tag.read('[default]CM21/Machine/Data/DoorClose.value')
	buttStart = system.tag.read('[default]CM21/Machine/Data/StartTl.value')
	heating = system.tag.read('[default]CM21/Machine/Data/ModeHeating.value')
	
	if door==0 and automat==1:
		if value == 1 :
			time = time+1
			system.tag.write('[default]CM_Merker/cas_prostoj', time)
	
		else : 
			time = system.tag.read('[default]CM_Merker/cas_prostoj')

	else: 
		if str(buttStart)=='true' :
		   time =0 
		   system.tag.write('[default]CM_Merker/cas_prostoj', time)
	return time

This is the whole code. the problem is that it will never pass this condition.
And the tag values are door false and automate true.

System.tag.read returns a QualifiedValue object. To get the value part you need to read the value property from it. Eg
system.tag.read(...).value
Also, no need to add ‘.value’ at the end of your tag path. That is implied

Thanks i repair my code and now is it OK :smiley:

1 Like

Since you said you are trying to learn, I thought I would point out a few things.

  1. I would try to get in the habit of reading all or as many tags as you need once at the beginning of the script and then writing all or as many as possible at the end of the script.
  2. Avoid unnecessary nesting and branches
  3. Treat boolean values like booleans
  4. Know what functions have been deprecated. While legacy functions may be supported there is a reason they have been deprecated. Also, support for those functions may be removed in future versions and then your code will break.

So, I assume you are using some version of 8.0 since you tagged this with perspective. If that is the case then you should replace system.tag.read and write with the appropriate functions, readBlocking, readAsync, writeBlocking, writeAsync. The legacy functions read and write are supported for backwards compatibility but they should not be used, particularly in new scripts.

The new functions accept a list of Tag Paths and will facilitate reading multiple tags at once. This is far more efficient than calling the functions multiple times.

The new write functions also accept a list of values. If you are writing multiple times then the index of the tag path should match the index of the intended value.

Taking all of that into consideration your script could look something like this:

tagPaths = ['[default]CM_Merker/cas_prostoj','[default]CM21/Machine/Data/ModeAutomat','[default]CM21/Machine/Data/DoorClose','[default]CM21/Machine/Data/StartTl','[default]CM21/Machine/Data/ModeHeating']
tagValues = system.tag.readBlocking(tagPaths)

#the values returned will be in a list with the same index as the associated tag path.
#remember that the read functions return Qualified Values
time = tagValues[0].value
autmat = tagValues[1].value
door = tagValues[2].value
buttStart = tagValues[3].value
heating = tagValues[4].value

#you do not define value anywhere in the provided script so technically it will never be 1
#unless you intend to take a different action based on value, there is no need for a nested if statement
if not door and automat and value:
     time += 1
#no need to nest an if statement here, just use and elif (else if) instead
elif str(buttStart)=='true':
     time = 0

system.tag.writeBlocking(['[default]CM_Merker/cas_prostoj'],[time])
return time

One last thing, when trying to figure out how to best use these functions the manual is your friend.

Hope this helps!

3 Likes