# Trouble updating boolean tag in change script

**URL:** <https://forum.inductiveautomation.com/t/trouble-updating-boolean-tag-in-change-script/81047>\
**Category:** Ignition\
**Tags:** perspective\
**Created:** [October 26, 2023, 5:40pm UTC](https://forum.inductiveautomation.com/t/trouble-updating-boolean-tag-in-change-script/81047 "2023-10-26T17:40:14Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Christy\_Jean](https://avatars.discourse-cdn.com/v4/letter/c/977dab/32.png) [@Christy\_Jean](https://forum.inductiveautomation.com/u/Christy_Jean)\
**Post date:** [October 26, 2023, 5:40pm UTC](https://forum.inductiveautomation.com/t/trouble-updating-boolean-tag-in-change-script/81047/1 "2023-10-26T17:40:14Z")

</div>

I have 2 tags High Force 1 and OffsetForce 1. I have another tag (Time passed) that's keeping track of minutes since a button click. After 3 mins, I check whether HF 1 is within +-5% of OF 1. If it's not within that range I want to change the value of a boolean tag to true.

So in Time passed, I wrote this change script but my boolean tag never changes to true even though HF 1 is outside the range:

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

```
    highF = system.tag.readBlocking('[Ignition_Bond_Press]Press 1/High Force 1')[0]
    offsetF = system.tag.readBlocking('[Ignition_Bond_Press]Press 1/OffsetForce 1')[0]
    if currentValue.value >= 3.00:
	   if highF < (offsetF * 0.95): 
		   if highF > (offsetF * 1.05):
			   system.tag.writeBlocking("[Ignition_Bond_Press]Press 1/Alert", True)
		   else:
			   system.tag.writeBlocking("[Ignition_Bond_Press]Press 1/Alert", False)
    else:
	    system.tag.writeBlocking("[Ignition_Bond_Press]Press 1/Alert", False)``

```

---

<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:** [October 26, 2023, 5:51pm UTC](https://forum.inductiveautomation.com/t/trouble-updating-boolean-tag-in-change-script/81047/2 "2023-10-26T17:51:55Z")

</div>

The logic is flawed here.  
If highF is Below the range, it will write false to the tag.

I assumed you made sure the timer tag is actually incrementing.

edit: made the script a bit easier to read  
edit2: and what @lrose said.

edit3: @lrose script makes more sense, use this instead.

---

<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:** [October 26, 2023, 5:53pm UTC](https://forum.inductiveautomation.com/t/trouble-updating-boolean-tag-in-change-script/81047/3 "2023-10-26T17:53:52Z")

</div>

You're not checking the actual value of the tags for highF or offsetF. Plus you should really read and write all of the tags at the same time.

`readBlocking()` returns a list of QualifiedValues, you are just referenceing the first QV, but not the actual value of that tag, so `if-else` clause is not evaluated the way you think that it should be.

Try code similar to this:

```auto

if currentValue.value >= 3.00:
    highF, offestF = system.tag.readBlocking(['[Ignition_Bond_Press]Press 1/High Force 1','[Ignition_Bond_Press]Press 1/OffsetForce 1'])
    lowLimit = offsetF.value * 0.95
    highLimit = offsetF.value * 1.05
    system.tag.writeBlocking(['[Ignition_Bond_Press]Press 1/Alert'],[not (lowLimit < highF.value < highLimit)])

```

Edit for explanation:

Because this is in a Tag Value Changed Script, we want the script to execute a quickly as possible, so if the current value isn't within range don't do anything. Which is why I put everything within the first check.

---

<div class="post-metadata">

**Author:** ![Christy\_Jean](https://avatars.discourse-cdn.com/v4/letter/c/977dab/32.png) [@Christy\_Jean](https://forum.inductiveautomation.com/u/Christy_Jean)\
**Post date:** [October 26, 2023, 5:59pm UTC](https://forum.inductiveautomation.com/t/trouble-updating-boolean-tag-in-change-script/81047/4 "2023-10-26T17:59:28Z")

</div>

how is the last line changing the boolean tag?

---

<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:** [October 26, 2023, 6:03pm UTC](https://forum.inductiveautomation.com/t/trouble-updating-boolean-tag-in-change-script/81047/5 "2023-10-26T18:03:44Z")

</div>

You want the script to write `False` if the highF is not within the limits.

That expression is short hand for the following:

```auto
lowLimit < highF.value and highF.value < highLimit

```

This will evaluate `True` if highF is within the limits, so we invert it with a `not` , however, we need to insure that we evaluate the expression prior to inversion so we enclose it in parenthesis.

```auto
not (lowLimit < highF.value < highLimit)

```

Python will evalueate the expression and return true or false prior to the call to `writeBlocking()`

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [October 26, 2023, 6:13pm UTC](https://forum.inductiveautomation.com/t/trouble-updating-boolean-tag-in-change-script/81047/6 "2023-10-26T18:13:15Z")

</div>

Consider, for the sake of you and everyone who comes after you, separating boolean expressions like that into standalone variables to make their logic and purpose more clear:

```python
if currentValue.value >= 3.00:
    highF, offestF = system.tag.readBlocking(['[Ignition_Bond_Press]Press 1/High Force 1','[Ignition_Bond_Press]Press 1/OffsetForce 1'])
    lowLimit = offsetF.value * 0.95
    highLimit = offsetF.value * 1.05
    isInRange = lowLimit < highF.value < highLimit
    system.tag.writeBlocking(['[Ignition_Bond_Press]Press 1/Alert'],[not isInRange])

```

---

<div class="post-metadata">

**Author:** ![Christy\_Jean](https://avatars.discourse-cdn.com/v4/letter/c/977dab/32.png) [@Christy\_Jean](https://forum.inductiveautomation.com/u/Christy_Jean)\
**Post date:** [October 26, 2023, 6:25pm UTC](https://forum.inductiveautomation.com/t/trouble-updating-boolean-tag-in-change-script/81047/7 "2023-10-26T18:25:57Z")

</div>

So how would it change Alert back to false if highF does change into within range?

---

<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:** [October 26, 2023, 6:55pm UTC](https://forum.inductiveautomation.com/t/trouble-updating-boolean-tag-in-change-script/81047/8 "2023-10-26T18:55:09Z")

</div>

In exactly the same way it changes it to true if it is out of range.

The expression evaluates to either `True` or `False` on each execution, that is then written to the tag.

---

<div class="post-metadata">

**Author:** ![Christy\_Jean](https://avatars.discourse-cdn.com/v4/letter/c/977dab/32.png) [@Christy\_Jean](https://forum.inductiveautomation.com/u/Christy_Jean)\
**Post date:** [October 26, 2023, 7:08pm UTC](https://forum.inductiveautomation.com/t/trouble-updating-boolean-tag-in-change-script/81047/9 "2023-10-26T19:08:58Z")

</div>

I thought so, but then I did some test runs and I noticed that once Alert becomes true it doesn't change to false even when I change highF to within the range

---

<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:** [October 26, 2023, 7:11pm UTC](https://forum.inductiveautomation.com/t/trouble-updating-boolean-tag-in-change-script/81047/10 "2023-10-26T19:11:23Z")

</div>

I'm really not sure, the only thing that I can suggest is to put some logging into the script to verify the code is executing the way you think it is. Assuming that the code executes, if the highF value is within the range, then `False` should be written to the tag.

---

<div class="post-metadata">

**Author:** ![Christy\_Jean](https://avatars.discourse-cdn.com/v4/letter/c/977dab/32.png) [@Christy\_Jean](https://forum.inductiveautomation.com/u/Christy_Jean)\
**Post date:** [October 26, 2023, 7:20pm UTC](https://forum.inductiveautomation.com/t/trouble-updating-boolean-tag-in-change-script/81047/11 "2023-10-26T19:20:11Z")

</div>

I forgot to take into account that I was testing with negative numbers. Once I used abs(), it worked
