Tag value changed script "currentValue" always true when used in if statement?

I've had to use the "System.tag.read" to get the value of the tag within the "Value Changed" script because "currentValue" is inconsistent. When I use it with an if statement it always thinks it's true, when I use it with "System.tag.write" it writes the value I expect.

def valueChanged(tag, tagPath, previousValue, currentValue, initialChange, missedEvents):
	#cv = system.tag.read("[default]Oscillators/%s" % (machine)).value
	cv = currentValue
	if cv 
		system.tag.write("[default]New Tag", 1)
	else:
		system.tag.write("[default]New Tag", 0)

Whether the value of the tag is true or false, it still writes 1 to the new tag.

If it's done like this though, it works as expected so New Tag becomes true or false depending on the value of the tag changing.

def valueChanged(tag, tagPath, previousValue, currentValue, initialChange, missedEvents):
	cv = system.tag.read("[default]Oscillators/%s" % (machine)).value
	#cv = currentValue
	if cv 
		system.tag.write("[default]New Tag", 1)
	else:
		system.tag.write("[default]New Tag", 0)

But somehow this also works.

def valueChanged(tag, tagPath, previousValue, currentValue, initialChange, missedEvents):
	system.tag.write("[default]New Tag", currentValue)

Try cv = currentValue.value

currentValue and previousValue are Qualified Values, similar to what you get from a tag read.

2 Likes

This video might be helpful on understanding qualitied values and what is returned.

1 Like

Because, system.tag.write() and it's replacments system.tag.writeBlocking() and system.tag.writeAsync() (which you should be using) accept a qualified value. So in the background the function correctly breaks out the objects value for the write.

3 Likes