# Scripting problems

**URL:** <https://forum.inductiveautomation.com/t/scripting-problems/14785>\
**Category:** General Discussion\
**Created:** [July 13, 2017, 6:57pm UTC](https://forum.inductiveautomation.com/t/scripting-problems/14785 "2017-07-13T18:57:11Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![jenkinsbradm](https://avatars.discourse-cdn.com/v4/letter/j/ed8c4c/32.png) [@jenkinsbradm](https://forum.inductiveautomation.com/u/jenkinsbradm)\
**Post date:** [July 13, 2017, 6:57pm UTC](https://forum.inductiveautomation.com/t/scripting-problems/14785/1 "2017-07-13T18:57:11Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Sanderd17](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/sanderd17/32/8735_2.png) [@Sanderd17](https://forum.inductiveautomation.com/u/Sanderd17)\
**Post date:** [July 13, 2017, 7:17pm UTC](https://forum.inductiveautomation.com/t/scripting-problems/14785/2 "2017-07-13T19:17:26Z")

</div>

Rule 1: name your variables 😉  
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](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

---

<div class="post-metadata">

**Author:** ![jenkinsbradm](https://avatars.discourse-cdn.com/v4/letter/j/ed8c4c/32.png) [@jenkinsbradm](https://forum.inductiveautomation.com/u/jenkinsbradm)\
**Post date:** [July 13, 2017, 7:38pm UTC](https://forum.inductiveautomation.com/t/scripting-problems/14785/3 "2017-07-13T19:38:16Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![JordanCClark](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jordancclark/32/83362_2.png) [@JordanCClark](https://forum.inductiveautomation.com/u/JordanCClark)\
**Post date:** [July 13, 2017, 8:41pm UTC](https://forum.inductiveautomation.com/t/scripting-problems/14785/4 "2017-07-13T20:41:57Z")

</div>

Hi Brad,

> [@jenkinsbradm](#):
>
> 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.

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

---

<div class="post-metadata">

**Author:** ![jenkinsbradm](https://avatars.discourse-cdn.com/v4/letter/j/ed8c4c/32.png) [@jenkinsbradm](https://forum.inductiveautomation.com/u/jenkinsbradm)\
**Post date:** [July 13, 2017, 9:23pm UTC](https://forum.inductiveautomation.com/t/scripting-problems/14785/5 "2017-07-13T21:23:52Z")

</div>

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

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

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [July 13, 2017, 9:42pm UTC](https://forum.inductiveautomation.com/t/scripting-problems/14785/6 "2017-07-13T21:42:13Z")

</div>

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.

```auto
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)

```

---

<div class="post-metadata">

**Author:** ![Sanderd17](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/sanderd17/32/8735_2.png) [@Sanderd17](https://forum.inductiveautomation.com/u/Sanderd17)\
**Post date:** [July 14, 2017, 6:37am UTC](https://forum.inductiveautomation.com/t/scripting-problems/14785/7 "2017-07-14T06:37:00Z")

</div>

> [@jenkinsbradm](#):
>
> 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.

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)`.

---

<div class="post-metadata">

**Author:** ![jenkinsbradm](https://avatars.discourse-cdn.com/v4/letter/j/ed8c4c/32.png) [@jenkinsbradm](https://forum.inductiveautomation.com/u/jenkinsbradm)\
**Post date:** [July 17, 2017, 12:36am UTC](https://forum.inductiveautomation.com/t/scripting-problems/14785/8 "2017-07-17T00:36:33Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Caleb\_Bohon](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/caleb_bohon/32/3293_2.png) [@Caleb\_Bohon](https://forum.inductiveautomation.com/u/Caleb_Bohon)\
**Post date:** [July 24, 2017, 4:25pm UTC](https://forum.inductiveautomation.com/t/scripting-problems/14785/9 "2017-07-24T16:25:08Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![jenkinsbradm](https://avatars.discourse-cdn.com/v4/letter/j/ed8c4c/32.png) [@jenkinsbradm](https://forum.inductiveautomation.com/u/jenkinsbradm)\
**Post date:** [August 1, 2017, 4:41am UTC](https://forum.inductiveautomation.com/t/scripting-problems/14785/10 "2017-08-01T04:41:23Z")

</div>

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
