# Adding values together only if tag is true

**URL:** <https://forum.inductiveautomation.com/t/adding-values-together-only-if-tag-is-true/93640>\
**Category:** Ignition\
**Tags:** perspective\
**Created:** [July 23, 2024, 11:54pm UTC](https://forum.inductiveautomation.com/t/adding-values-together-only-if-tag-is-true/93640 "2024-07-23T23:54:34Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![thomas\_wallace](https://avatars.discourse-cdn.com/v4/letter/t/57b2e6/32.png) [@thomas\_wallace](https://forum.inductiveautomation.com/u/thomas_wallace)\
**Post date:** [July 23, 2024, 11:54pm UTC](https://forum.inductiveautomation.com/t/adding-values-together-only-if-tag-is-true/93640/1 "2024-07-23T23:54:34Z")

</div>

I have 160 tags I need to add up to get a total sum. I only want the sum of the individual tags if its "true" though. So any tag that is not true I would not want its value added into the total sum.  
I'm new with expressions and could use some help if anyone knows how to write this out.

---

<div class="post-metadata">

**Author:** ![Kevin.Herron](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/kevin.herron/32/7259_2.png) [@Kevin.Herron](https://forum.inductiveautomation.com/u/Kevin.Herron)\
**Post date:** [July 24, 2024, 12:00am UTC](https://forum.inductiveautomation.com/t/adding-values-together-only-if-tag-is-true/93640/2 "2024-07-24T00:00:00Z")

</div>

Are all these tags boolean and you want to count the number of `true` values?

---

<div class="post-metadata">

**Author:** ![thomas\_wallace](https://avatars.discourse-cdn.com/v4/letter/t/57b2e6/32.png) [@thomas\_wallace](https://forum.inductiveautomation.com/u/thomas_wallace)\
**Post date:** [July 24, 2024, 12:05am UTC](https://forum.inductiveautomation.com/t/adding-values-together-only-if-tag-is-true/93640/3 "2024-07-24T00:05:40Z")

</div>

Yes they are boolean. Sorry I forgot to say if the boolean is true then I need to add the value of another tag. And so on 160 times.  
If boolean is not true then dont add that sum into the final number.

This is hard for me to explain. Sorry

---

<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:** [July 24, 2024, 12:07am UTC](https://forum.inductiveautomation.com/t/adding-values-together-only-if-tag-is-true/93640/4 "2024-07-24T00:07:29Z")

</div>

Try this:

```python
{[default]Bool0} * {[default]Other0} +
{[default]Bool1} * {[default]Other1} +
{[default]Bool2} * {[default]Other2} +
... +
{[default]Bool159} * {[default]Other159} 

```

---

<div class="post-metadata">

**Author:** ![Kevin.Herron](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/kevin.herron/32/7259_2.png) [@Kevin.Herron](https://forum.inductiveautomation.com/u/Kevin.Herron)\
**Post date:** [July 24, 2024, 12:08am UTC](https://forum.inductiveautomation.com/t/adding-values-together-only-if-tag-is-true/93640/5 "2024-07-24T00:08:35Z")

</div>

This is a clever solution if this must be an expression... not great performance implications though.

@thomas_wallace are you sure this has to be an expression and not a script?

---

<div class="post-metadata">

**Author:** ![thomas\_wallace](https://avatars.discourse-cdn.com/v4/letter/t/57b2e6/32.png) [@thomas\_wallace](https://forum.inductiveautomation.com/u/thomas_wallace)\
**Post date:** [July 24, 2024, 12:09am UTC](https://forum.inductiveautomation.com/t/adding-values-together-only-if-tag-is-true/93640/6 "2024-07-24T00:09:43Z")

</div>

It could be script. I know little about expressions and even less about scripts.

---

<div class="post-metadata">

**Author:** ![matt.eaton](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/matt.eaton/32/72046_2.png) [@matt.eaton](https://forum.inductiveautomation.com/u/matt.eaton)\
**Post date:** [July 24, 2024, 12:15am UTC](https://forum.inductiveautomation.com/t/adding-values-together-only-if-tag-is-true/93640/7 "2024-07-24T00:15:12Z")

</div>

The gateway events section of your project is where to start if you want to run scripts.  
Let's say for instance you wanted this to update every minute. You could make a scheduled script with these settings:

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

On the script side, the pseudo-code steps would be:

```python
# Read all the booleans and value tags
# Iterate through each pair of tags
# If the boolean of the pair is true, add the value
# If not true, ignore / don't even write a clause for this
# write the value of the result to a memory tag to hold your results.

```

If you need the actual lines of python... [matt.eaton@alphastationautomation.com](mailto:matt.eaton@alphastationautomation.com) lol

---

<div class="post-metadata">

**Author:** ![thomas\_wallace](https://avatars.discourse-cdn.com/v4/letter/t/57b2e6/32.png) [@thomas\_wallace](https://forum.inductiveautomation.com/u/thomas_wallace)\
**Post date:** [July 24, 2024, 12:15am UTC](https://forum.inductiveautomation.com/t/adding-values-together-only-if-tag-is-true/93640/8 "2024-07-24T00:15:18Z")

</div>

![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/a/2/a2d184b52fa341d3fa1f89f01f87114529491025.png)  
Line 1 by itself works. When I added line 2 it doesnt work. Line 1 boolean is true. Line 2 boolean is false. Was using line 2 as a test.

---

<div class="post-metadata">

**Author:** ![Kevin.Herron](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/kevin.herron/32/7259_2.png) [@Kevin.Herron](https://forum.inductiveautomation.com/u/Kevin.Herron)\
**Post date:** [July 24, 2024, 12:16am UTC](https://forum.inductiveautomation.com/t/adding-values-together-only-if-tag-is-true/93640/9 "2024-07-24T00:16:28Z")

</div>

Here's my pseudocode dump:

```python
tagPaths = [] # build this somehow, first all the boolean tag paths, then all the value tag paths

values = [qv.value for qv in system.tag.readBlocking(tagsPaths)]

z = zip(values[0:159], values[160:319])

total = sum(map(lambda t: t[1], filter(z, lambda t: t[0])))

```

---

<div class="post-metadata">

**Author:** ![matt.eaton](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/matt.eaton/32/72046_2.png) [@matt.eaton](https://forum.inductiveautomation.com/u/matt.eaton)\
**Post date:** [July 24, 2024, 12:17am UTC](https://forum.inductiveautomation.com/t/adding-values-together-only-if-tag-is-true/93640/10 "2024-07-24T00:17:30Z")

</div>

zip with slicing, you rockstar! I'm stealing that.

---

<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:** [July 24, 2024, 12:19am UTC](https://forum.inductiveautomation.com/t/adding-values-together-only-if-tag-is-true/93640/11 "2024-07-24T00:19:04Z")

</div>

Thomas, hover over the Error\_ExpressionEval and report what it says.

In general you should be posting code, not pictures of code (unless the context is relevant). See [Wiki - how to post code on this forum](https://forum.inductiveautomation.com/t/wiki-how-to-post-code-on-this-forum/77831).

---

<div class="post-metadata">

**Author:** ![thomas\_wallace](https://avatars.discourse-cdn.com/v4/letter/t/57b2e6/32.png) [@thomas\_wallace](https://forum.inductiveautomation.com/u/thomas_wallace)\
**Post date:** [July 24, 2024, 12:23am UTC](https://forum.inductiveautomation.com/t/adding-values-together-only-if-tag-is-true/93640/12 "2024-07-24T00:23:11Z")

</div>

Ok thanks. Was wondering how people posted code.  
Error says "Expression right operand is null"

---

<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:** [July 24, 2024, 12:24am UTC](https://forum.inductiveautomation.com/t/adding-values-together-only-if-tag-is-true/93640/13 "2024-07-24T00:24:16Z")

</div>

Right, so use the tag browser to examine the tags and figure out why you're getting a null.

---

<div class="post-metadata">

**Author:** ![thomas\_wallace](https://avatars.discourse-cdn.com/v4/letter/t/57b2e6/32.png) [@thomas\_wallace](https://forum.inductiveautomation.com/u/thomas_wallace)\
**Post date:** [July 24, 2024, 12:25am UTC](https://forum.inductiveautomation.com/t/adding-values-together-only-if-tag-is-true/93640/14 "2024-07-24T00:25:06Z")

</div>

@Kevin.Herron So add all my tags to the top line of your script then the rest is exactly how you have it?

---

<div class="post-metadata">

**Author:** ![Kevin.Herron](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/kevin.herron/32/7259_2.png) [@Kevin.Herron](https://forum.inductiveautomation.com/u/Kevin.Herron)\
**Post date:** [July 24, 2024, 12:27am UTC](https://forum.inductiveautomation.com/t/adding-values-together-only-if-tag-is-true/93640/15 "2024-07-24T00:27:03Z")

</div>

More or less... I typed this out in a text editor and didn't actually try to run it.

If you type it out by hand the tag paths should look something like:

```python
tagPaths = ["[default]Foo", "[default]Bar", ...]

```

but you should be able to build it programmatically if all your tag paths are predictable.

You also would need to write this script and run it somewhere else, not where you're currently defining the expression. Something like @matt.eaton suggested. It depends on how often you need this summation to happen.

---

<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:** [July 24, 2024, 12:43am UTC](https://forum.inductiveautomation.com/t/adding-values-together-only-if-tag-is-true/93640/16 "2024-07-24T00:43:04Z")

</div>

Can you share your tag paths?

---

<div class="post-metadata">

**Author:** ![thomas\_wallace](https://avatars.discourse-cdn.com/v4/letter/t/57b2e6/32.png) [@thomas\_wallace](https://forum.inductiveautomation.com/u/thomas_wallace)\
**Post date:** [July 24, 2024, 12:44am UTC](https://forum.inductiveautomation.com/t/adding-values-together-only-if-tag-is-true/93640/17 "2024-07-24T00:44:59Z")

</div>

@pturmel  
{[default]779/CabTrimAGVs/AGVProgram/AGV01/B2} \* {[default]779/CabTrimAGVs/AGVStatus/AGV01/R5}

The AGV01 changes by one until it reaches 159. So AGV02, AGV03,.....so on for each tag

---

<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:** [July 24, 2024, 12:45am UTC](https://forum.inductiveautomation.com/t/adding-values-together-only-if-tag-is-true/93640/18 "2024-07-24T00:45:47Z")

</div>

No AGV00?

---

<div class="post-metadata">

**Author:** ![thomas\_wallace](https://avatars.discourse-cdn.com/v4/letter/t/57b2e6/32.png) [@thomas\_wallace](https://forum.inductiveautomation.com/u/thomas_wallace)\
**Post date:** [July 24, 2024, 12:47am UTC](https://forum.inductiveautomation.com/t/adding-values-together-only-if-tag-is-true/93640/19 "2024-07-24T00:47:11Z")

</div>

no, AGV01-AGV159

---

<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:** [July 24, 2024, 12:51am UTC](https://forum.inductiveautomation.com/t/adding-values-together-only-if-tag-is-true/93640/20 "2024-07-24T00:51:37Z")

</div>

> [@thomas\_wallace](#):
>
> [default]779/CabTrimAGVs/AGVStatus/AGV01/R5

AGV159 or AGV0159? Two digits until 99, then three? Or always a leading zero?

[Next page](https://forum.inductiveautomation.com/t/adding-values-together-only-if-tag-is-true/93640.md?page=2)
