I have a boolean tag that occasionally gets set TRUE from a PLC. The tag has a value change script that should run when its value becomes TRUE but for some reason its running the script when it goes TRUE and again when it goes FALSE. I tested it by turning it into a memory boolean and toggling it myself. The script begins as such:
if not initialChange:
if currentValue.value == True:
"area where scripting magick happens"
I'm a little lost as to why it would trigger when the tag goes into the FALSE state, and any input would be much appreciated.
if not initialChange:
if currentValue.value == True:
print "initialChange=%s currentValue.value=%s" % (initialChange, currentValue.value)
# area where scripting magick happens
Also, copy all of your script into an external editor and make sure it's all tabs or all spaces. There was another thread with weird behavior recently that was caused by a mix of tabs/spaces in the script.
And don't forget that you can just use, if currentValue.value:
This will return a logical True or False and the logic will respond correctly. For False testing use, if not currentValue.value:
It's all tabs, I've gone through and checked multiple times.
I have posted the entire script below, minus the "magick" part, which was just supposed to be a placeholder. After running calculations it's writing an adjusted value back to a single tag for now, during my testing phase, and when I set the boolean tag to TRUE I can see the value in the other tag change. If I set the boolean tag to FALSE, I see the value in the other tag change a second time.
if not initialChange:
if currentValue.value == True:
for i in range(0,19):
for x in range(1,96):
value = system.tag.read("[.]ARRAYS/ARRAY "+str(i)+"/MEMBER "+str(x))
system.tag.write("[.]DATA["+str(i)+","+str(x-1)+"]", value)
for i in range(0,95):
values = []
for x in range(0,19):
calc = system.tag.read("[.]DATA["+str(x)+","+str(i)+"]")
values.append(calc.value)
cValue = system.math.median(values)
system.tag.write("[.]MEDIANS[0,"+str(i)+"]", cValue)
Enable = True
if Enable == True:
for i in range(1,97):
Mode = True
medianValue = system.tag.read("[default]PACKAGING/LINE 6/CAMERA/CAMERA DATA/MEDIANS[0,"+str(i-1)+"]").value
flowSP = system.tag.readBlocking("[default]PACKAGING/LINE 6/HUEFT/OVERFILL PER VALVE/VALVE"+str(i)+"/SP_TIMER_FLOWRATE")[0].value
AutoMode = True
if Mode == True and AutoMode == True:
if medianValue >= 980:
adjustedFlow = flowSP + 12
elif 980 > medianValue >= 920:
adjustedFlow = flowSP + 6
elif 920 > medianValue >= 880:
adjustedFlow = flowSP + 3
elif 720 > medianValue > 700:
adjustedFlow = flowSP - 3
elif 700 > medianValue > 640:
adjustedFlow = flowSP - 6
elif 620 >= medianValue >= 0:
adjustedFlow = flowSP - 12
elif medianValue == -1:
adjustedFlow = flowSP
# system.tag.write("[default]PACKAGING/LINE 6/HUEFT/OVERFILL PER VALVE/VALVE"+str(i)+"/SP_TIMER_FLOWRATE", flowSP)
system.tag.write("[default]Scratchpad/testFlow["+str(i-1)+"]", adjustedFlow)
if i == 1:
system.tag.write("[default]PACKAGING/LINE 6/HUEFT/OVERFILL PER VALVE/VALVE1/SP_TIMER_FLOWRATE", adjustedFlow)
When I paste your code into Notepad++ I can see that lines 8 to 14 have three tabs and then multiples of four spaces. I don't think that's your problem though.
Well, I got it working by adding an evaluation of the currentValue to the script at line 45:
if i == 1 and currentValue.value == True:
system.tag.write("[default]PACKAGING/LINE 6/HUEFT/OVERFILL PER VALVE/VALVE1/SP_TIMER_FLOWRATE", adjustedFlow)
I can't tell if its triggering the script twice and only writing once or if its actually triggering the script only one time, but I don't see the result of my calculations being added to the tag twice. Doesn't make sense because I would think that the initial evaluation of the currentValue would be all that was needed, but, here we are.