Using Substring in the Change Value Script

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

And ? What happens ?

ps: substring is not a python function

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

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

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.

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

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

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.

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

Though @John_Marin should really be using

    ["[~]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.

s = "foobarbaz"
s[:4] == "foo"
s[4:7] == "bar"
s[7:] == "baz"
1 Like

Thank you very much! I will try this out.

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.

if previousValue.value != currentValue.value:
			["[~]Test2", "[~]Test3"],
			[currentValue.value[0:2], currentValue.value[2:]]

Thanks again!

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.

1 Like