Swapping Screens based on a tag from PLC

No I did not,

This is the code:

val= system.tag.readBlocking('[default]OptTouch')[0].value
if val==1 and system.nav.getCurrentWindow()=="Top":
		system.nav.swapTo('Bottom')
else val==1 and system.nav.getCurrentWindow()=="Bottom":
		system.nav.swapTo('Top')
val= system.tag.readBlocking('[default]OptTouch')[0].value
if val:
	if system.nav.getCurrentWindow()== "Top":
		system.nav.swapTo('Bottom')
	else:
		system.nav.swapTo('Top')
2 Likes

I don’t know if its just the way its presenting, but you should only have 4 spaces or 1 tab indent.

Also its not else if its elif

1 Like

its either elif val ... :
or only else:

val= system.tag.readBlocking('[default]OptTouch')[0].value
if val==1 and system.nav.getCurrentWindow()=="Top":
		system.nav.swapTo('Bottom')
elif val==1 and system.nav.getCurrentWindow()=="Bottom":
		system.nav.swapTo('Top')

val= system.tag.readBlocking('[default]OptTouch')[0].value
if val==1 and system.nav.getCurrentWindow()=="Top":
system.nav.swapTo('Bottom')
else:
system.nav.swapTo('Top')

1 Like

You can't do it this way, because the else will fire when val is false

3 Likes

true

Thank you very much for all your help!
@victordcq @dkhayes117 and @bkarabinchak.psi.

I am going to try your solution and post the result here.

2 Likes

Long version:

# Put your trigger tag ('[default]OptTouch') into Client tag change scripts, then:

# A variable that is a flag (0 or 1) which indicates whether or not the event is due to the initial subscription or not. 
# This is useful as you can filter out the event that is the inital subscription, 
# preventing a script from running when the values haven't actually changed.
if not initialChange:

	# if your tag '[default]OptTouch' has changed to 1 (or True)
	if newValue.getValue() == 1:
	
		# Get current window
		curWin = system.nav.getCurrentWindow()
		
		# Check if TOP page is opened
		if curWin == 'Top': 
			# Swap to 'Bottom' page
			system.nav.swapTo('Bottom') # Must pass the new page's path to the function
			
		# Check if BOTTOM page is opened
		elif curWin == 'Bottom':
			# Swap to 'Top' page
			system.nav.swapTo('Top') # Must pass the new page's path to the function

		# else: 
			# Put here something if you need to.    

Short version:

if not initialChange:

	# if your tag '[default]OptTouch' has changed to 1 (or True)
	if newValue.getValue() == 1:
		
		# Swap to bottom page if current is top, else swap to top page
		system.nav.swapTo('Bottom') if system.nav.getCurrentWindow() == 'Top' else system.nav.swapTo('Top')
	
1 Like

Thank you very much! Both of them worked. I do really appreciate your help.

This question has been answered by

@dkhayes117 Solution1:

@bkarabinchak.psi and @victordcq Solution2:

@n.meluccio Solution 3:

Thank you!

1 Like

Solution 1 with a not initialChange condition is probably the best. Most readable and most maintainable. I’m just not a fan of one line ternary operators for function calls in the 3rd solution but its personal preference.

1 Like