# Save information when returning to a previous state

**URL:** <https://forum.inductiveautomation.com/t/save-information-when-returning-to-a-previous-state/45834>\
**Category:** Ignition\
**Tags:** ignition78, ignition79, ignition80, ignition81, scripting\
**Created:** [April 20, 2021, 10:23pm UTC](https://forum.inductiveautomation.com/t/save-information-when-returning-to-a-previous-state/45834 "2021-04-20T22:23:03Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![antonio.sanchez](https://avatars.discourse-cdn.com/v4/letter/a/a587f6/32.png) [@antonio.sanchez](https://forum.inductiveautomation.com/u/antonio.sanchez)\
**Post date:** [April 20, 2021, 10:23pm UTC](https://forum.inductiveautomation.com/t/save-information-when-returning-to-a-previous-state/45834/1 "2021-04-20T22:23:03Z")

</div>

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

```auto
	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

---

<div class="post-metadata">

**Author:** ![nminchin](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/nminchin/32/6911_2.png) [@nminchin](https://forum.inductiveautomation.com/u/nminchin)\
**Post date:** [April 20, 2021, 10:28pm UTC](https://forum.inductiveautomation.com/t/save-information-when-returning-to-a-previous-state/45834/2 "2021-04-20T22:28:23Z")

</div>

> [@antonio.sanchez](#):
>
> `currentValue.value > 1`

Does this help?

---

<div class="post-metadata">

**Author:** ![chandler](https://avatars.discourse-cdn.com/v4/letter/c/e9a140/32.png) [@chandler](https://forum.inductiveautomation.com/u/chandler)\
**Post date:** [April 20, 2021, 10:28pm UTC](https://forum.inductiveautomation.com/t/save-information-when-returning-to-a-previous-state/45834/3 "2021-04-20T22:28:25Z")

</div>

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:

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

```

---

<div class="post-metadata">

**Author:** ![antonio.sanchez](https://avatars.discourse-cdn.com/v4/letter/a/a587f6/32.png) [@antonio.sanchez](https://forum.inductiveautomation.com/u/antonio.sanchez)\
**Post date:** [April 20, 2021, 10:35pm UTC](https://forum.inductiveautomation.com/t/save-information-when-returning-to-a-previous-state/45834/4 "2021-04-20T22:35:06Z")

</div>

now it doesn’t save it

---

<div class="post-metadata">

**Author:** ![chandler](https://avatars.discourse-cdn.com/v4/letter/c/e9a140/32.png) [@chandler](https://forum.inductiveautomation.com/u/chandler)\
**Post date:** [April 20, 2021, 10:39pm UTC](https://forum.inductiveautomation.com/t/save-information-when-returning-to-a-previous-state/45834/5 "2021-04-20T22:39:00Z")

</div>

you changed your code to look like this?

```auto
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")
```

---

<div class="post-metadata">

**Author:** ![antonio.sanchez](https://avatars.discourse-cdn.com/v4/letter/a/a587f6/32.png) [@antonio.sanchez](https://forum.inductiveautomation.com/u/antonio.sanchez)\
**Post date:** [April 21, 2021, 1:06pm UTC](https://forum.inductiveautomation.com/t/save-information-when-returning-to-a-previous-state/45834/6 "2021-04-21T13:06:24Z")

</div>

> [@chandler](#):
>
> you changed your code to look like this?

yes, I do

---

<div class="post-metadata">

**Author:** ![antonio.sanchez](https://avatars.discourse-cdn.com/v4/letter/a/a587f6/32.png) [@antonio.sanchez](https://forum.inductiveautomation.com/u/antonio.sanchez)\
**Post date:** [April 21, 2021, 6:30pm UTC](https://forum.inductiveautomation.com/t/save-information-when-returning-to-a-previous-state/45834/7 "2021-04-21T18:30:48Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![nminchin](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/nminchin/32/6911_2.png) [@nminchin](https://forum.inductiveautomation.com/u/nminchin)\
**Post date:** [April 21, 2021, 7:18pm UTC](https://forum.inductiveautomation.com/t/save-information-when-returning-to-a-previous-state/45834/8 "2021-04-21T19:18:18Z")

</div>

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.

```auto
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")

```
