# Copy Previous Value to a Tag Not working in Scripting on my Laptop

**URL:** <https://forum.inductiveautomation.com/t/copy-previous-value-to-a-tag-not-working-in-scripting-on-my-laptop/77280>\
**Category:** Ignition\
**Created:** [August 2, 2023, 11:34am UTC](https://forum.inductiveautomation.com/t/copy-previous-value-to-a-tag-not-working-in-scripting-on-my-laptop/77280 "2023-08-02T11:34:47Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![NARASIMHA\_CASTURI](https://avatars.discourse-cdn.com/v4/letter/n/ed655f/32.png) [@NARASIMHA\_CASTURI](https://forum.inductiveautomation.com/u/NARASIMHA_CASTURI)\
**Post date:** [August 2, 2023, 11:34am UTC](https://forum.inductiveautomation.com/t/copy-previous-value-to-a-tag-not-working-in-scripting-on-my-laptop/77280/1 "2023-08-02T11:34:47Z")

</div>

Per Video : Not Writing . Per Manual too .  
def valueChanged(tag, tagPath, previousValue, currentValue, initialChange, missedEvents):  
#print "tag =" + tag + " Path= " + tagPath  
#If previousValue.value != currentValue.value and initialChange == False :  
If previousValue != currentValue and initialChange == False :  
#system.tag.writeBlocking(["[default]Last Value"], [previousValue.value])  
paths = ["[default]CN\_Tags/My Tag","[default]CN\_Tags/Last Value"]  
values = [currentValue.value,previousValue.value]  
# Execute the write operation.  
system.tag.writeBlocking(paths, values)

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [August 2, 2023, 11:46am UTC](https://forum.inductiveautomation.com/t/copy-previous-value-to-a-tag-not-working-in-scripting-on-my-laptop/77280/2 "2023-08-02T11:46:34Z")

</div>

{ Please edit your topic to format the code properly. That is, highlight all of the code in the topic editor, then click the "preformatted text" button \</\> to add the formatting markers. }

In your code, you must not check `previousValue` before checking the `initialChange` flag, as you will get a null pointer exception (`None` value) if you access any member of `previousValue` when `initialChange` is `True`.

Also, do not compare `initialChange` to `True` or `False`. It is already a boolean, so just use it by itself if you want to check for true, or `not initialChange` to check for false.

---

<div class="post-metadata">

**Author:** ![NARASIMHA\_CASTURI](https://avatars.discourse-cdn.com/v4/letter/n/ed655f/32.png) [@NARASIMHA\_CASTURI](https://forum.inductiveautomation.com/u/NARASIMHA_CASTURI)\
**Post date:** [August 2, 2023, 12:07pm UTC](https://forum.inductiveautomation.com/t/copy-previous-value-to-a-tag-not-working-in-scripting-on-my-laptop/77280/3 "2023-08-02T12:07:04Z")

</div>

Thanks pturmel, I have been working on a 32 bit SCADA system for over 10+ years.  
Found this Ignition very sophisticated and trying to come up to speed.

This is the exact script I am running. \*\*Still not writing with or without Boolean compare of Initial Change.

def valueChanged(tag, tagPath, previousValue, currentValue, initialChange, missedEvents):

If previousValue.value != currentValue.value and initialChange == False :  
system.tag.writeBlocking(["[default]Last Value"], [previousValue.value])

---

<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:** [August 2, 2023, 12:17pm UTC](https://forum.inductiveautomation.com/t/copy-previous-value-to-a-tag-not-working-in-scripting-on-my-laptop/77280/4 "2023-08-02T12:17:42Z")

</div>

I hate to be a broken record, but.. @NARASIMHA_CASTURI

> [@pturmel](#):
>
> { Please edit your topic to format the code properly. That is, highlight all of the code in the topic editor, then click the "preformatted text" button \</\> to add the formatting markers. }

Otherwise, the code you've posted is invalid python

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [August 2, 2023, 12:18pm UTC](https://forum.inductiveautomation.com/t/copy-previous-value-to-a-tag-not-working-in-scripting-on-my-laptop/77280/5 "2023-08-02T12:18:49Z")

</div>

You are accessing `previousValue.value` before checking `initialChange`. Fix that first. And edit your posts to format your code as requested. We cannot see your indentations or other potential mistakes without the proper formatting.

---

<div class="post-metadata">

**Author:** ![NARASIMHA\_CASTURI](https://avatars.discourse-cdn.com/v4/letter/n/ed655f/32.png) [@NARASIMHA\_CASTURI](https://forum.inductiveautomation.com/u/NARASIMHA_CASTURI)\
**Post date:** [August 2, 2023, 1:20pm UTC](https://forum.inductiveautomation.com/t/copy-previous-value-to-a-tag-not-working-in-scripting-on-my-laptop/77280/6 "2023-08-02T13:20:47Z")

</div>

![CN_script](https://us1.discourse-cdn.com/inductiveautomation/original/3X/4/5/453630792f94d47f75cec0031613e94021191c8f.png)  
Thank you Nick and pturmel . Attached my script screen shot. It appears same as that of training videos but checking the boolean first in mine per your advice. Still not writing previous value to Last Value tag. both tags are Read/Writable.

---

<div class="post-metadata">

**Author:** ![JordanCClark](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jordancclark/32/83362_2.png) [@JordanCClark](https://forum.inductiveautomation.com/u/JordanCClark)\
**Post date:** [August 2, 2023, 1:25pm UTC](https://forum.inductiveautomation.com/t/copy-previous-value-to-a-tag-not-working-in-scripting-on-my-laptop/77280/7 "2023-08-02T13:25:31Z")

</div>

it needs to be indented under the def

```python
def valueChanged....
    if not initialChange....
        system.tag.writeblocking.....

```

![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/9/5/95b9be61dd9e3a05109c4fac4292ac6b4c8d0cc4.jpeg)

Note that, if you had formatted the code you posted as requested, this would have been a shorter conversation. 😉

---

<div class="post-metadata">

**Author:** ![NARASIMHA\_CASTURI](https://avatars.discourse-cdn.com/v4/letter/n/ed655f/32.png) [@NARASIMHA\_CASTURI](https://forum.inductiveautomation.com/u/NARASIMHA_CASTURI)\
**Post date:** [August 2, 2023, 2:06pm UTC](https://forum.inductiveautomation.com/t/copy-previous-value-to-a-tag-not-working-in-scripting-on-my-laptop/77280/8 "2023-08-02T14:06:04Z")

</div>

Thank you Jordan for good catch. (Migrating from basic 4 case insensitive /no indention code. haha. forgive me.) .

But still not working as expected. Corrected indent and code pretty much same as that of your screenshot .

 ![CN_script_corrected Indent](https://us1.discourse-cdn.com/inductiveautomation/original/3X/1/6/16cc4bc3ee0e80cd55b3eba8c83d8793bc07300b.png)

---

<div class="post-metadata">

**Author:** ![JordanCClark](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jordancclark/32/83362_2.png) [@JordanCClark](https://forum.inductiveautomation.com/u/JordanCClark)\
**Post date:** [August 2, 2023, 2:50pm UTC](https://forum.inductiveautomation.com/t/copy-previous-value-to-a-tag-not-working-in-scripting-on-my-laptop/77280/9 "2023-08-02T14:50:09Z")

</div>

There may be something in the gateway log that would provide a clue.

---

<div class="post-metadata">

**Author:** ![NARASIMHA\_CASTURI](https://avatars.discourse-cdn.com/v4/letter/n/ed655f/32.png) [@NARASIMHA\_CASTURI](https://forum.inductiveautomation.com/u/NARASIMHA_CASTURI)\
**Post date:** [August 2, 2023, 3:09pm UTC](https://forum.inductiveautomation.com/t/copy-previous-value-to-a-tag-not-working-in-scripting-on-my-laptop/77280/10 "2023-08-02T15:09:22Z")

</div>

Yes Jordan, the 'false' was corrected to 'False' . still no good . sorry

([CN\_Tags]My Tag, valueChanged) Error executing tag event script: Traceback (most recent call last): File "tagevent:valueChanged", line 2, in valueChanged NameError: global name 'false' is not defined. I am changing the tag value but no Log now .

---

<div class="post-metadata">

**Author:** ![Transistor](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/transistor/32/3380_2.png) [@Transistor](https://forum.inductiveautomation.com/u/Transistor)\
**Post date:** [August 2, 2023, 3:20pm UTC](https://forum.inductiveautomation.com/t/copy-previous-value-to-a-tag-not-working-in-scripting-on-my-laptop/77280/11 "2023-08-02T15:20:12Z")

</div>

Can you fix the code formatting in the earlier posts as suggested? It may help. Make it easy for us to help you!  
Select the code and hit the \</\> button.

---

<div class="post-metadata">

**Author:** ![lrose](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/lrose/32/62724_2.png) [@lrose](https://forum.inductiveautomation.com/u/lrose)\
**Post date:** [August 2, 2023, 4:09pm UTC](https://forum.inductiveautomation.com/t/copy-previous-value-to-a-tag-not-working-in-scripting-on-my-laptop/77280/12 "2023-08-02T16:09:17Z")

</div>

You've gone back to you're original script.

Try this:

```python
def valueChanged(tag,tagPath,previousValue,currentValue,initialChange,missedEvents):
    if not initialChange and previousValue.value != currentValue.value:
        system.tag.writeBlocking(['[default]Last Value'],[previousValue.value])

```

What exactly are you expecting to happen that isn't?

Is `CN_Tags` the default tag provider? Is the `Last Value` tag really a part of the default tag provider?

---

<div class="post-metadata">

**Author:** ![NARASIMHA\_CASTURI](https://avatars.discourse-cdn.com/v4/letter/n/ed655f/32.png) [@NARASIMHA\_CASTURI](https://forum.inductiveautomation.com/u/NARASIMHA_CASTURI)\
**Post date:** [August 2, 2023, 4:38pm UTC](https://forum.inductiveautomation.com/t/copy-previous-value-to-a-tag-not-working-in-scripting-on-my-laptop/77280/13 "2023-08-02T16:38:09Z")

</div>

I surely check when I get back home  
Thanks a lot  
Regards  
CN

---

<div class="post-metadata">

**Author:** ![NARASIMHA\_CASTURI](https://avatars.discourse-cdn.com/v4/letter/n/ed655f/32.png) [@NARASIMHA\_CASTURI](https://forum.inductiveautomation.com/u/NARASIMHA_CASTURI)\
**Post date:** [August 2, 2023, 6:18pm UTC](https://forum.inductiveautomation.com/t/copy-previous-value-to-a-tag-not-working-in-scripting-on-my-laptop/77280/14 "2023-08-02T18:18:54Z")

</div>

both 'My Tag' and 'Last Value' tags belong to same Tag Group ('Default')

I stick the code you sent me ( with indent and single quote for string)

Gateway is Running  
Still NOT writing to Last Value when I change the value of My Tag

 ![Gateway Log](https://us1.discourse-cdn.com/inductiveautomation/original/3X/4/8/48d1b64e3528a29a02c2ad66318f176327db46de.png)

---

<div class="post-metadata">

**Author:** ![lrose](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/lrose/32/62724_2.png) [@lrose](https://forum.inductiveautomation.com/u/lrose)\
**Post date:** [August 2, 2023, 6:33pm UTC](https://forum.inductiveautomation.com/t/copy-previous-value-to-a-tag-not-working-in-scripting-on-my-laptop/77280/15 "2023-08-02T18:33:03Z")

</div>

> [@NARASIMHA\_CASTURI](#):
>
> both 'My Tag' and 'Last Value' tags belong to same Tag Group ('Default')

Note that _ **Tag Group** _ is not the same thing as _ **Tag Provider** _.

I asked because you posted this:

> [@NARASIMHA\_CASTURI](#):
>
> ([CN\_Tags]My Tag, valueChanged) Error executing tag event script:

That clearly shows that `My Tag` is in the Tag Provider `CN_Tags`. That is not the same as the `default` tag provider. The same thing can be seen on the screen shots of the tag editor that you have shown.

What happens if you try this:

```auto
def valueChanged(tag,tagPath,previousValue,currentValue,initialChange,missedEvents):
    if not initialChange and previousValue.value != currentValue.value:
        system.tag.writeBlocking(['[CN_Tags]Last Value'],[previousValue.value])

```

There is nothing syntactically wrong with the script, which means we need to look elsewhere, the most obvious place is the tag provider.

Can you show a snipit of the tag browser in the designer so we can see which tag provider is being used? Something like this:  
 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/c/8/c8d1ef804863aeb8f3adf90d3df185518d07bdb6.png)

---

<div class="post-metadata">

**Author:** ![NARASIMHA\_CASTURI](https://avatars.discourse-cdn.com/v4/letter/n/ed655f/32.png) [@NARASIMHA\_CASTURI](https://forum.inductiveautomation.com/u/NARASIMHA_CASTURI)\
**Post date:** [August 2, 2023, 8:01pm UTC](https://forum.inductiveautomation.com/t/copy-previous-value-to-a-tag-not-working-in-scripting-on-my-laptop/77280/16 "2023-08-02T20:01:58Z")

</div>

It is working NOW after putting in default tag provider

But still not working in CN\_tags provider  
tag path from right click:  
[CN\_Tags]My Tag  
[CN\_Tags]Last Value

 ![Good](https://us1.discourse-cdn.com/inductiveautomation/original/3X/3/5/35b6bc14df46d1a42b69190570c5187abfaf489f.png)  
 ![No GOOD](https://us1.discourse-cdn.com/inductiveautomation/original/3X/8/5/859ef66fb44d6969c5e98ee3482d938e2e26ed36.png)

I think I will put this to rest.

Thanks for all your help

it is indeed a sophisticated SCADA

Just last question?  
In Automotive Industry : Can IGN track a Carrier and Vehicle Combo to Route to A or B lane based on some attribute Value ?? I am kind of new to IGN and like to know .

---

<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:** [August 2, 2023, 10:50pm UTC](https://forum.inductiveautomation.com/t/copy-previous-value-to-a-tag-not-working-in-scripting-on-my-laptop/77280/17 "2023-08-02T22:50:36Z")

</div>

Is there any error in the tag diagnostics for the one that isn't working? (heartbeat icon in the top right array of icons in the tag editor in that screenshot) If scripts produce errors, then they'll show up in there

---

<div class="post-metadata">

**Author:** ![Matrix\_Engineering](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/matrix_engineering/32/19781_2.png) [@Matrix\_Engineering](https://forum.inductiveautomation.com/u/Matrix_Engineering)\
**Post date:** [August 2, 2023, 10:57pm UTC](https://forum.inductiveautomation.com/t/copy-previous-value-to-a-tag-not-working-in-scripting-on-my-laptop/77280/18 "2023-08-02T22:57:58Z")

</div>

From your second screenshot, the one that isn't working, the tag script isn't enabled (top right check box)

---

<div class="post-metadata">

**Author:** ![NARASIMHA\_CASTURI](https://avatars.discourse-cdn.com/v4/letter/n/ed655f/32.png) [@NARASIMHA\_CASTURI](https://forum.inductiveautomation.com/u/NARASIMHA_CASTURI)\
**Post date:** [August 3, 2023, 1:13am UTC](https://forum.inductiveautomation.com/t/copy-previous-value-to-a-tag-not-working-in-scripting-on-my-laptop/77280/19 "2023-08-03T01:13:09Z")

</div>

Yes Sir, you are right. I enabled and it is good. thanks

---

<div class="post-metadata">

**Author:** ![zbinder](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/zbinder/32/28918_2.png) [@zbinder](https://forum.inductiveautomation.com/u/zbinder)\
**Post date:** [August 3, 2023, 2:45pm UTC](https://forum.inductiveautomation.com/t/copy-previous-value-to-a-tag-not-working-in-scripting-on-my-laptop/77280/20 "2023-08-03T14:45:48Z")

</div>

@NARASIMHA_CASTURI , are you familiar with the ability to edit?

If you click the pencil icon at the bottom of one of your posts, it will allow you to edit it.  
 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/f/5/f5091ab243e35bfd7b9cc18de0267cfd5d311001.png)

I think everyone is trying to politely point out that it's a lot easier to help someone out when you can see the code in its proper formatting. It also shows the indentation, which can make a difference in Python.

if False:  
print "Hi"  
else:  
print "Hello World"

vs

```auto
if False:
	print "Hi"
else:
print "Hello World"

```

Why isn't my code working? It's because I didn't indent the last line, but you can't necessarily tell that when the format isn't shown. (edit: It's also a lot easier to read. Also, "False" needed to be capitalized. Whoops, this is why editing is great.)

[Next page](https://forum.inductiveautomation.com/t/copy-previous-value-to-a-tag-not-working-in-scripting-on-my-laptop/77280.md?page=2)
