Toggle a tag in PLC based on the visibility of one component

Hello everyone,

I am trying to toggle a boolean tag of a PLC based on the visibility of one of my components in vision. I wrote the script below in the property change script of my component, but the code did not work and I am trying to understand why. I would appreciate any suggestion. Thank you!

if event.source.visible == 1:
	system.tag.writeBlocking("[default]NJ501/Bin1.value",1)
else:
	system.tag.writeBlocking("[default]NJ501/Bin1.value", 0)

I made it working with this script:

system.tag.writeBlocking("[default]NJ501/Bin1.value", event.source.visible)

You don’t need a script for this at all.
If you want the tag to update, and your component to reflect that, just bind the Visibility property on the component directly to the tag in question.
If you want updates to your component’s visibility to update the tag as well, then make that tag binding ‘Bidirectional’.

1 Like

Thank you very much for your response, Paul. I wanted to do this at the beginning, but I have another script for visibility. I was not sure how can I add write to tag to this script here.

if({[default]op[0]} = "Right" && {Root Container.model.value} = 72, 1,
		if({[default]opt[1]} = "Left" && {Root Container.model.value} = 75, 1,
     		0
	)
)

I’ve tested the original script you provided and it worked just fine for me, what happened (or didn’t happen) on your side?

1 Like

You are correct! I had one small mistake in my code in the designer that is why it did not work for me. Anyway the 2nd code was shorter and it worked as well.

I’ve noticied something on your script that might cause some performance issues later on(i managed to freeze my designer more than once with this). You should probably include this first line on your script:

if event.propertyName == "visible":
	system.tag.writeBlocking("[default]NJ501/Bin1.value", event.source.visible)

Your script executes every time ANY change on the component happens because it’s on the property change script, if you don’t filter it for the right property change you want to watch, it might cause issues. Imagine a slighty more complex script or many components doing something similar or a component that has very quick changes to its value property, the complex script will execute every time anything in your component changes

1 Like

That is a really great point! Thank you Leonardo! I will definitely do that.

If you’re doing this on the client page, this will only write to the tag when a client has the page open (not to mention it will be written to by every client that has that page open). This is far better handled inside 1) the plc, or 2nd resort, in a tag or global script which don’t depend on having a client open on the correct page

1 Like