Get tag name and save it on a variable

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 :

# 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.

Welcome to the forum, Daniel.
Please have a read of Wiki - how to post code on this forum and then hit the pencil icon below your post to edit and fix the code formatting. Thanks.

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

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

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?

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

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

It was actually missing a [.
Fixed, thanks!

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.

2 Likes

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

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

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.

1 Like