Question On Configure Scripts

I'm sure this has been asked before but it's just not clicking for me. I'm trying to learn some basic scripting. I want to start with something simple such as when an Led display value is 1 or 2 a different Led display multiplies it by 10 or 20 and displays that value. Trying to do this when the first tag updates.

Any pointers? I saw in the documentation about custom methods but I'm not sure how to apply those correctly.

Thanks for any help

You need a binding to trig.

Thanks, got me further along. Can you add to this? Such as writing the new value to a sql table? Or is that done in a different location?

How much of the free online tutorials have you done?

Usually, use tag historian to store value change. You can create a memory tag, and write the new value to the tag via system.tag.writeBlocking. But you may explain what is your complete function. There might be an easier way.
As Transistor's say, If you never used a SCADA software, watch the videos first.

Id rather not make another topic if I can avoid it. What formatting error am I facing currently? I'm following the documentation and everything seems correct. I'm trying to on tag value change execute and insert query into the database.

Here's my current script.

def valueChanged(tag, tagPath, previousValue, currentValue, initialChange, missedEvents):
if previousValue.value != currentValue.value and initialChange == False:
parameters = {"param1": currentValue.value}
system.db.runNamedQuery("Insert Cycle Time", parameters)

It's writing to a postgres database. Personal preference. I know the named query works this part isn't executing and I'm not sure why.

Any pointers?

def valueChanged(tag, tagPath, previousValue, currentValue, initialChange, missedEvents):
	if previousValue.value != currentValue.value and initialChange == False:
		parameters = {"param1": currentValue.value}
		system.db.runNamedQuery("Insert Cycle Time", parameters)

There's a subtle difference in the system.db.runNamedQuery syntax depending on whether the function is being run in the Project Scope or in the Gateway Scope. You seem to be doing the latter so,

Gateway Scope Syntax

system.db.runNamedQuery(project, path, [parameters], [tx], [getKey])

I suspect that the script can't find your named query because that's defined in a project but the tag change event is independent of all projects.

https://docs.inductiveautomation.com/display/DOC81/system.db.runNamedQuery

Thank you for that, its now working as intended. Is there any easy way to know when something is project scope or gateway scope? I'm slowly going through the training and trying to apply what I'm learning to my current goal.

Thanks again

Project scope doesn’t exist. There is only:

  • gateway
  • client
  • designer

I might be corrected on this but it should be:

  • Tag events and anything programmed through the Project Browser → Scripting & Gateway Events (as the name suggests).
  • Tag scripting. These are set up in Tag Editor → Scripting and the events include
    • value changed
    • quality changed
    • alarm active
    • alarm cleared
    • alarm acknowledged

Every other script will be executed in the scope of a project. e.g., A database save on button press, etc.

It does in the documentation, Kevin! (What are you doing manning the forum on a Sunday night?)

  • Scope
    Vision Client, Perspective Session

AFAIK the only faux paus the documentation makes re: scope is pretending "Perspective Session" is something different than "Gateway".

But really, there's only 3 scopes of execution. Sometimes there is an implicit project and sometimes there isn't, but this isn't scope.

Scope can most simply be determined by asking yourself "which JVM is this code executing in?". There's only 3 possible answers:

  • the one running the Gateway
  • the one running the Client
  • the one running the Designer

It's especially important that you understand "project scope" isn't a thing. You can write code in a project's script module and call it from any of the execution scopes.

The other thing that might catch somebody up is that some things belong to projects (script modules, named queries, etc...) and some don't (tags). When you write a tag event script and try to call a system function that relies on a resource defined in a project, then it either needs belong to project configured as the Gateway Scripting Project, or needs to be a call that takes an explicit project name.

2 Likes

So what is a better wording for this then?

It's from the 8.1 system.db.runNamedQuery Documentation

Project Scope Syntax

system.db.runNamedQuery(path, [parameters], [tx], [getKey])

  • Parameters

String path - The path to the Named Query to run. Note that this is the full path to the query, including any folders.

Dictionary[String, Any] parameters - A Python dictionary of parameters for the Named Query to use. [optional]

String tx - An transaction ID, obtained from beginNamedQueryTransaction. If blank, will not be part of a transaction. [optional]

Boolean getKey - Only used for Update Query types. A flag indicating whether or not the result should be the number of rows affected (getKey=0) or the newly generated key value that was created as a result of the update (getKey=1). Not all databases support automatic retrieval of generated keys. [optional]

  • Returns

Any - The results of the query. The exact object returned depends on the Query Type property of the Named Query: typically either a dataset when set to Query, an integer representing the number of rows affected when set to Update Query, or an object matching the data type of the value returned by a Scalar Query.

This feature was changed in Ignition version 8.1.29:

PyDataset - The results of the Named Query as a PyDataset.

  • Scope

Vision Client, Perspective Session

"Perspective Session" scope is basically Gateway scope with some extra contextual information available, notably a project. That's why you see the documentation split that function into 2 different calls with 2 different sets of arguments. Vision Client and Perspective Session have a project available as context.

In your case, a tag event script, there is no associated project because tags don't belong to projects. Tags do, however, execute in "Gateway" scope, which is you see a version of the function that requires you give it an explicit project name to find the named query in.

To some degree this is kind of just stuff you learn along the way.

Thank you for that clarification.