# Using Substring in the Change Value Script

**URL:** <https://forum.inductiveautomation.com/t/using-substring-in-the-change-value-script/65303>\
**Category:** Ignition\
**Created:** [October 17, 2022, 7:08pm UTC](https://forum.inductiveautomation.com/t/using-substring-in-the-change-value-script/65303 "2022-10-17T19:08:36Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![John\_Marin](https://avatars.discourse-cdn.com/v4/letter/j/aca169/32.png) [@John\_Marin](https://forum.inductiveautomation.com/u/John_Marin)\
**Post date:** [October 17, 2022, 7:08pm UTC](https://forum.inductiveautomation.com/t/using-substring-in-the-change-value-script/65303/1 "2022-10-17T19:08:36Z")

</div>

Hello. I am trying to take the Substring of test2 and write that value to test3 but only when the value of tag1 changes. I tried in the Change Value tag script using the following:

if previousValue.value != currentValue.value:

```
	system.tag.writeBlocking("[~]Test2", currentValue.value)
	system.tag.writeBlocking("[~]Test3", substring("[~]Test2", 0, 2))

```

---

<div class="post-metadata">

**Author:** ![pascal.fragnoud](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pascal.fragnoud/32/24728_2.png) [@pascal.fragnoud](https://forum.inductiveautomation.com/u/pascal.fragnoud)\
**Post date:** [October 17, 2022, 7:13pm UTC](https://forum.inductiveautomation.com/t/using-substring-in-the-change-value-script/65303/2 "2022-10-17T19:13:04Z")

</div>

And ? What happens ?

ps: substring is not a python function

---

<div class="post-metadata">

**Author:** ![John\_Marin](https://avatars.discourse-cdn.com/v4/letter/j/aca169/32.png) [@John\_Marin](https://forum.inductiveautomation.com/u/John_Marin)\
**Post date:** [October 17, 2022, 7:13pm UTC](https://forum.inductiveautomation.com/t/using-substring-in-the-change-value-script/65303/3 "2022-10-17T19:13:49Z")

</div>

The value does not change. I want to pull the first 2 characters from a string tag and write them to a separate tag.

---

<div class="post-metadata">

**Author:** ![John\_Marin](https://avatars.discourse-cdn.com/v4/letter/j/aca169/32.png) [@John\_Marin](https://forum.inductiveautomation.com/u/John_Marin)\
**Post date:** [October 17, 2022, 7:15pm UTC](https://forum.inductiveautomation.com/t/using-substring-in-the-change-value-script/65303/4 "2022-10-17T19:15:15Z")

</div>

It makes sense if substring is not valid in script. New to ignition

---

<div class="post-metadata">

**Author:** ![pascal.fragnoud](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pascal.fragnoud/32/24728_2.png) [@pascal.fragnoud](https://forum.inductiveautomation.com/u/pascal.fragnoud)\
**Post date:** [October 17, 2022, 7:15pm UTC](https://forum.inductiveautomation.com/t/using-substring-in-the-change-value-script/65303/5 "2022-10-17T19:15:25Z")

</div>

I was trying to hint that when you need help with something that doesn't work, providing the error is a good thing to do.  
I'm willing to bet that if you looked at the logs, you'd see something like `NameError: name "substring" is not defined.`

---

<div class="post-metadata">

**Author:** ![Transistor](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/transistor/32/3380_2.png) [@Transistor](https://forum.inductiveautomation.com/u/Transistor)\
**Post date:** [October 17, 2022, 7:15pm UTC](https://forum.inductiveautomation.com/t/using-substring-in-the-change-value-script/65303/6 "2022-10-17T19:15:50Z")

</div>

I'm not sure if this is the problem, but writeBlocking expects **lists**. The two arguements should be in `[...]`.

```auto
if previousValue.value != currentValue.value:
	system.tag.writeBlocking(["[~]Test2"], [currentValue.value])
	system.tag.writeBlocking(["[~]Test3"], [substring("[~]Test2", 0, 2)])

```

[https://docs.inductiveautomation.com/display/DOC80/system.tag.writeBlocking](https://docs.inductiveautomation.com/display/DOC80/system.tag.writeBlocking)

---

<div class="post-metadata">

**Author:** ![John\_Marin](https://avatars.discourse-cdn.com/v4/letter/j/aca169/32.png) [@John\_Marin](https://forum.inductiveautomation.com/u/John_Marin)\
**Post date:** [October 17, 2022, 7:17pm UTC](https://forum.inductiveautomation.com/t/using-substring-in-the-change-value-script/65303/7 "2022-10-17T19:17:00Z")

</div>

I think the main issue is with the substring instruction. I am able to write the full value to another tag without issue but I only want the first 2 characters. Thanks for the replies, and yes pascal that is the exact error I am getting.

---

<div class="post-metadata">

**Author:** ![pascal.fragnoud](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pascal.fragnoud/32/24728_2.png) [@pascal.fragnoud](https://forum.inductiveautomation.com/u/pascal.fragnoud)\
**Post date:** [October 17, 2022, 7:21pm UTC](https://forum.inductiveautomation.com/t/using-substring-in-the-change-value-script/65303/8 "2022-10-17T19:21:41Z")

</div>

I think the write and read functions actually do accept single tags now.

Though @John_Marin should really be using

```python
system.tag.writeBlocking(
    ["[~]Test2", "[~]Test3"],
    [currentValue.value, X]
)

```

About that X in there. I'm guessing you want the first 2 letters of `Test2`'s value. This, if `substring` worked, would return `[~`, the first the characters of the string you passed it.  
If you need the tag's value, you'll need to read it with `system.tag.readBlocking`

To get a substring in python, use slices.

```python
s = "foobarbaz"
s[:4] == "foo"
s[4:7] == "bar"
s[7:] == "baz"

```

---

<div class="post-metadata">

**Author:** ![John\_Marin](https://avatars.discourse-cdn.com/v4/letter/j/aca169/32.png) [@John\_Marin](https://forum.inductiveautomation.com/u/John_Marin)\
**Post date:** [October 17, 2022, 7:24pm UTC](https://forum.inductiveautomation.com/t/using-substring-in-the-change-value-script/65303/9 "2022-10-17T19:24:50Z")

</div>

Thank you very much! I will try this out.

---

<div class="post-metadata">

**Author:** ![John\_Marin](https://avatars.discourse-cdn.com/v4/letter/j/aca169/32.png) [@John\_Marin](https://forum.inductiveautomation.com/u/John_Marin)\
**Post date:** [October 17, 2022, 7:38pm UTC](https://forum.inductiveautomation.com/t/using-substring-in-the-change-value-script/65303/10 "2022-10-17T19:38:04Z")

</div>

It worked! Here is the final code, it takes the first 2 characters of the string to one tag and the remainder to the rest.

```auto
if previousValue.value != currentValue.value:
		system.tag.writeBlocking(
			["[~]Test2", "[~]Test3"],
			[currentValue.value[0:2], currentValue.value[2:]]
		)

```

Thanks again!

---

<div class="post-metadata">

**Author:** ![Transistor](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/transistor/32/3380_2.png) [@Transistor](https://forum.inductiveautomation.com/u/Transistor)\
**Post date:** [October 17, 2022, 7:39pm UTC](https://forum.inductiveautomation.com/t/using-substring-in-the-change-value-script/65303/11 "2022-10-17T19:39:49Z")

</div>

Tip: use the \</\> code formatting button to preserve code indentation and apply syntax highlighting. It's essential for Python and makes any code much easier to read. There's an edit button (pencil icon) below your post so you can fix it.
