Save information when returning to a previous state

Hello everybody

I have a tag with a variable data 1 or 2 it depends on the case that is presented, and I require that the information be saved in my database when it changes from a state 2 to 1

	if currentValue.value > 1 and currentValue.value != previousValue.value and not initialChange:
				INS = "INSERT INTO PAROS (Linea, Maquina, TipoParo, TiempoParo, Fecha) VALUES (?, ?, ?, ?, ?)"			
					
				timeDiff = int(system.tag.read("[.]../MemTimeDiff").value)
				Contador = system.date.format(system.date.addMinutes(system.date.midnight(system.date.now()), timeDiff), "HH:mm:ss")
				Fecha = system.date.now()	
					
				system.db.runPrepUpdate(INS,["Rim","FBW","Prueba",Contador,Fecha],"MW")

currently it changes it to me when it changes from 1 to 2 but what I require is when it changes from 2 to 1

Does this help?

If you only want the information to go to the database when it changes from 2 to 1 then the condition for your if statement isn’t quite right.
Instead try this:

if currentValue.value == 1 and previousValue.value == 2 and not initialChange:
     #Use your same code here

now it doesn’t save it

you changed your code to look like this?

if currentValue.value == 1 and previousValue.value == 2 and not initialChange:
				INS = "INSERT INTO PAROS (Linea, Maquina, TipoParo, TiempoParo, Fecha) VALUES (?, ?, ?, ?, ?)"			
					
				timeDiff = int(system.tag.read("[.]../MemTimeDiff").value)
				Contador = system.date.format(system.date.addMinutes(system.date.midnight(system.date.now()), timeDiff), "HH:mm:ss")
				Fecha = system.date.now()	
					
				system.db.runPrepUpdate(INS,["Rim","FBW","Prueba",Contador,Fecha],"MW")

yes, I do

Now already saved it but saves the elapsed time at 00:00

Is the value of memtimediff = 0?
Put some print statements into the script and check what values are being passed into the query. It might help to separate the calculation of Contador into multiple stages instead of in a single line. The prints will appear in the wrapper log, so make sure you print something that will let you easily search for it, otherwise it will be a needle in a haystack (difficult to find).

I. E.

if currentValue.value == 1 and previousValue.value == 2 and not initialChange:
	print "FINDME'
	INS = "INSERT INTO PAROS (Linea, Maquina, TipoParo, TiempoParo, Fecha) VALUES (?, ?, ?, ?, ?)"			
					
	timeDiff = int(system.tag.read("[.]../MemTimeDiff").value)
	print 'timeDiff=', timeDiff
	Contador = system.date.midnight(system.date.now())
	print "Contador=", Contador
	Contador = system.date.addMinutes(Contador, timeDiff)
	print "Contador=", Contador 
	Contador = system.date.format(Contador, "HH:mm:ss") 
	print "Contador=", Contador
	Fecha = system.date.now()	
					
	system.db.runPrepUpdate(INS,["Rim","FBW","Prueba",Contador,Fecha],"MW")
1 Like