Gateway Tag Change Script Behavior

Hi,

I have the following simple gateway tag change script:

 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?

Which Tag does script is configured on ?

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

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.

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?

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.

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:

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])

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

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:

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.

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


Thank you witman for your support!