How to change the value of a component using script

Hi there

Am new to Ignition

Trying to write a script in a button on the main window.
Whenever click on this button, it will check another component(Eg a Cylindrical Tank) in another window(tanks).
If the tank is filled with water(Eg water volume is some value), then it will try to set its value to 0.

Am able to get the value of the tank.
However, i couldnt find anything or command to set the component to 0.
As there isnt such command: window.rootContainer.setComponent(“tank1”).value = 0

Also i search through the forum with the above topic/title keywords but it didnt find anything related. Thus writing here for any advise or assistance.

Below is my script, it gives error of
java.lang.NullPointerException: java.lang.NullPointerException
caused by NullPointerException

###################################################################
import sys
window = system.gui.getWindow(“tanks”)
print window.rootContainer.getComponent(“tank1”).value
event.source.setPropertyValue(“tank1”, “0”)

###################################################################

thank you

First, here’s what you’re asking for-- the main gotcha is straight from the scripting docs – if the window is not open, you’ll get an error because you can only access open windows. The other gotcha – is maybe your tank isn’t in the rootContainer because you’ve moved it into some sort of panel/container and now your button breaks without you realizing.

Component & Window Scripting

# Start the try block in case the window is not open.
try:
    # Grab the window reference and assign it to the variable window.
    window = system.gui.getWindow("tanks")
    window.getRootContainer().getComponent("tank1").value = 0
 
# Handle the exception by opening an informative error.
except ValueError:
    system.gui.errorBox("The window is not open!", "Error")

Next, you might be going about this from the wrong angle. So here’s some assumptions:

  1. Your tank is(or should be) bound to a Tag representing tank level that is pulling from some hardware or simulated in software.
  2. You want to be able to reset tank level whether or not the window is active(via script, timer, or whatever)

In that case you’d just set the value of the tag that the tank value is bound to like:

system.tag.write("Tanks/tank1",0)
2 Likes

Dear Phillip

Thank you very much for your insightful and comprehensive advises and great assistance.

Yes, you are absolutely right on the dot.
When I run the script trying to access the window that is not opened, I encountered error .
So far, didnt encounter the other error as i am not yet familiar, so i didnt move the component(tank1) around but it is still residing in the root container i suppose . But definitely noted the point that you highlighted in advance.

Next, it is most grateful that you are so sharp to point out those assumptions and that i am coming from the wrong angle.

Right now, i am just trying out, so i just manually key in some values for those tanks initially and try to see how to reset it.

Also, I am still figuring out whether to get the tanks’ values from the hardware directly or from the database(mysql).

As of now, I dont have any hardware connected to Ignition yet, so those tanks components in Ignition are not bounded to any tag yet.

However, in time to come, the actual implementation is that all the tanks(around 20 of them) would be connected to the sensors and that Ignition need to be able to read the values of those sensors and then able to reset those values. Yes, whether or not the window is active, I would need to be able to reset the tanks’ level.

Thank you

For the time being, as I dont have any tag tagged to the tanks’ levels, I just try out the codes above for the “try and except ValueError:”

However, after executing the script, there is an error for the line :
window.getRootContainer().getComponent(“tank1”).value = 0

The error message is “AttributeError: read-only attr: value”

Wonder how to overcome the above error or what other way could change the value of the component besides tagging it?

Thanks in advance for any advise or assistance

Thank you

Tried tagging the tank1 to a memory tag

Was able to set the value of the tag that the tank value is bound to using :

system.tag.write(“Tanks/tank1”,0)

perhaps this may be a more correct approach as pointed out above

thank you

1 Like