Scripting problems

Thanks for the help guys.
I’m making pretty good progress learning this for school . Its taking me awhile to get this stuff. Now I’m having issues with scripting. I want to have a tank and a 2 state toggle tied to an alarm. When the toggle is pressed I want the tank to fill and when its off I want the tank to stop filling and hold its current value. If the tank gets to 75 percent I’d like a led or light to come on.
I was thinking:

to fill the tank

x = tank
y = bool
if y == True:
system.tag.write(x)
x = x+1
else:
system.tag.write(x)
x = x

to set off the led

z = led
x = tank

If x >=75:
system.tag.write(z)
z==True
else:
system.tag.write(z)
z== False

I’m curious as to what I did wrong with my code… can I please get some assistance?
Thank you in advance.
respectfully,
brad

Rule 1: name your variables :wink:
x, y, and z are fine when you need to keep track of a handful of values. For anything more, you need names like “isFilling”, “fillPct”, …

Then the lines x = tank and y = bool make no sense in python. You try to assign the value of tank to x, but unless tank is defined before, this will simply fail. The y = bool assignment will work, but it won’t do what you expect. It will make y a datatype itself, instead of a variable of a certain datatype.

What you need to do is initialising your variables, so y = True or y = False in the case above. Probably not hardcoded, but depending on an Ignition tag. So that would be y = system.tag.read("tagPath").value (see the documenation about the system.tag methods: https://support.inductiveautomation.com/usermanuals/ignition/index.html?system_tag_read.htm).

Another issue is that when writing a tag, you need to give 2 arguments: where to write to (the name of the tag) and what value to write. So you need to add a tag path to your methods like system.tag.write("tagPath", x)

That should probably fix your code, but you still need to make sure it’s executed at the right time. The first part should probably be executed on a timer (so it fills in small steps with a time delay between the steps), while the second one should probably be executed on a tag trigger when the contents of the tank changes

My brain slightly broke when reading your post.
I understood that instead of tank, bool and led I need to use a tagpath
but I was confused by :
"[quote=“Sanderd17, post:2, topic:14785”]
Another issue is that when writing a tag, you need to give 2 arguments: where to write to (the name of the tag) and what value to write. So you need to add a tag path to your methods like system.tag.write(“tagPath”, x)
[/quote]

Are you saying that to write for instance to a writeable integer it would have to be
if y == True:
system.tag.write(writeableInteger1,x)
x = x+1
else:
system.tag.write(writeableInteger1,x)
x = x

I think im confused because I declared the variables at the top of the screen. Do you need to do it twice or did I mess something else up.

Thank you by the way for your quick response. I apologize that my questions aren’t the most advanced but I really do want to learn this software.
Respectfully,
Brad

Hi Brad,

What @Sanderd17 is saying is that, for example, your first line says x=tank. The issue here is that if tank isn't defined earlier in the script, you will get an error. Is there more code we're not seeing?

Regards,
Jordan

tank = “[.]WriteableInteger1”
button = “[.]WriteableBoolean2”

if button == True:
	system.tag.write("[.]WriteableInteger1",x)
	x = x+1
else:
	system.tag.write("[.]WriteableInteger1",x)
	x = x

Getting there. Right now, tank and button are just static strings, though - they don’t represent the actual tags or the values. Also, you don’t need an else on the statement if you’re not actually doing anything.

tank = "[.]WriteableInteger1"
button = system.tag.read("[.]WriteableBoolean2")

if button.value == True:
	currentValue = system.tag.read(tank)
	newValue = currentValue.value + 1
	system.tag.write(tank, newValue)

Variables declared in python are one-time use, every time you click the button, the entire script will be ran and the value of variables you initialise like x = 5 will be reset to it's original value (5 in this case).

What you need to do is initialise a variable from an Ignition tag. Ignition uses tags as permanent memory, it will even be remembered between system reboots. So you get the latest value from the permanent memory: x = system.tag.read(...).value, you modify it in your script, and then you write the modified back to the permanent memory: system.tag.write(..., x).

nice.
I was trying to get there not sure what I’m doing wrong. I appreciate the help.
How do i tie these into the timer so that it has a refresh rate ?
my idea is to do something like this
if timer.value == value & system.tag.read(“writeableBoolean2”).value==True:
system.tag.write(“writeableInteger1”,newValue)
newValue == “writeableInteger1” + 1

I’m assuming that you are using a Timer component.

If so, you can use the actionPerformed script. Put the code you want to execute there. The documentation is a little off. It will run your script every time the timer increments. Change the increment value to get longer or shorter times.

thanks.
I figured that out but it took a while.
Then the actual ignition gurus on here showed me another way. … This is a very convoluted and cool software