# Indirect OPC Tag Reading

**URL:** <https://forum.inductiveautomation.com/t/indirect-opc-tag-reading/96373>\
**Category:** Ignition\
**Tags:** scripting\
**Created:** [September 24, 2024, 2:22pm UTC](https://forum.inductiveautomation.com/t/indirect-opc-tag-reading/96373 "2024-09-24T14:22:53Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![Joe\_Alcide](https://avatars.discourse-cdn.com/v4/letter/j/b5a626/32.png) [@Joe\_Alcide](https://forum.inductiveautomation.com/u/Joe_Alcide)\
**Post date:** [September 24, 2024, 2:22pm UTC](https://forum.inductiveautomation.com/t/indirect-opc-tag-reading/96373/1 "2024-09-24T14:22:53Z")

</div>

Hello!

I would like to be able to indirection in an OPC Tag, is there any way to do this? I have another OPC tag that is reading a tag path across the OPC to Ignition (XX\AT\_P7), I want to then be able to have another OPC tag in Ignition in which I can use that tag path and read a parameter from that tag. Any ideas? Any help would be appreciated!

I also have a dumber question. If that the path on that other OPC tag is XX\AT\_P7, how can I remove the XX. I tried using the Replace function in an expression tag and couldn't get it to work.

Thanks!

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [September 24, 2024, 2:26pm UTC](https://forum.inductiveautomation.com/t/indirect-opc-tag-reading/96373/2 "2024-09-24T14:26:13Z")

</div>

Not easily, no. The simplest solution would be to use a tag `valueChange` event that constructs the correct path from the updated value of the path tag, and writes to the `.opcItemPath` configuration property of the indirect tag.

If you want the fastest possible behavior, subscribe to all of the possible OPC items as OPC tags, and use a reference tag. Then have the `valueChange` event write to the reference tag's `.source` configuration property.

---

<div class="post-metadata">

**Author:** ![michael.flagler](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/michael.flagler/32/26272_2.png) [@michael.flagler](https://forum.inductiveautomation.com/u/michael.flagler)\
**Post date:** [September 24, 2024, 2:28pm UTC](https://forum.inductiveautomation.com/t/indirect-opc-tag-reading/96373/3 "2024-09-24T14:28:45Z")

</div>

You could do what @pturmel suggested, or in a very similar fashion, if you have a parameter on the 2nd tag that you use in the expression for the OPC item path, you could write to the parameter the new value and the OPC item would rebind to the new path also. Both are accomplishing the same thing, just 2 slightly different ways about it.

If you're needing to do a one-shot read of an OPC tag that is based on the value in your first tag, there's always the `system.opc.readValue` scripting function:

> **[system.opc.readValue | Ignition User Manual](https://www.docs.inductiveautomation.com/docs/8.1/appendix/scripting-functions/system-opc/system-opc-readValue)**
>
> Reads a single value directly from an OPC server connection.

---

<div class="post-metadata">

**Author:** ![Joe\_Alcide](https://avatars.discourse-cdn.com/v4/letter/j/b5a626/32.png) [@Joe\_Alcide](https://forum.inductiveautomation.com/u/Joe_Alcide)\
**Post date:** [September 24, 2024, 2:41pm UTC](https://forum.inductiveautomation.com/t/indirect-opc-tag-reading/96373/5 "2024-09-24T14:41:12Z")

</div>

Are you saying I could just do something like:

```tgpath
system.opc.readValue(tgpath.S1CURSTS)

''' S1CURSTS is the parameter I need to pull

```

---

<div class="post-metadata">

**Author:** ![Joe\_Alcide](https://avatars.discourse-cdn.com/v4/letter/j/b5a626/32.png) [@Joe\_Alcide](https://forum.inductiveautomation.com/u/Joe_Alcide)\
**Post date:** [September 24, 2024, 2:50pm UTC](https://forum.inductiveautomation.com/t/indirect-opc-tag-reading/96373/6 "2024-09-24T14:50:36Z")

</div>

> [@pturmel](#):
>
> .opcItemPath

Should this work?

```python
def valueChanged(tag, tagPath, previousValue, currentValue, initialChange, missedEvents):
	CurVal = currentValue.value
	newpath = CurVal.S1CURSTS
	system.tag.writeBlocking("[.]Line 1 Pump 1 Status.OpcItemPath", newpath)

```

---

<div class="post-metadata">

**Author:** ![michael.flagler](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/michael.flagler/32/26272_2.png) [@michael.flagler](https://forum.inductiveautomation.com/u/michael.flagler)\
**Post date:** [September 24, 2024, 3:01pm UTC](https://forum.inductiveautomation.com/t/indirect-opc-tag-reading/96373/7 "2024-09-24T15:01:57Z")

</div>

First off, you should be using the preformatted text to paste your code (go back and edit your posts) to make it more readable.

Most likely your value you get from your first tag isn't going to be a correctly formatted OPC item path, but could be a tag name or an index value of an array. Let's say for fun that it returns the name of the base tag in the PLC that you need to get the value from, your script to update the OPC Item Path parameter on the second tag would look something like this:

```python
def valueChanged(tag, tagPath, previousValue, currentValue, initialChange, missedEvents):
	CurVal = currentValue.value
	newpath = "ns=1;s=[Device]%s.S1CURSTS" % CurVal
	system.tag.writeBlocking(["[.]Line 1 Pump 1 Status.OpcItemPath"], [newpath])

```

Edit: Updated the preformatted text to use tabs instead of spaces!

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [September 24, 2024, 3:19pm UTC](https://forum.inductiveautomation.com/t/indirect-opc-tag-reading/96373/10 "2024-09-24T15:19:12Z")

</div>

Tabs versus spaces matter. Michael's snippet uses spaces. (Bad Michael!)

---

<div class="post-metadata">

**Author:** ![Joe\_Alcide](https://avatars.discourse-cdn.com/v4/letter/j/b5a626/32.png) [@Joe\_Alcide](https://forum.inductiveautomation.com/u/Joe_Alcide)\
**Post date:** [September 24, 2024, 3:21pm UTC](https://forum.inductiveautomation.com/t/indirect-opc-tag-reading/96373/11 "2024-09-24T15:21:30Z")

</div>

It worked! Thank y'all so much! Appreciate the help!

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [September 24, 2024, 3:29pm UTC](https://forum.inductiveautomation.com/t/indirect-opc-tag-reading/96373/12 "2024-09-24T15:29:07Z")

</div>

Please don't delete comments that provide context for later comments.

---

<div class="post-metadata">

**Author:** ![Joe\_Alcide](https://avatars.discourse-cdn.com/v4/letter/j/b5a626/32.png) [@Joe\_Alcide](https://forum.inductiveautomation.com/u/Joe_Alcide)\
**Post date:** [September 24, 2024, 3:33pm UTC](https://forum.inductiveautomation.com/t/indirect-opc-tag-reading/96373/13 "2024-09-24T15:33:17Z")

</div>

Hi! I'm still trying to modify a tag name I'm pulling from an OPC Tag. The Tag returns the string "XX\AT\_P7". I want to make a new expression tag that just keeps the "AT\_P7". I tried using the replace function but I keep getting errors, any help would be appreciated, thanks!

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [September 24, 2024, 3:35pm UTC](https://forum.inductiveautomation.com/t/indirect-opc-tag-reading/96373/14 "2024-09-24T15:35:13Z")

</div>

If it is always just the three characters, I'd use python's slice notation to omit the first three characters. Something like this:

```python
clippedPath = currentValue.value[3:]

```

Then use that in the value you write.

---

<div class="post-metadata">

**Author:** ![Joe\_Alcide](https://avatars.discourse-cdn.com/v4/letter/j/b5a626/32.png) [@Joe\_Alcide](https://forum.inductiveautomation.com/u/Joe_Alcide)\
**Post date:** [September 24, 2024, 3:36pm UTC](https://forum.inductiveautomation.com/t/indirect-opc-tag-reading/96373/15 "2024-09-24T15:36:27Z")

</div>

It's in the expression tag script, not the tag event script

```python
n=trim({[.]Line 1 Pump.value})
replace(n,"PD\","")

```

Sorry, new to Ignition.

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [September 24, 2024, 3:39pm UTC](https://forum.inductiveautomation.com/t/indirect-opc-tag-reading/96373/16 "2024-09-24T15:39:41Z")

</div>

You cannot use assignment in expressions. They aren't scripts, they are **just** expressions, and **not** python.

But consider using the `substring()` expression function, not `replace()`.

Doing this in the OPC tag that supplies the raw indirection path will reduce the number of tags required to implement this. I'd call that a win.

---

<div class="post-metadata">

**Author:** ![michael.flagler](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/michael.flagler/32/26272_2.png) [@michael.flagler](https://forum.inductiveautomation.com/u/michael.flagler)\
**Post date:** [September 24, 2024, 3:46pm UTC](https://forum.inductiveautomation.com/t/indirect-opc-tag-reading/96373/17 "2024-09-24T15:46:03Z")

</div>

Hey! I use tabs....but when I'm writing code directly in the pre-formatted text it wouldn't let me tab it over, so used spaces instead. (I guess I could have copy/pasted to a text editor then put it back, but was taking a shortcut).

(I went back and fixed it to use tabs)
