# Get tag name and save it on a variable

**URL:** <https://forum.inductiveautomation.com/t/get-tag-name-and-save-it-on-a-variable/91231>\
**Category:** Ignition\
**Tags:** ignition81\
**Created:** [May 31, 2024, 10:33pm UTC](https://forum.inductiveautomation.com/t/get-tag-name-and-save-it-on-a-variable/91231 "2024-05-31T22:33:04Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Daniel\_Hernandez](https://avatars.discourse-cdn.com/v4/letter/d/2bfe46/32.png) [@Daniel\_Hernandez](https://forum.inductiveautomation.com/u/Daniel_Hernandez)\
**Post date:** [May 31, 2024, 10:33pm UTC](https://forum.inductiveautomation.com/t/get-tag-name-and-save-it-on-a-variable/91231/1 "2024-05-31T22:33:04Z")

</div>

Inside tag scripting menu. I trying to get automatically the name of the tag and save it to a variable, in order to user later to get the value of the tag. And I would like to get the full path of the tag and use later in the readBlocking function. The code that I using is :

```python
# Run the Named Query|
tag = system.tag.readBlocking([[**Sample_Tags]Random/RandomInteger1**])[0]|
# Create a python dictionary of parameters to pass|
parameters = {DataName:RandomInteger1, DataValue:tag.value}|
system.db.runNamedQuery(samplequickstart,UpdateTagValueQuery,parameters)|

```

Actually I putting the path name ("Sample\_Tags]Random/RandomInteger1") manually.  
And I would like to get it in auto.

---

<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:** [May 31, 2024, 10:37pm UTC](https://forum.inductiveautomation.com/t/get-tag-name-and-save-it-on-a-variable/91231/2 "2024-05-31T22:37:35Z")

</div>

Welcome to the forum, Daniel.  
Please have a read of [Wiki - how to post code on this forum](https://forum.inductiveautomation.com/t/wiki-how-to-post-code-on-this-forum/77831) and then hit the pencil icon below your post to edit and fix the code formatting. 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:** [May 31, 2024, 10:57pm UTC](https://forum.inductiveautomation.com/t/get-tag-name-and-save-it-on-a-variable/91231/3 "2024-05-31T22:57:30Z")

</div>

What event are you using to run this code? Because origin information is part of tag events, but format varies with type of event.

---

<div class="post-metadata">

**Author:** ![Daniel\_Hernandez](https://avatars.discourse-cdn.com/v4/letter/d/2bfe46/32.png) [@Daniel\_Hernandez](https://forum.inductiveautomation.com/u/Daniel_Hernandez)\
**Post date:** [June 1, 2024, 9:24pm UTC](https://forum.inductiveautomation.com/t/get-tag-name-and-save-it-on-a-variable/91231/4 "2024-06-01T21:24:14Z")

</div>

Hi. I m using tag value change event. Appreciate your support.

---

<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:** [June 1, 2024, 9:38pm UTC](https://forum.inductiveautomation.com/t/get-tag-name-and-save-it-on-a-variable/91231/5 "2024-06-01T21:38:37Z")

</div>

You didn't quite manage to format the code. (What are all the `|` characters?)

`tag = system.tag.readBlocking([[Sample_Tags]Random/RandomInteger1])[0]`  
The tag name needs to be quoted like this:  
`tag = system.tag.readBlocking(["[Sample_Tags]Random/RandomInteger1"])[0]`

`parameters = {DataName: RandomInteger1, DataValue: tag.value}`  
That looks OK. (Add spaces after punctuation, same as English.)

`system.db.runNamedQuery(samplequickstart, UpdateTagValueQuery, parameters)`  
That looks OK too

* * *

Back to your question:

> I trying to get automatically the name of the tag and save it to a variable ...

1. What is the source of the tag name?
2. Why are you doing it this way? What is the real problem you are trying to solve?

---

<div class="post-metadata">

**Author:** ![lrose](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/lrose/32/62724_2.png) [@lrose](https://forum.inductiveautomation.com/u/lrose)\
**Post date:** [June 2, 2024, 12:33am UTC](https://forum.inductiveautomation.com/t/get-tag-name-and-save-it-on-a-variable/91231/6 "2024-06-02T00:33:47Z")

</div>

> [@Transistor](#):
>
> tag = system.tag.readBlocking(["Sample\_Tags]Random/RandomInteger1"])[0]

Your '”' and '[' are swapped at the beginning of the provider.

```python
system.tag.readBlocking(["[Sample_Tags]Random/RandomInteger1"])[0]

```

---

<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:** [June 2, 2024, 12:54am UTC](https://forum.inductiveautomation.com/t/get-tag-name-and-save-it-on-a-variable/91231/7 "2024-06-02T00:54:05Z")

</div>

It was actually missing a `[`.  
Fixed, thanks!

---

<div class="post-metadata">

**Author:** ![nminchin](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/nminchin/32/6911_2.png) [@nminchin](https://forum.inductiveautomation.com/u/nminchin)\
**Post date:** [June 2, 2024, 7:58am UTC](https://forum.inductiveautomation.com/t/get-tag-name-and-save-it-on-a-variable/91231/8 "2024-06-02T07:58:20Z")

</div>

> [@Transistor](#):
>
> parameters = {DataName: RandomInteger1, DataValue: tag.value}

This is wrong too unless there are DataName, RandomInteger1, and DataValue variables defined further up in the script. These need to be in quotes otherwise.

---

<div class="post-metadata">

**Author:** ![Daniel\_Hernandez](https://avatars.discourse-cdn.com/v4/letter/d/2bfe46/32.png) [@Daniel\_Hernandez](https://forum.inductiveautomation.com/u/Daniel_Hernandez)\
**Post date:** [June 2, 2024, 1:38pm UTC](https://forum.inductiveautomation.com/t/get-tag-name-and-save-it-on-a-variable/91231/9 "2024-06-02T13:38:12Z")

</div>

Apprecite your support.  
Here the response to your questions.  
1.- Actually the source ot the tag is a memory tag, saved in a specific folder.  
2.- The main problem is that I need to determinate the tag name automatically. I know, if I use the change value trigger, I need to put the tag name to get the value. But, I need that inside the script, use a function o something to get the tag name ("RandomInteger1"), not put it manually as string. Regards

---

<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:** [June 2, 2024, 1:41pm UTC](https://forum.inductiveautomation.com/t/get-tag-name-and-save-it-on-a-variable/91231/10 "2024-06-02T13:41:05Z")

</div>

> [@Daniel\_Hernandez](#):
>
> The main problem is ...

No, you're describing your solution again. That's not the original problem. I suspect that you've gone down the wrong path.

---

<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:** [June 2, 2024, 2:29pm UTC](https://forum.inductiveautomation.com/t/get-tag-name-and-save-it-on-a-variable/91231/11 "2024-06-02T14:29:33Z")

</div>

> [@Daniel\_Hernandez](#):
>
> if I use the change value trigger, I need to put the tag name to get the value

The value change event's `event.tagPath` contains the full path/name of the subject tag. So, no, you do not need the tag's own name in your script.

Also, tag event scripts run in a context where `system.tag.readBlocking()` and `system.tag.writeBlocking()` understand tag names of the form `[.]someName` as references to **sibling** tags in the same folder as the subject tag.

So the answers to both of your original questions are right there in the `event` object.

> **[Tag Event Scripts | Ignition User Manual](https://www.docs.inductiveautomation.com/docs/8.1/platform/tags/tag-event-scripts)**
>
> Scripts can be attached to Tags. When you edit a Tag, you can navigate to the Tag Events section and click on the Edit icon to see a list of all of the available events. Those events are

> **[Gateway Event Scripts | Ignition User Manual](https://www.docs.inductiveautomation.com/docs/8.1/platform/scripting/scripting-in-ignition/gateway-event-scripts#tag-change-scripts)**
>
> Gateway Event Scripts Overview
