Gateway Tag Change Script Does Not Run

Hi, it might be silly question, but still there is a long way in front of me in the scripting and IA.
So, I have gateway tag change script, which works properly only once, when the script is commit and the project saved. How to make the script running, i.e updating the tag DailyOutput12 values on regular intervals (supposed equal to the gateway scan rate?):

if not initialChange:
currentValue = event.getCurrentValue().getValue()
previousValue = event.getPreviousValue().getValue()

if currentValue == 0 and previousValue == 1:

cc = system.tag.readBlocking(["[default]/UNI_Room12/CurrentBatchCount12"])[0].value
sc = system.tag.readBlocking(["[default]/UNI_Room12/StartCount12"])[0].value
pv = system.tag.readBlocking(["[default]/UNI_Room12/PreviousBatchCount12"])[0].value
	
output12 = cc-sc
system.tag.writeBlocking(["[default]UNI_Room12/DailyOutput12"],[output12])	

if currentValue == 1 and previousValue == 0:

	output12 = pv-sc+cc
	system.tag.writeBlocking(["[default]UNI_Room12/DailyOutput12"],[output12])

How is the tag you are using to trigger the script set up?

Please edit this post to include formatting marks for your code. Three reverse-single-quotes by themselves on a line above and again on a line below your code. It should look like this in the comment editor:
```
pasted code
```

Hi, Jordan,
The tag is BatchResetBit12
I can recall you helped me few days ago with other piece from the same task (daily counter which might be reset anytime of the shift. Sorry I could not make a snip showing the whole screen.

Taking a closer look at what you have, it looks like the script errors out on your second β€˜if’ block. cc/sc/pv are not defined, as they are defined in the first β€˜if’ block. They should be defined before any β€˜reusable’ code.

if not initialChange:
	currentValue = event.getCurrentValue().getValue()
	previousValue = event.getPreviousValue().getValue()
	
	cc = system.tag.readBlocking(["[default]UNI_Room12/CurrentBatchCount12"])[0].value
	sc = system.tag.readBlocking(["[default]UNI_Room12/StartCount12"])[0].value
	pv = system.tag.readBlocking(["[default]UNI_Room12/PreviousBatchCount12"])[0].value

	if currentValue == 0 and previousValue == 1:
		output12 = cc-sc
		system.tag.writeBlocking(["[default]UNI_Room12/DailyOutput12"],[output12])	

	if currentValue == 1 and previousValue == 0:
		output12 = pv-sc+cc
		system.tag.writeBlocking(["[default]UNI_Room12/DailyOutput12"],[output12])

No, here is the initial code:
""if not initialChange:
currentValue = event.getCurrentValue().getValue()
previousValue = event.getPreviousValue().getValue()

if currentValue == 0 and previousValue == 1:

cc = system.tag.readBlocking(["[default]/UNI_Room12/CurrentBatchCount12"])[0].value
sc = system.tag.readBlocking(["[default]/UNI_Room12/StartCount12"])[0].value
pv = system.tag.readBlocking(["[default]/UNI_Room12/PreviousBatchCount12"])[0].value
	
output12 = cc-sc
system.tag.writeBlocking(["[default]UNI_Room12/DailyOutput12"],[output12])	

if initialChange:
currentValue = event.getCurrentValue().getValue()
previousValue = event.getPreviousValue().getValue()

if currentValue == 1:

	cc = system.tag.readBlocking(["[default]/UNI_Room12/CurrentBatchCount12"])[0].value
	sc = system.tag.readBlocking(["[default]/UNI_Room12/StartCount12"])[0].value
	pv = system.tag.readBlocking(["[default]/UNI_Room12/PreviousBatchCount12"])[0].value
		
		
	output12 = pv-sc+cc
	system.tag.writeBlocking(["[default]UNI_Room12/DailyOutput12"],[output12])	""	

So it should be something else. What it might be?

cc, sc, and pv need to be defined before you can do anything with them. In your code, if currentValue is 1, they will not be defined, causing an error when you try to calculate pv-sc+cc.

Take a look at where I placed the definitions above the if statements. Since they are defined right away, they can be used in either β€˜if’ block.

Also try it without the initialChange flag, if you are not concerned on what it does when the gateway first starts up.

currentValue = event.getCurrentValue().getValue()

cc = system.tag.readBlocking(["[default]UNI_Room12/CurrentBatchCount12"])[0].value
sc = system.tag.readBlocking(["[default]UNI_Room12/StartCount12"])[0].value
pv = system.tag.readBlocking(["[default]UNI_Room12/PreviousBatchCount12"])[0].value

if currentValue == 1:
	output12 = pv-sc+cc
else:
	output12 = cc-sc

system.tag.writeBlocking(["[default]UNI_Room12/DailyOutput12"],[output12])

Thank you, Jordan, code nice and clean now, I will revert tomorrow after check up.

Watch out! The code to get the previous value will fail on the initial run.

1 Like

Huh. Thought I took that out. Thanks for the catch. Edited.

Thank you both, guys.

The reason that the script was not updating was that it was bind to boolean tag, which was supposed to work once only per shift - when the counter is reset.
Below is the code in its final variant:
β€˜β€™β€™


Thank you once again for the help!

1 Like