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

**URL:** <https://forum.inductiveautomation.com/t/toggle-a-tag-in-plc-based-on-the-visibility-of-one-component/50237>\
**Category:** Ignition\
**Created:** [August 24, 2021, 6:19pm UTC](https://forum.inductiveautomation.com/t/toggle-a-tag-in-plc-based-on-the-visibility-of-one-component/50237 "2021-08-24T18:19:20Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![pkhoshroo](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pkhoshroo/32/26801_2.png) [@pkhoshroo](https://forum.inductiveautomation.com/u/pkhoshroo)\
**Post date:** [August 24, 2021, 6:19pm UTC](https://forum.inductiveautomation.com/t/toggle-a-tag-in-plc-based-on-the-visibility-of-one-component/50237/1 "2021-08-24T18:19:20Z")

</div>

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!

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

```

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/8/d/8d5fcfc16776253be02eea307d6531ea277e99ea.png)

---

<div class="post-metadata">

**Author:** ![pkhoshroo](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pkhoshroo/32/26801_2.png) [@pkhoshroo](https://forum.inductiveautomation.com/u/pkhoshroo)\
**Post date:** [August 24, 2021, 6:38pm UTC](https://forum.inductiveautomation.com/t/toggle-a-tag-in-plc-based-on-the-visibility-of-one-component/50237/2 "2021-08-24T18:38:38Z")

</div>

I made it working with this script:

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

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [August 24, 2021, 6:38pm UTC](https://forum.inductiveautomation.com/t/toggle-a-tag-in-plc-based-on-the-visibility-of-one-component/50237/3 "2021-08-24T18:38:58Z")

</div>

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’.

---

<div class="post-metadata">

**Author:** ![pkhoshroo](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pkhoshroo/32/26801_2.png) [@pkhoshroo](https://forum.inductiveautomation.com/u/pkhoshroo)\
**Post date:** [August 24, 2021, 6:41pm UTC](https://forum.inductiveautomation.com/t/toggle-a-tag-in-plc-based-on-the-visibility-of-one-component/50237/4 "2021-08-24T18:41:57Z")

</div>

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.

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

```

---

<div class="post-metadata">

**Author:** ![leonardo.godoi](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/leonardo.godoi/32/27178_2.png) [@leonardo.godoi](https://forum.inductiveautomation.com/u/leonardo.godoi)\
**Post date:** [August 24, 2021, 8:30pm UTC](https://forum.inductiveautomation.com/t/toggle-a-tag-in-plc-based-on-the-visibility-of-one-component/50237/5 "2021-08-24T20:30:13Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![pkhoshroo](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pkhoshroo/32/26801_2.png) [@pkhoshroo](https://forum.inductiveautomation.com/u/pkhoshroo)\
**Post date:** [August 25, 2021, 5:15pm UTC](https://forum.inductiveautomation.com/t/toggle-a-tag-in-plc-based-on-the-visibility-of-one-component/50237/6 "2021-08-25T17:15:11Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![leonardo.godoi](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/leonardo.godoi/32/27178_2.png) [@leonardo.godoi](https://forum.inductiveautomation.com/u/leonardo.godoi)\
**Post date:** [August 25, 2021, 5:57pm UTC](https://forum.inductiveautomation.com/t/toggle-a-tag-in-plc-based-on-the-visibility-of-one-component/50237/7 "2021-08-25T17:57:23Z")

</div>

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:

```auto
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

---

<div class="post-metadata">

**Author:** ![pkhoshroo](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pkhoshroo/32/26801_2.png) [@pkhoshroo](https://forum.inductiveautomation.com/u/pkhoshroo)\
**Post date:** [August 25, 2021, 5:58pm UTC](https://forum.inductiveautomation.com/t/toggle-a-tag-in-plc-based-on-the-visibility-of-one-component/50237/8 "2021-08-25T17:58:42Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![nminchin](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/nminchin/32/6911_2.png) [@nminchin](https://forum.inductiveautomation.com/u/nminchin)\
**Post date:** [August 25, 2021, 8:02pm UTC](https://forum.inductiveautomation.com/t/toggle-a-tag-in-plc-based-on-the-visibility-of-one-component/50237/9 "2021-08-25T20:02:25Z")

</div>

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
