Using system.tag.readBlocking and turning into string

So I’ve taken a piece of data from my tag through using system.tag.readBlocking. It comes out as [“data”], but I need to concatenate the actual word data with a string, so I used str() on the array.

It returns the entire tag value, quality and time and still outputs as an array. I’ve tried pulling the first value out however it does not work. Additionally, using the .value on the system.tag.readBlocking output does not pull only the value out either.

The system.dataset.toDataSet function does not work either unfortunately.

Is there a way to convert the actual value into a string?

I suggest another read of the manual. system.tag.readBlocking returns a list of objects. You will need to get the value attribute of whatever object in your list you are wanting to use. The manual includes examples that show you how to do this.

I’ve solved this through using the str() function and then a mid() function - since using str() on the system.tag.readBlocking also gave the quality and time - apologies, I wasn’t clear enough on the actual issue, which was the extra input.

I still think you have something goofy going on. Try something like this in the script console.

tags = ["[default]Tag 1"]
results = system.tag.readBlocking(tags)
print results[0].value
1 Like

I tried this out - this also works thank you. I also will need to change this to a string to manipulate it too. Cheers

Why? Is not already a string?

For some reason, it doesn’t come out as a string - I tested and the only way I could add it to another string was to first use the str() function, even using josborn’s method.

What is the type of the tag?

If your tag type is string you will get a string value. Also please don't do this, that makes me cringe.

1 Like

Its a list type with a value, quality and timestamp

Check here:

Let’s start from first principles.

When you use system.tag.readBlocking, the return value is a list of QualifiedValue objects.

To extract the first element of the list, use subscript 0 to retrieve the first element: qualifiedValue = listOfValues[0]. Now you have a QualifiedValue object. To retrieve the actual value, which can be any type, use getValue(), or, better, .value: actualValue = qualifiedValue.value.
The type of value at this point will be whatever the tag’s type is, per @josborn’s post above.
If your tag is not a string, and you actually need to do a string formatting operation, you can ask it for its string representation using the Python builtin str function. This is a lossy operation - the string representation of an object is not guaranteed to and indeed almost never does encode as much information as the actual type.

You should drive logic from the string representation of a value approximately never. For presentation, sure, it’s fine, although if you have a richer type you might still lose information (even a simple floating point might not be represented the way you want it to be if you simply call str).


Just to summarize the above and provide a 1-line answer for your script, to get your the current value out of a single tag you would use:

myValue = system.tag.readBlocking([myTagPath])[0].value

if the tag is not a string type you would change it to:

myValue = str(system.tag.readBlocking([myTagPath])[0].value)

but always remember the lossy aspect of this that Mr. Griffith mentions.

1 Like