# Gateway Tag Change Script Behavior

**URL:** <https://forum.inductiveautomation.com/t/gateway-tag-change-script-behavior/35674>\
**Category:** General Discussion\
**Created:** [June 15, 2020, 6:41pm UTC](https://forum.inductiveautomation.com/t/gateway-tag-change-script-behavior/35674 "2020-06-15T18:41:29Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![vasilevdim](https://avatars.discourse-cdn.com/v4/letter/v/7ba0ec/32.png) [@vasilevdim](https://forum.inductiveautomation.com/u/vasilevdim)\
**Post date:** [June 15, 2020, 6:41pm UTC](https://forum.inductiveautomation.com/t/gateway-tag-change-script-behavior/35674/1 "2020-06-15T18:41:29Z")

</div>

Hi,

I have the following simple gateway tag change script:

```auto
 if not initialChange:
	 currentValue = event.getCurrentValue().getValue()
	 previousValue = event.getPreviousValue().getValue()
	 ss = system.tag.readBlocking(["[New]/Trigger"])[0].value	
	    	
if currentValue == 0 and ss == 1:
	system.tag.writeBlocking(["[New]/BatchCount"],[previousValue])

```

The problem is that no matter what value the memory boolean tag “Trigger” has , the script “works”, i.e. the previousValue is written to tag “BatchCount”. What is the reason?

---

<div class="post-metadata">

**Author:** ![kaushik.shah](https://avatars.discourse-cdn.com/v4/letter/k/34f0e0/32.png) [@kaushik.shah](https://forum.inductiveautomation.com/u/kaushik.shah)\
**Post date:** [June 15, 2020, 8:14pm UTC](https://forum.inductiveautomation.com/t/gateway-tag-change-script-behavior/35674/2 "2020-06-15T20:14:50Z")

</div>

Which Tag does script is configured on ?

---

<div class="post-metadata">

**Author:** ![vasilevdim](https://avatars.discourse-cdn.com/v4/letter/v/7ba0ec/32.png) [@vasilevdim](https://forum.inductiveautomation.com/u/vasilevdim)\
**Post date:** [June 16, 2020, 3:37am UTC](https://forum.inductiveautomation.com/t/gateway-tag-change-script-behavior/35674/3 "2020-06-16T03:37:35Z")

</div>

Script is configured on memory Long tag “Counter” - still in test environment.

---

<div class="post-metadata">

**Author:** ![vasilevdim](https://avatars.discourse-cdn.com/v4/letter/v/7ba0ec/32.png) [@vasilevdim](https://forum.inductiveautomation.com/u/vasilevdim)\
**Post date:** [June 16, 2020, 5:05pm UTC](https://forum.inductiveautomation.com/t/gateway-tag-change-script-behavior/35674/4 "2020-06-16T17:05:45Z")

</div>

This is the way my script is ordered (dented) too.  
But it does not work the way it should be. Even if I give to memory tag “Trigger” value “False”, the overwriting of tag “BatchCount” takes place.

---

<div class="post-metadata">

**Author:** ![witman](https://avatars.discourse-cdn.com/v4/letter/w/d2c977/32.png) [@witman](https://forum.inductiveautomation.com/u/witman)\
**Post date:** [June 16, 2020, 5:58pm UTC](https://forum.inductiveautomation.com/t/gateway-tag-change-script-behavior/35674/5 "2020-06-16T17:58:49Z")

</div>

It's not immediately obvious to me what would cause the behavior you're seeing. Some questions: is that the whole script? Does ss get set to anything when initialChange is true? Should your second if code block be executing even when initialChange is true, or should it be indented under your first if?

The forward slash after the closing square bracket around your tag provider in tag path seems a bit unusual, but doesn't appear to affect tag reads. What does this script return if you run it in the script console when your trigger tag is false?

> [@vasilevdim](#):
>
> ss = system.tag.readBlocking(["[New]/Trigger"])[0].value  
> print ss  
> print bool(ss)

---

<div class="post-metadata">

**Author:** ![vasilevdim](https://avatars.discourse-cdn.com/v4/letter/v/7ba0ec/32.png) [@vasilevdim](https://forum.inductiveautomation.com/u/vasilevdim)\
**Post date:** [June 16, 2020, 6:24pm UTC](https://forum.inductiveautomation.com/t/gateway-tag-change-script-behavior/35674/6 "2020-06-16T18:24:30Z")

</div>

Yes, this is the whole script. ss can be anything, it does not get to anything when initialChange is truth. The second if is intended under the first if. The slashes do not change anything, the script runs properly in the script console.

---

<div class="post-metadata">

**Author:** ![witman](https://avatars.discourse-cdn.com/v4/letter/w/d2c977/32.png) [@witman](https://forum.inductiveautomation.com/u/witman)\
**Post date:** [June 16, 2020, 6:34pm UTC](https://forum.inductiveautomation.com/t/gateway-tag-change-script-behavior/35674/7 "2020-06-16T18:34:36Z")

</div>

> [@vasilevdim](#):
>
> The second if is intended under the first if.

The way the code is indented in your original post, the second `if` is evaluated regardless of whether `initialChange` is true or not. This means it also gets evaluated whether `ss` is set or not. If you only want a write to occur when initialChange is _not_ true, you'll want to indent the second `if` like this:

```auto
if not initialChange:
	currentValue = event.getCurrentValue().getValue()
	previousValue = event.getPreviousValue().getValue()
	ss = system.tag.readBlocking(["[New]/Trigger"])[0].value	
	if currentValue == 0 and ss == 1:
		system.tag.writeBlocking(["[New]/BatchCount"],[previousValue])

```

---

<div class="post-metadata">

**Author:** ![vasilevdim](https://avatars.discourse-cdn.com/v4/letter/v/7ba0ec/32.png) [@vasilevdim](https://forum.inductiveautomation.com/u/vasilevdim)\
**Post date:** [June 16, 2020, 6:54pm UTC](https://forum.inductiveautomation.com/t/gateway-tag-change-script-behavior/35674/8 "2020-06-16T18:54:46Z")

</div>

you are right, but still it does not work. Very strange indeed.

---

<div class="post-metadata">

**Author:** ![witman](https://avatars.discourse-cdn.com/v4/letter/w/d2c977/32.png) [@witman](https://forum.inductiveautomation.com/u/witman)\
**Post date:** [June 16, 2020, 7:28pm UTC](https://forum.inductiveautomation.com/t/gateway-tag-change-script-behavior/35674/9 "2020-06-16T19:28:52Z")

</div>

Yes. I’m thinking you must be somehow getting something different than expected for ss–or you’ve run into a weird bug. Maybe try logging some information:

```auto
if not initialChange:
	currentValue = event.getCurrentValue().getValue()
	previousValue = event.getPreviousValue().getValue()
	ss = system.tag.readBlocking(["[New]/Trigger"])[0].value
	logger = system.util.getLogger("TheNameYouWantInGatewayWebInterfaceLogs")
	logger.info"'Script executed with currentValue %s and ss value %s."% (currentValue, ss)
	if currentValue == 0 and ss == 1:
		logger.info"'2nd if executed with currentValue %s and ss value %s."% (currentValue, ss)
		system.tag.writeBlocking(["[New]/BatchCount"],[previousValue])

```

Filter for the logger name you gave in logs on the gateway web interface to see what this logs and maybe it’ll offer some insight.

---

<div class="post-metadata">

**Author:** ![vasilevdim](https://avatars.discourse-cdn.com/v4/letter/v/7ba0ec/32.png) [@vasilevdim](https://forum.inductiveautomation.com/u/vasilevdim)\
**Post date:** [June 17, 2020, 2:53pm UTC](https://forum.inductiveautomation.com/t/gateway-tag-change-script-behavior/35674/10 "2020-06-17T14:53:36Z")

</div>

I restarted the gateway and the script worked. Strange. Below the logs snip:

 ![Logs](https://us1.discourse-cdn.com/inductiveautomation/original/2X/1/1fd51eb68da38b5bd2d7dee9de1b11bbe4c3c34a.png)  
Thank you witman for your support!
