# Increase a tag value by scripting

**URL:** <https://forum.inductiveautomation.com/t/increase-a-tag-value-by-scripting/59821>\
**Category:** Ignition\
**Tags:** scripting\
**Created:** [May 20, 2022, 6:51am UTC](https://forum.inductiveautomation.com/t/increase-a-tag-value-by-scripting/59821 "2022-05-20T06:51:08Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![yifeiliu08](https://avatars.discourse-cdn.com/v4/letter/y/bbe5ce/32.png) [@yifeiliu08](https://forum.inductiveautomation.com/u/yifeiliu08)\
**Post date:** [May 20, 2022, 6:51am UTC](https://forum.inductiveautomation.com/t/increase-a-tag-value-by-scripting/59821/1 "2022-05-20T06:51:08Z")

</div>

![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/a/8/a8b722978f9b75d8602d601f8265a9dfd3a011cd.png)

```auto
	c=currentValue.value
	p=previousValue.value
	if not initialChange:
		if p!=c:				
			if c==0:
				BDindex=system.tag.readAsync("[Malu OPC tags]Rubber_Extruder/BDIndex/BDIndex").value
				system.tag.writeAsync("[Malu OPC tags]Rubber_Extruder/BDIndex/BDIndex",BDindex+1)

```

How to modify the code to make it works, thanks a lot.

---

<div class="post-metadata">

**Author:** ![iscrac](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/iscrac/32/49757_2.png) [@iscrac](https://forum.inductiveautomation.com/u/iscrac)\
**Post date:** [May 20, 2022, 10:46am UTC](https://forum.inductiveautomation.com/t/increase-a-tag-value-by-scripting/59821/2 "2022-05-20T10:46:44Z")

</div>

try

```auto
c == False

```

or convert c to integer

```auto
int(c) == 0

```

and… try to write Gateway Event Script Timer for that if You want it will decrease constantly while Trigger is False

---

<div class="post-metadata">

**Author:** ![deon.korb](https://avatars.discourse-cdn.com/v4/letter/d/8c91f0/32.png) [@deon.korb](https://forum.inductiveautomation.com/u/deon.korb)\
**Post date:** [May 20, 2022, 10:55am UTC](https://forum.inductiveautomation.com/t/increase-a-tag-value-by-scripting/59821/3 "2022-05-20T10:55:43Z")

</div>

You need to use a readBlocking if you need the value in the next line

---

<div class="post-metadata">

**Author:** ![pascal.fragnoud](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pascal.fragnoud/32/24728_2.png) [@pascal.fragnoud](https://forum.inductiveautomation.com/u/pascal.fragnoud)\
**Post date:** [May 20, 2022, 11:35am UTC](https://forum.inductiveautomation.com/t/increase-a-tag-value-by-scripting/59821/4 "2022-05-20T11:35:15Z")

</div>

@deon.korb is right, `writeAsync` doesn’t return the value.  
You also don’t need to check for `previous != current` since it’s a value changed script.

```python
if not initialChange and currentValue.value:
	idx = system.tag.readBlocking(["[Malu OPC tags]Rubber_Extruder/BDIndex/BDIndex"])[0].value
	system.tag.writeAsync(["[Malu OPC tags]Rubber_Extruder/BDIndex/BDIndex"], idx+1)

```

---

<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:** [May 20, 2022, 12:23pm UTC](https://forum.inductiveautomation.com/t/increase-a-tag-value-by-scripting/59821/5 "2022-05-20T12:23:41Z")

</div>

> [@yifeiliu08](#):
>
> ```auto
> p=previousValue.value
> if not initialChange:
> 
> ```

This is out of order. When initialChange is true, previousValue is null. So the preceding line blows up trying to look up `.value` on a `None`. Move the assignment to `p` to after that check.
