# How to change the value of a component using script

**URL:** <https://forum.inductiveautomation.com/t/how-to-change-the-value-of-a-component-using-script/17477>\
**Category:** Ignition\
**Created:** [March 23, 2018, 2:38pm UTC](https://forum.inductiveautomation.com/t/how-to-change-the-value-of-a-component-using-script/17477 "2018-03-23T14:38:24Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![mic](https://avatars.discourse-cdn.com/v4/letter/m/e0b2c6/32.png) [@mic](https://forum.inductiveautomation.com/u/mic)\
**Post date:** [March 23, 2018, 2:38pm UTC](https://forum.inductiveautomation.com/t/how-to-change-the-value-of-a-component-using-script/17477/1 "2018-03-23T14:38:24Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![phillip.bow](https://avatars.discourse-cdn.com/v4/letter/p/ce73a5/32.png) [@phillip.bow](https://forum.inductiveautomation.com/u/phillip.bow)\
**Post date:** [March 23, 2018, 2:57pm UTC](https://forum.inductiveautomation.com/t/how-to-change-the-value-of-a-component-using-script/17477/2 "2018-03-23T14:57:10Z")

</div>

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](https://docs.inductiveautomation.com/display/DOC79/Component+and+Window+Scripting)

```auto
# 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:

```auto
system.tag.write("Tanks/tank1",0)

```

---

<div class="post-metadata">

**Author:** ![mic](https://avatars.discourse-cdn.com/v4/letter/m/e0b2c6/32.png) [@mic](https://forum.inductiveautomation.com/u/mic)\
**Post date:** [March 25, 2018, 12:49pm UTC](https://forum.inductiveautomation.com/t/how-to-change-the-value-of-a-component-using-script/17477/3 "2018-03-25T12:49:45Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![mic](https://avatars.discourse-cdn.com/v4/letter/m/e0b2c6/32.png) [@mic](https://forum.inductiveautomation.com/u/mic)\
**Post date:** [March 26, 2018, 1:01am UTC](https://forum.inductiveautomation.com/t/how-to-change-the-value-of-a-component-using-script/17477/4 "2018-03-26T01:01:15Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![mic](https://avatars.discourse-cdn.com/v4/letter/m/e0b2c6/32.png) [@mic](https://forum.inductiveautomation.com/u/mic)\
**Post date:** [March 26, 2018, 2:54am UTC](https://forum.inductiveautomation.com/t/how-to-change-the-value-of-a-component-using-script/17477/5 "2018-03-26T02:54:03Z")

</div>

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
