Gateway Script Tag Issue

I have the following gateway script:

def dura(tagPath,box):
	tag = system.tag.readBlocking(tagPath)
	if tag == 2:
		box.props.value = tag
	else:
		box.props.value = 0

Very simple script, if the tags value is equal to 2, then display 2 in a numeric entry field.

Then in my project, I have a button with the following script:

	startTime = "[default]Timesheet/startTime"
	box = self.getSibling("startTime1")
	message.dura(startTime,box)

startTime is the tag that is being read in the gateway script and box is the numeric entry field where the value 2 should be displayed.

Currently, the tag does have a value if 2. So, in theory, a value of 2 should be displayed… but a 0 is being displayed. I can read the tag in correctly in the gateway script, but every time I use the tag in a conditional statement, it seems as if the value isn’t being read…

I tried to just use the value attribute of the tag, but I get an error saying

‘str’ object has no attribute ‘value’

Thank you in advance.

tag = system.tag.readBlocking(tagPath)[0].value

Added this and am still getting the same result

You need to do some hunting then. Try logging the value and type of tag before the comparison.

Add something like:

# If you're running in a gateway context, this should show up on the webpage log viewer.
_logger = system.util.getLogger('script debug')
_logger.info(tag)
_logger.info(type(tag))

You said gateway script, but you’re trying to interact with components. Exactly where is this script defined?

1 Like

image

Here is where the script resides.

The script is called when a button is clicked in my project.

Okay, that makes more sense.

Given that, I would expect this variation of your original script to work:

def dura(tagPath,box):
	tag = system.tag.readBlocking(tagPath)[0].value
	if tag == 2:
		box.props.value = tag
	else:
		box.props.value = 0

What error are you getting from that, if any?

I am not receiving an error, the problem is that the conditional statement is returning a 0 when the value of the tag is, in fact, 2. So, the conditional statement should return a 2, rather than a 0.

I would then echo @zacht’s suggestion to log the data type of the value; perhaps it’s a string when you’re comparing it against an int? Python won’t do any automatic type coercion.

2 Likes

Got it, had to change it to an int.

1 Like