Phython coding help

I have this Code


:

Why is it when barge source is not true it saves a 0 into target percent? I am newer to Phython coding sorry if this is a silly question but ChatGPT couldn't solve it for me so here i am.

Have you confirmed the type of value you are getting from barge source? Is it actually a boolean or is it a string? Strings of non zero length are treated as true when coerced to a boolean.

1 Like
def runAction(self, event):
	barge_source_silo_1 = system.tag.read("[West Point]BargeSourceSilo1Mem").value
	

    # Target percent memory tags
	target_percent_mem_1 = system.tag.read("[West Point]Silo1TargetPercentMem").value

    # Non-memory target percent tags (the ones that are written to)
	target_percent_1 = "[West Point]Silo1TargetPercent"


    # Check each barge source and write the corresponding target percent
	if barge_source_silo_1 == True:
		system.tag.write(target_percent_1, target_percent_mem_1)

Yes it is a memory tag that I made boolean. Just double checked aswell.

I would recommend using system.util.getLogger and logging the value or system.perspective.print() to print the value at button press. If it really is a boolean it is certainly not seeing if False == True and proceeding.

Also system.tag.read is deprecated, use system.tag.readBlocking instead.

2 Likes

Is this action script on a button? Are you trying to prevent writing a value to target percent unless some other requirement is met that makes barge source true? Could you explain the overall use case for us? There may be an approach that requires less code.

Also, if you decide to use system.tag.readBlocking, the system.tag.read*() functions return a list of Qualified Value Objects.

A qualified value has value, quality, and timestamp attributes. To get the value you must access the value attribute of the Qualified Value element at the index corresponding to the tag path.

actualValue = system.tag.readBlocking(['TagPath\Tag_1'])[0].value

you can also use type(barge_source_silo_1) to confirm.

Well embarrassingly enough I figured out the issue. The guy on the PLC side had something that 0ed them and the connection between the PLC and ignition is almost instant. So knowing my python ignorance I assumed I was doing something wrong. But all fixed thanks for the help guys