Writing to a Tag from a Webservice

we are having issues getting a Tag to be written from data pulled in from a web service,(“we are the Consumer”) we are getting the Data from the web-service but having issues writing one value to a tag.
can anyone post some Python Code or script of how they accomplish this

thanks so much

,

I think you’ll need to provide more information about what problems you’re having.

Ignition version? Error messages? Quality code returned by the write? Are you logging the data as you pull it to confirm it is what you think it is?

There’s not much of an example needed to just call system.tag.writeBlocking beyond what’s already in the user manual.

#-----------------------(code-Snippet)-------------------------------------
wsVariable = system.ws.runWebService(“GetCurrentProduct”,
{

},
None,
None)

Product = wsVariable.getChild(‘GetCurrentProductResult’).toDict()[‘GetCurrentProductResult’]

for Item in Product:
for Name in Item:
if Item[Name] is not None:
system.tag.write(“Product/” + str(Name), Item[Name])
#print “Product/” + str(Name)
#print Item[Name]
#print Name

Most likely just missing the tag provider for the write call.
system.tag.write("[default]Product/"+ str(Name),str(Item[Name]))

Replace [default] if you have a different tag provider name.

As @MMaynard says, probably just missing the tag provider.

Though I would probably write this in such a way that only one write function is called.

For 7.9:

#if you want a one liner and don't care about optimization that much
paths, values = zip(*[('[default]Product/' + str(name),item[name]) for item in product for name in item if item[name] is not None])
system.tag.writeAll(list(paths),list(values))

#if you would rather have something a litle simpliar
paths = []
values = []
for Item in Product:
    for Name in Item:
        if Item[Name] is not None:
            paths.append('[default]Product/' + str(Name)
            values.append(Item[name])

system.tag.writeAll(paths,values)

Or for 8.* use the system.tag.writeBlocking()

system.tag.writeBlocking(paths,values)

Writing all of the tags with one call will fairly significantly speed up this bit of code, unless you only have 1 or 2 values that are being written as a result of this.

Also if you are using 8.* switching to writeBlocking will future proof your code as system.tag.write has been depricated.

Just my 2 cents

I tested this I think it was with 400 writes. Writing all at once took something like 1.4% of the time it took to write 5 at a time (I was proving to someone else how much faster their code would be if they switched, so I didn't test writing tags singularly)

I knew this was the case, but I hadn't ever really quantified it, and your post made me curious, so I did this.

NOTE: This was in 7.9 script console

from java.lang import System

tagPaths = []
ret = 0
currentTime = system.date.now()

for i in range(400):
	tagPaths.append('[Client]CurrentTime')
	

start = System.nanoTime()

for i in range(400):
	ret = system.tag.write(tagPaths[i],currentTime)
	
end = System.nanoTime()

print '400 singular writes took %.3f ms' % ((end-start) / 1000000.0)

values = []
paths = []

start = System.nanoTime()

for i in range(400):
	values.append(currentTime)
	paths.append('[Client]CurrentTime')
	if i % 5 == 0:
		ret = system.tag.writeAll(paths,values)
		values = []
		paths = []

end = System.nanoTime()

print '400 writes in blocks of 5 took %.3f ms' % ((end-start) / 1000000.0)

values=[]
paths = []

start = System.nanoTime()

for i in range(400):
	values.append(currentTime)
	paths.append('[Client]CurrentTime')
ret = system.tag.writeAll(paths,values)

end = System.nanoTime()

print '400 writes in 1 block took %.3f ms' % ((end-start) / 1000000.0)

and here are the results

400 singular writes took 6.925 ms
400 writes in blocks of 5 took 2.864 ms
400 writes in 1 block took 0.879 ms

So writing 400 tags at once took 12.7% of the time it took to write 400 tags singularly and 3.1% of the time it took to write 5 at a time.