I think the general consensus around here is to avoid time.sleep() whenever possible. Try replacing it with system.util.invokeLater or system.util.invokeAsynchronous. I think usually invokeLater is reserved for GUI interactions so I think invokeAsynchronous is what you should try.
EDIT : Evidently system.util.invokeLater is not available in tag scripts as @bakarabinchak.psi mentioned*
Use a tag event script on value change of your AUTO tag. When the AUTO tag goes true, run your query as a named query then write the results to a memory tag instead of using a query tag.
if currentValue.value:
def runQuery():
results = system.db.runNamedQuery(myQuery,{})
system.db.write(memoryTag,results)
system.util.invokeLater(runQuery,5000)
dkhayes117, thank you very much for your efforts.
I could not make your first script (the one using system.util.invokeLater) working, though there were no errors.
Regarding your second proposal, where the db query part must be inserted?
You have re-implemented time.sleep() in a horribly CPU-burning way. Without addressing the reason sleep is bad: it ties up anything else that needs to run on that shared thread or in that work queue.
Don't do it. Use state machines and either timer scripts or scripts triggered repeatedly by scan class/tag group. If a state machine cannot handle your case (really freaking rare), use a sleep only in an asynchronous thread.
OK, trying to wrap all that have been said above, can somebody tell me why the following tag change script does not work on condition that it is working on the script console:
‘’'if previousValue.value == 0 and currentValue.value == 1 and initialChange == False:
def runThisLater():
ss = system.tag.readBlocking(["[New]StartCount"])[0].value
system.tag.writeBlocking(["[New]Trigger"],[ss])
system.util.invokeLater(runThisLater, 5000)'''
Firstly, you don't need both the previous and current values. This is a tag change event, so if current value is true, then the previous value must be false in order for it to trigger. Secondly, invokeLater() is not available for use in a tag change event. I think it is only for client scripting not gateway scripts. Use Jordan's suggestion.
Put a tag change event on the expression tag above like so
if currentValue.value and not initialChange:
results = system.db.runNamedQuery(myQuery,{})
system.db.write(memoryTag,results)
Still does not work.
The outcome of:
‘’‘results = system.db.runNamedQuery(myQuery,{})’’’
is dataset, which cannot be written to the memoryTag.
By the way, is “system.db.write” included in 8.04? I could not find it when i was checking the syntax.
system.db.write() is a v7.9 function. v8.0.x is writeBlocking() or writeAync() You’ll want to use the writeBlocking() one I believe. Make sure your memory tag is of dataset type. It should work.
I tried this tag event scrip on Trigger tagt:
‘’‘if currentValue.value:
results = system.db.runNamedQuery(“Query”,{})
system.tag.writeBlocking(["[New]TestTag",[results]])’’’
where TestTag is dataset memory type tag.
The Named Query is tested and works, system.tag,writeBlocking is working in the script console too.
The thing is I cannot understand if the script works:
How to make visible the value in the dataset and use it after in expressions?
‘’’ if currentValue == 1:
results = system.db.runNamedQuery(“Query”,{})
outcome = results.getValueAt(0,0)
system.tag.writeBlocking(["[New]Tag",[outcome]]) ‘’’
Again it works in the script console, but not like tag event script.
What is the problem?
Your runNamedQuery call probably needs to include the project name. Tag event scripts execute in a global scope (outside of any specific project) but named queries reside inside specific projects. Note the ‘Gateway Scope Syntax’ section on the manual page: https://docs.inductiveautomation.com/display/DOC80/system.db.runNamedQuery