# 2 State Toggle Event Script

**URL:** <https://forum.inductiveautomation.com/t/2-state-toggle-event-script/93706>\
**Category:** Ignition\
**Created:** [July 24, 2024, 9:03pm UTC](https://forum.inductiveautomation.com/t/2-state-toggle-event-script/93706 "2024-07-24T21:03:28Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![GChase](https://avatars.discourse-cdn.com/v4/letter/g/b3f665/32.png) [@GChase](https://forum.inductiveautomation.com/u/GChase)\
**Post date:** [July 24, 2024, 9:03pm UTC](https://forum.inductiveautomation.com/t/2-state-toggle-event-script/93706/1 "2024-07-24T21:03:29Z")

</div>

When using a 2 state toggle what is the best way to set up the a script to write the value to a tag? I found two ways of doing this: one through the Event configuration using onActionPreformed script and the other using a change script on props.selected.

##### Events onActionPreformed script:

```python
 def runAction(self, event):
	if self.props.selected == 1:
		system.tag.write("Test/TestOos",1)
	else:
		system.tag.write("Test/TestOos",0) 

```

##### props.selected change script:

```python
def valueChanged(self, previousValue, currentValue, origin, missedEvents):
	system.tag.write("Test/TestOos",currentValue) 

```

Is one way better than the other?

---

<div class="post-metadata">

**Author:** ![Transistor](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/transistor/32/3380_2.png) [@Transistor](https://forum.inductiveautomation.com/u/Transistor)\
**Post date:** [July 24, 2024, 9:18pm UTC](https://forum.inductiveautomation.com/t/2-state-toggle-event-script/93706/2 "2024-07-24T21:18:51Z")

</div>

The first could be re-written as,

```python
def runAction(self, event):
	system.tag.write("Test/TestOos", self.props.selected)

```

I don't know which of your two solutions would be better but a much better way would be to create a tag binding on the toggle. Point it directly to "Test/TestOos" and make the binding bidirectional. No script needed so it's faster and more efficient.

---

<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:** [July 24, 2024, 9:19pm UTC](https://forum.inductiveautomation.com/t/2-state-toggle-event-script/93706/3 "2024-07-24T21:19:55Z")

</div>

The two scripts are effectively equivalent (I'll point out that you can rewrite the action as just `system.tag.write("Test/TestOos", self.props.selected)`), but the better question is - why use a script at all? A bidirectional binding on `selected` will mean that your toggle always reflects the value of the tag, _and_ writes back to the tag whenever the button is pressed.

---

<div class="post-metadata">

**Author:** ![GChase](https://avatars.discourse-cdn.com/v4/letter/g/b3f665/32.png) [@GChase](https://forum.inductiveautomation.com/u/GChase)\
**Post date:** [July 24, 2024, 9:33pm UTC](https://forum.inductiveautomation.com/t/2-state-toggle-event-script/93706/4 "2024-07-24T21:33:28Z")

</div>

Thank you guys! this is exactly what is was looking for!! The whole time I was thinking..."there has got to be a more efficient/effective route".
