Tag Event Scripts function must be declared before or after if currentValue.value:

which is the correct place for a declaration of the function in the Tag Event Scripts in Value Events-Value Changed before of the if currentValue.value: line (code1) or after the if currentValue.value: line (code2)

code1:

def funcion_hist(id_Variable):
	objTS = system.tag.read("[.]../HeartBeatSystem/ts_MsSQL")
	strConsultaSQL = system.db.runQuery("SELECT TOP(1) " + id_Variable + " FROM HrsOperadas 
                             ORDER BY hrsoperadas_ndx DESC", database="cnxMsSQL")
	for row in strConsultaSQL:
	strHrs = row[0]
	query = "INSERT INTO HistorialHrsOperadas (id_Variable, Valor, id_TimeStamp) VALUES (?, ?, ?)"
	args = [id_Variable.value, strHrs.value, objTS.value]
	system.db.runPrepUpdate(query, args, database="cnxMsSQL")
	
if currentValue.value:
	funcion_hist("BbaSubFlujo_1")
	funcion_hist("BbaSubFlujo_2")
	funcion_hist("BbaSubFlujo_3")
	funcion_hist("BbaSubFlujo_4")

or

code2:

if currentValue.value:
	def funcion_hist(id_Variable):
		objTS = system.tag.read("[.]../HeartBeatSystem/ts_MsSQL")
		strConsultaSQL = system.db.runQuery("SELECT TOP(1) " + id_Variable + " FROM HrsOperadas 
                             ORDER BY hrsoperadas_ndx DESC", database="cnxMsSQL")
		for row in strConsultaSQL:
		strHrs = row[0]
		query = "INSERT INTO HistorialHrsOperadas (id_Variable, Valor, id_TimeStamp) VALUES (?, ?, ?)"
		args = [id_Variable.value, strHrs.value, objTS.value]
		system.db.runPrepUpdate(query, args, database="cnxMsSQL")
			
	funcion_hist("BbaSubFlujo_1")
	funcion_hist("BbaSubFlujo_2")
	funcion_hist("BbaSubFlujo_3")
	funcion_hist("BbaSubFlujo_4")

PD. sorry for the tab order but the original code have the correct tabs between lines.

Regards Gerardo

Hi Gerardo,

Please put your code in triple backquotes (```),or click on the “preformatted text” button.

Then it will be formatted and keep spaces/tabs.

But from what I read, you wonder if you need to define the function inside or out of the if-clause. If you only use it inside the if-clause, it’s really a matter of taste.

It may give you a little performance gain to not create that function in the cases where currentValue is false. But usually, the caching in Jython is good enough that this won’t make a difference.

Thanks Sanderd17

Regards

No, an inline function definition is executed every time, and cannot be cached. Caching and jython-java-jvm jit optimization cannot be applied to newly-defined functions. Put your function in a shared script if speed matters. Or even better, use a gateway tag change script and a project script module instead of the tag event.

1 Like

hi pturmel.

This would be the correct way?

Close. Remove the function definition from the script entirely. Move everything generic to a function in a project script module. Perform the objTS read once, in the event script, passing it in each function call. The function calls would look like project.someModule.function_hist(objTS, "whatever").

1 Like

Thanks pturmel

Regards Gerardo.