Gateway Tag change script Help

Hi, I want to use a Gateway Tag Change Script, but I never use it before. I have a script in a “2-State Toggle” button, it works but, the script runs the number of times a client is connected, in my case I have an insert statement, when a bolean tag change to 0 “insert some data to DB”, and when is 1 “insert some other data to DB”, but I get multiple rows inserted in my DB because it runs in all the clients separately.

So, I decided to use a gateway script but I don’t know how to put my script. Basicaly I want to do something when a tag change to 0 or 1.

I don’t know what is this for, I see it in the Inductive University’s videos, but I don’t see any other example.

print event.tagpath print initialChange print newValue

This is my script inserting data to db when a tag goes 0 or 1.


tag = system.tag.getTagValue("Produccion/Funcionamiento/Maquina ON OFF") 

	if tag == 0:
		
		fecha_detencion = system.tag.getTagValue("Produccion/Tiempo Turno/Hora actual")		
		aviso = "Aviso de Detencion"
		id = system.db.runPrepUpdate("INSERT INTO partidas_paradas (fecha_detencion, aviso)" 
  		+ "VALUES (?, ?)", [fecha_detencion, aviso])
  	
  	if tag == 1:

  		fecha_inicio = system.tag.getTagValue("Produccion/Tiempo Turno/Hora actual")
  		usuario = system.security.getUsername()
  		tiempo_detenido_tramo = system.tag.getTagValue("Produccion/Funcionamiento/Tiempo Detenido en horas Turno 1 por tramo")
  		update = system.db.runPrepUpdate("UPDATE partidas_paradas SET fecha_inicio = ?, tiempo_detenido = ?, usuario= ? ORDER BY id DESC LIMIT 1",[fecha_inicio, tiempo_detenido_tramo, usuario])




Hi!

Consistent indentation is very important in python. In the code you posted, the first “if” is part of the tag read (because it’s indented) the second “if” is contained in the first one (it’s indented even further)

Also, as s a side note, you can use tabs to indent, you can use space to indent, but you can’t mix them in the same block (you didn’t do that here, I just wanted you to be aware). So the moral of the story: Keep it consistent. :wink:

[code]tag = system.tag.getTagValue(“Produccion/Funcionamiento/Maquina ON OFF”)

if tag == 0:
fecha_detencion = system.tag.getTagValue(“Produccion/Tiempo Turno/Hora actual”)
aviso = “Aviso de Detencion”
id = system.db.runPrepUpdate(“INSERT INTO partidas_paradas (fecha_detencion, aviso)” + “VALUES (?, ?)”, [fecha_detencion, aviso])

if tag == 1:
fecha_inicio = system.tag.getTagValue(“Produccion/Tiempo Turno/Hora actual”)
usuario = system.security.getUsername()
tiempo_detenido_tramo = system.tag.getTagValue(“Produccion/Funcionamiento/Tiempo Detenido en horas Turno 1 por tramo”)
update = system.db.runPrepUpdate(“UPDATE partidas_paradas SET fecha_inicio = ?, tiempo_detenido = ?, usuario= ? ORDER BY id DESC LIMIT 1”,[fecha_inicio, tiempo_detenido_tramo, usuario])
[/code]

Thanks Jordan I change my code to this, but still not working. Ignore how it looks, I follow your advice, I don’t know why It shows messy.

print event.tagPath
print initialChange
print newValue

if newValue.value==0:	
	system.tag.write("Produccion/Animaciones/color",1)
	system.tag.write("Produccion/Condicion visible/Condicion_Visible",1)
	fecha_detencion = system.tag.getTagValue("Produccion/Tiempo Turno/Hora actual")		
	aviso = "Aviso de Detencion"
	id = system.db.runPrepUpdate("INSERT INTO partidas_paradas (fecha_detencion, aviso)" 
  	+ "VALUES (?, ?)", [fecha_detencion, aviso])

if newValue.value==1:	
        fecha_inicio = system.tag.getTagValue("Produccion/Tiempo Turno/Hora actual")
        usuario = system.security.getUsername()
  	tiempo_detenido_tramo = system.tag.getTagValue("Produccion/Funcionamiento/Tiempo Detenido en horas Turno 1 por tramo")
  	update = system.db.runPrepUpdate("UPDATE partidas_paradas SET fecha_inicio = ?, tiempo_detenido = ?, usuario= ? ORDER BY id DESC LIMIT 1",[fecha_inicio, tiempo_detenido_tramo, usuario])

The console show this error, object has no attribute ‘getUsername’, and that the only problem with the script, I changed that to a string inserted by me for now and is working, but I need to get the username some way.

Your script is running on the gateway now - there is no username to get. This function is not available unless you’re in the client/designer scope.