# Perspective scripting will trigger twice

**URL:** <https://forum.inductiveautomation.com/t/perspective-scripting-will-trigger-twice/59535>\
**Category:** Ignition\
**Tags:** perspective, ignition81, scripting\
**Created:** [May 13, 2022, 11:48am UTC](https://forum.inductiveautomation.com/t/perspective-scripting-will-trigger-twice/59535 "2022-05-13T11:48:04Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![yifeiliu08](https://avatars.discourse-cdn.com/v4/letter/y/bbe5ce/32.png) [@yifeiliu08](https://forum.inductiveautomation.com/u/yifeiliu08)\
**Post date:** [May 13, 2022, 11:48am UTC](https://forum.inductiveautomation.com/t/perspective-scripting-will-trigger-twice/59535/1 "2022-05-13T11:48:04Z")

</div>

![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/4/9/494e4fd13c7dcb9cd0d065a9acc5089a1bab6b1e.png)

here is the code

```auto
	C=currentValue.value	
	Stock=Carcass.LotStock(Lot=C)
	system.perspective.print(Stock)
        # I use the print to see the output
	system.tag.write('[Malu OPC tags]Carcass/Scan/BarCode',C)

```

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/3/0/30382217377462fcd52dbc0650098189b832a39d.png)

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

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [May 13, 2022, 11:56am UTC](https://forum.inductiveautomation.com/t/perspective-scripting-will-trigger-twice/59535/2 "2022-05-13T11:56:22Z")

</div>

Print both current value and previous value.

---

<div class="post-metadata">

**Author:** ![yifeiliu08](https://avatars.discourse-cdn.com/v4/letter/y/bbe5ce/32.png) [@yifeiliu08](https://forum.inductiveautomation.com/u/yifeiliu08)\
**Post date:** [May 13, 2022, 12:10pm UTC](https://forum.inductiveautomation.com/t/perspective-scripting-will-trigger-twice/59535/3 "2022-05-13T12:10:16Z")

</div>

Hi pturmel :How to modify the code meke it only trigger once, only base on the current value change?

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [May 13, 2022, 12:17pm UTC](https://forum.inductiveautomation.com/t/perspective-scripting-will-trigger-twice/59535/4 "2022-05-13T12:17:36Z")

</div>

I don’t know yet. You haven’t done as I suggested: print both current and previous value. That should tell you if this is a spurious event or a bug. Consider also printing the `origin` value.

---

<div class="post-metadata">

**Author:** ![yifeiliu08](https://avatars.discourse-cdn.com/v4/letter/y/bbe5ce/32.png) [@yifeiliu08](https://forum.inductiveautomation.com/u/yifeiliu08)\
**Post date:** [May 14, 2022, 12:58am UTC](https://forum.inductiveautomation.com/t/perspective-scripting-will-trigger-twice/59535/5 "2022-05-14T00:58:18Z")

</div>

```auto
	C=currentValue.value
	P=previousValue
	system.perspective.print('CURRENT'+str(C))
	system.perspective.print('origin'+str(origin))
	system.perspective.print('Pre'+str(P))

```

I have try the code

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/0/e/0ed04a1b576fd26e3e85b9281899f102f46300c3.png)  
but when I add one more code

```auto
	C=currentValue.value
	P=previousValue
	system.perspective.print('CURRENT'+str(C))
	system.perspective.print('origin'+str(origin))
	system.perspective.print('Pre'+str(P))	
	
	system.tag.write('[Malu OPC tags]Carcass/Scan/BarCode',C)	
	# write this data to tag

```

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/4/0/408ac01263b423adb8e4493afb3b749c6e733657.png)

actually I need write the components value to that tag

---

<div class="post-metadata">

**Author:** ![lrose](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/lrose/32/62724_2.png) [@lrose](https://forum.inductiveautomation.com/u/lrose)\
**Post date:** [May 14, 2022, 1:58am UTC](https://forum.inductiveautomation.com/t/perspective-scripting-will-trigger-twice/59535/6 "2022-05-14T01:58:22Z")

</div>

The quality is changing. You will notice that in your second screen shot, the quality of the previous value changes from “Good” to “Good\_Unspecified”. You will also notice that the origin changes.

My guess is the the tag quality change is triggering the binding to update.

1. Filter the event so that the code is only executed in the value has changed.

```auto
if not currentValue.value == previousValue.value:
    #your code here

```

1. You should be using `system.tag.writeBlocking()` instead of the deprecated `system.tag.write()`

```auto
system.tag.writeBlocking(['[Malu OPC tags]Carcass/Scan/BarCode'],[C])

```

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [May 14, 2022, 5:21am UTC](https://forum.inductiveautomation.com/t/perspective-scripting-will-trigger-twice/59535/7 "2022-05-14T05:21:29Z")

</div>

> [@yifeiliu08](#):
>
> but when I add one more code

So, the property is bound to the tag that you are writing back to? If so, then of course you get another change event. The tag value, when written, is distributed to all bindings "listening" to it.

The origin property is there when you need such circular bindings, and want to suppress actions that don't come from the user (browser).

---

<div class="post-metadata">

**Author:** ![yifeiliu08](https://avatars.discourse-cdn.com/v4/letter/y/bbe5ce/32.png) [@yifeiliu08](https://forum.inductiveautomation.com/u/yifeiliu08)\
**Post date:** [May 14, 2022, 5:50am UTC](https://forum.inductiveautomation.com/t/perspective-scripting-will-trigger-twice/59535/8 "2022-05-14T05:50:43Z")

</div>

it works, thanks

---

<div class="post-metadata">

**Author:** ![yifeiliu08](https://avatars.discourse-cdn.com/v4/letter/y/bbe5ce/32.png) [@yifeiliu08](https://forum.inductiveautomation.com/u/yifeiliu08)\
**Post date:** [May 14, 2022, 5:51am UTC](https://forum.inductiveautomation.com/t/perspective-scripting-will-trigger-twice/59535/9 "2022-05-14T05:51:49Z")

</div>

I noted, thanks a lot
