Increasing the tag value in different rates

Can anyone tell me why the value of this gateway timer script becomes "null" any time I save the project? and how to fix it?
If I don't save It's works, no issues.

# Define the rates of increase for Y tag
rates = {'Low': 1500, 'Medium': 2163, 'High': 3090}
tags = system.tag.readBlocking(["[default]PredictionTabTags/ModelSuggestedO2Rate", "[default]PredictionTabTags/O2 Lance Consumtion "])

x_value = tags[0].value
y_value = tags[1].value

if x_value in rates:
	
	y_value += rates[x_value] / 60 # Divide by 60 to get the rate per second
	# Write the new Y tag value to the Y tag
	system.tag.writeBlocking(["[default]PredictionTabTags/O2LanceTEST"], [y_value])
	
else:
	    
	system.tag.writeBlocking(Test_tag_path, 0)  

Thanks

What is getting a null value? The O2LanceTEST tag?
If so, I don't see why it would, as each execution reads the tag values fresh from the tags.

You don't really need the else and the last writeBlocking lines as they're doing nothing, it seems unless there's more to the script than you're showing.

Yes the value of the [default]PredictionTabTags/O2LanceTEST" become null anything I save the project. Not sure why.

I need to reset the tag value to 0 when none of the conditions are met. My project is like a simulation and at some points everything resets to 0, at that time, rates = null so the tag value become 0, that's what I need.
When I remove the else, the tag value continues to increase even though all other values are zero, so I added the else.

Ok, I guess I assumed something else was setting it back to 0 when a "cycle" completed or something.
I would temporarily add some loggers then to see what values the readBlocking is returning. Are the other values from a PLC or driven by other logic/scripts? That could be the reason if they are.

Other values are driven by logics/scripts not PLC.
Good idea, I'll add to the logger. Thanks

Every time you save a project, it restarts the gateway scripts. Anything driven by a script/logic would restart as well, so if any have uninitialized values, they'll be null and cause this script to break. You may just need to add a try/except block to ignore these initial null values, or check for null before doing any calculations.

Thank you @michael.flagler, problem is solved. The tag was written into by two different scrips causing this problem.

now there is another script that is not working, I am not why because it was working before. Haven't been able to find the casue.

pot_value = system.tag.readBlocking("[default]PredictionTabTags/Power On Time")
	if pot_value == 0:
		
		heat_number = int(system.tag.readBlocking("[default]PredictionTabTags/Heat Number"))
		
		# Increment the Heat_Number tag by 1
		new_heat_number = heat_number + 1
		import time
		time.sleep(2)		  		        
		
		system.tag.writeBlocking("[default]PredictionTabTags/Heat Number.value", new_heat_number)

Why the time.sleep command??

system.tag.readBlocking returns a list of qualified value objects, not just a simple value like you are assuming in the code. Replace line 1 with:

pot_value = system.tag.readBlocking("[default]PredictionTabTags/Power On Time")[0].value

And do the same thing to your heat_number tag read.

1 Like

Original script was like this:

pot_value = (self.parent.parent.getChild("Settings").getChild("Parameters_Table").props.data[0].Power_On_Time)
	if pot_value == 0:
		
		heat_number = int("[default]PredictionTabTags/Heat Number")
		
		# Increment the Heat_Number tag by 1
		new_heat_number = heat_number + 1
		import time
		time.sleep(2)		  		        
		
		system.tag.writeBlocking("[default]PredictionTabTags/Heat Number.value", new_heat_number)

Reason for sleep, when it was working, because Pot_value could take couple of a seconds to change from 0, it was changing the new heat number multiple times until pot was more that 0, by adding sleep I was able to stop that and it worked fine. I just don't know why it stopped working.

@Bushmatic told you why: You were not using the function's return properly.

Remove the sleep. And if it doesn't work without it, we'll help you find a solution, but DO NOT use sleep for this.
hint: as their names suggests, readBlocking and writeBlocking are blocking. The script's execution will wait for them to return. Any sleep after that is pointless.
If you have issues because the tags values change between the read and the write, then it means you have a race condition. You need to fix that instead of trying to work around it. You won't get to a reliable solution by just adding sleep

5 Likes

It's not uncommon for me to use ChatGPT as a starting point for some Python code, but it pretty much never gets it right. Even after feeding back a few error messages or re-clarifying what it is that I wanted, I usually have to manually change portions of the code myself, multiple times, despite giving fairly clear instructions. It's even gone as far as I pointed out a glaring mistake in the script, ChatGPT apologized, but then provided a fixed version of the script that was still identical.

ChatGPT is a nice tool to use to learn some of Python's tricks, coming from more traditional languages, but it will not do the work for you.

Also, as before readBlocking() returns qualified values. So, use [0].value at the end of your readBlocking call (in cases where you're only reading one tag).

And FYI, because calls to readBlocking() should be minimal. In future use, combine any tag paths you need to read into a single list of paths and read them like so (assuming that you only need the values)

tagPaths = ['[default]SomeTag0','[default]SomeTag1']

tagVals = [tag.value for tag in system.tag.readBlocking(tagPaths)]

SomeTag0 = tagVals[0]
SomeTag1 = tagVals[1]

Same idea for writeBlocking() as well. Combine paths and values into parallel lists before a single call.

Not sure if that comparison to a qualified value is what's causing it to seem like it's not executing.

tagsToRead = ['[default]PredictionTabTags/Power On Time','[default]PredictionTabTags/Heat Number']

tagVals = [tag.value for tag in system.tag.readBlocking(tagsToRead)]

pot_value = tagVals[0]
heat_number = tagVals[1]

if pot_value == 0:

    system.tag.writeBlocking(['[default]PredictionTabTags/Heat Number'],[(heat_number + 1)])

Also, no need to specify "value" in your write call.

The problem isn't using ChatGPT (or copilot), it's not knowing enough about the language your using and/or the application your are writing the script for, to know when you're being led astray.

For instance, take this snippet from @Majid_Zamani's earlier post.

pot_value = (self.parent.parent.getChild("Settings").getChild("Parameters_Table").props.data[0].Power_On_Time)
	if pot_value == 0:
		
		heat_number = int("[default]PredictionTabTags/Heat Number")
		
		# Increment the Heat_Number tag by 1
		new_heat_number = heat_number + 1
		import time
		time.sleep(2)		  		        
		
		system.tag.writeBlocking("[default]PredictionTabTags/Heat Number.value", new_heat_number)

If ChatGPT spat out that script and you didn't know Python/Jython, then you wouldn't be able to spot common pitfalls (like importing a library inside of a function) or obvious errors like attempting an Integer conversion on a string.

Maybe you know those things and you can instruct the GPT system that its incorrect, then the resultant script might look like it should work, but without knowing the Ignition Environment you wouldn't know that time.sleep() is evil, or that the string which was erroniusly being converted to an integer should have really been a Tag Read.

If you know enough about the language and the environment to properly vet any script that GPT spits out, then go right ahead, if you're just starting on either of those, then you should avoid it.

2 Likes