# Adding Values using "if" statements

**URL:** <https://forum.inductiveautomation.com/t/adding-values-using-if-statements/38130>\
**Category:** Ignition\
**Tags:** ignition80\
**Created:** [August 27, 2020, 3:14pm UTC](https://forum.inductiveautomation.com/t/adding-values-using-if-statements/38130 "2020-08-27T15:14:53Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![mhicks1](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/mhicks1/32/14121_2.png) [@mhicks1](https://forum.inductiveautomation.com/u/mhicks1)\
**Post date:** [August 27, 2020, 3:14pm UTC](https://forum.inductiveautomation.com/t/adding-values-using-if-statements/38130/1 "2020-08-27T15:14:53Z")

</div>

Good Morning,  
I am trying to add several values using IF statements and within those IF statements I am using TAG functions. I have tried to add these values without any TAG functions and it works fine. It currently will only show the first IF statement value that is TRUE. Attached is a sample of my Expression.

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/c/4/c45af71ac4d2a60a8ad9ce6045885cbc059637a9.png)  
Here is the actual Expression.  
 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/5/3/53bc74959de411ff8a0de3407735814912d0889f.png)

---

<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 27, 2020, 3:37pm UTC](https://forum.inductiveautomation.com/t/adding-values-using-if-statements/38130/2 "2020-08-27T15:37:58Z")

</div>

> [@mhicks1](#):
>
> without any TAG functions and it works fine

There's your answer. Don't use the tag() function. Create custom properties with indirect tag bindings for each would-be tag() function, and use that property in your complex expression.

---

<div class="post-metadata">

**Author:** ![jlandwerlen](https://avatars.discourse-cdn.com/v4/letter/j/bc8723/32.png) [@jlandwerlen](https://forum.inductiveautomation.com/u/jlandwerlen)\
**Post date:** [August 27, 2020, 4:05pm UTC](https://forum.inductiveautomation.com/t/adding-values-using-if-statements/38130/3 "2020-08-27T16:05:55Z")

</div>

I use tag() all of the time without issue. What is the problem with it?

---

<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 27, 2020, 4:39pm UTC](https://forum.inductiveautomation.com/t/adding-values-using-if-statements/38130/4 "2020-08-27T16:39:03Z")

</div>

Start here:

> [@Binding to Custom Component Methods](http://forum.inductiveautomation.com/t/binding-to-custom-component-methods/19245/9):
>
> One more optimization tip: never use the tag() expression function. Wherever you think you need it, move its result to an indirect tag binding, on a custom property if necessary, and use that property’s value instead. The tag() expression function is a backwards-compatibility function with less than ideal behavior.

Then here:

> [@Nesting Not Allowed while using Properties in a Template to turn on a Light](http://forum.inductiveautomation.com/t/nesting-not-allowed-while-using-properties-in-a-template-to-turn-on-a-light/33609/16):
>
> Yes, there is. If the items that comprise your tagpath are updated, even if yielding the same result path, the entire expression containing the tag() function will have to execute. With indirect binding, this doesn’t happen. The strings themselves have to be reconstructed every time, even if not changing, slowing down the final expression execution. There are a few cases with computed gateway tags where the tag() expression is the only good way to do indirection, and the dynamic stuff above…

Follow some of the links in those posts for more.

---

<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 27, 2020, 4:40pm UTC](https://forum.inductiveautomation.com/t/adding-values-using-if-statements/38130/5 "2020-08-27T16:40:34Z")

</div>

Also in Perspective:

> [@Slow loading times after 8.0.14](http://forum.inductiveautomation.com/t/slow-loading-times-after-8-0-14/37413/3):
>
> I do have tag() in my expression bindings…I think you’re right and thats my issue! Thanks! I’ll give it a try and see if I can swap these to indirect tags. UPDATE: I went back and changed the tags to indirect addressing with transforms. The loading times have improved greatly and my issues have been resolved. I’m wondering if something is wrong with the tag() in an expression statement as thats what I was using. It worked fine prior to 8.0.14

---

<div class="post-metadata">

**Author:** ![mhicks1](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/mhicks1/32/14121_2.png) [@mhicks1](https://forum.inductiveautomation.com/u/mhicks1)\
**Post date:** [August 27, 2020, 7:35pm UTC](https://forum.inductiveautomation.com/t/adding-values-using-if-statements/38130/6 "2020-08-27T19:35:52Z")

</div>

@pturmel Thank you for referring me back to my original request from many months ago. 😀 I have been working with the custom properties and believe I can apply them to this problem. Thanks again…

---

<div class="post-metadata">

**Author:** ![jlandwerlen](https://avatars.discourse-cdn.com/v4/letter/j/bc8723/32.png) [@jlandwerlen](https://forum.inductiveautomation.com/u/jlandwerlen)\
**Post date:** [August 27, 2020, 8:34pm UTC](https://forum.inductiveautomation.com/t/adding-values-using-if-statements/38130/7 "2020-08-27T20:34:49Z")

</div>

Thanks for the info. I do agree custom properties are the way to go, especially if you have to reference those tags several times and using several tag() is silly.

FYI, v8.0.16 has a fix for tag()…  
“Use of the tag() function inside a tag in a UDT can potentially lead to a blocking delay of 30 seconds when the tag is initialized.”

---

<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 27, 2020, 10:27pm UTC](https://forum.inductiveautomation.com/t/adding-values-using-if-statements/38130/8 "2020-08-27T22:27:45Z")

</div>

I noticed that problem and fix. Significant, as tag() is appropriate within expression tags. That fix has nothing to do with the drawbacks of using tag() anywhere else.

---

<div class="post-metadata">

**Author:** ![mhicks1](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/mhicks1/32/14121_2.png) [@mhicks1](https://forum.inductiveautomation.com/u/mhicks1)\
**Post date:** [September 21, 2020, 12:43pm UTC](https://forum.inductiveautomation.com/t/adding-values-using-if-statements/38130/9 "2020-09-21T12:43:05Z")

</div>

I wanted to clarify the Custom Property and where I would put them inside of my template. I have a template with properties and then inside of that template I have the expression I am trying to use to sum the values X4. Would those custom properties with indirect bindings live in the Template properties or in the Display where I am doing all of the If statements? I have tried both places but have not been successful. Thank you for your reply.

---

<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:** [September 21, 2020, 3:18pm UTC](https://forum.inductiveautomation.com/t/adding-values-using-if-statements/38130/10 "2020-09-21T15:18:30Z")

</div>

> [@mhicks1](#):
>
> in the Display where I am doing all of the If statements

I'm not precisely sure what you mean by "Display" here.

Anyways, template properties qualify as custom properties, but can only have bindings in the Vision window that holds the template, or an outer template that nests the given template. Template internal properties are custom properties that are invisible to the Vision window or outer template but can have bindings inside the template. Expression bindings inside the template, both for template internal properties and for custom properties on a template's components, can use values from anywhere else inside the template.

Choosing where to create a custom property to hold an intermediate value (like an indirect tag binding) is pretty open, since bindings can draw from all over in your template. I like to keep custom properties on the component where they are used, if they are only used in one place. Then on any container where they have multiple consuming expressions. I would use template internal props if the intermediate value is used all over the place.

Note that you do not have any binding option to expose template internal calculations to the vision window or outer template of a nesting situation. Template internals must use scripting to write to exposed template properties, and that is only useful for the single-instance template holder component. Repeater/Canvas usage doesn't expose them at all.

---

<div class="post-metadata">

**Author:** ![mhicks1](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/mhicks1/32/14121_2.png) [@mhicks1](https://forum.inductiveautomation.com/u/mhicks1)\
**Post date:** [September 21, 2020, 5:02pm UTC](https://forum.inductiveautomation.com/t/adding-values-using-if-statements/38130/11 "2020-09-21T17:02:37Z")

</div>

Good Afternoon. Sorry the Display I am speaking of is a Numeric Display that is within the template.

---

<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:** [September 21, 2020, 7:07pm UTC](https://forum.inductiveautomation.com/t/adding-values-using-if-statements/38130/12 "2020-09-21T19:07:46Z")

</div>

Ok. So your expression with all the “if” functions is on a numeric display? And the indirect tags you need within that are only for that numeric display’s use? Then I’d create custom properties for them right on that display component.

---

<div class="post-metadata">

**Author:** ![mcgheeiv](https://avatars.discourse-cdn.com/v4/letter/m/71c47a/32.png) [@mcgheeiv](https://forum.inductiveautomation.com/u/mcgheeiv)\
**Post date:** [September 21, 2020, 9:59pm UTC](https://forum.inductiveautomation.com/t/adding-values-using-if-statements/38130/13 "2020-09-21T21:59:02Z")

</div>

All of Phil’s comments are correct… The “then” portion of your “if” statement appears to return an empty string, change this to a 0 and it might work a bit better. I would still follow Phil’s advice.

---

<div class="post-metadata">

**Author:** ![Sanderd17](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/sanderd17/32/8735_2.png) [@Sanderd17](https://forum.inductiveautomation.com/u/Sanderd17)\
**Post date:** [September 22, 2020, 7:48am UTC](https://forum.inductiveautomation.com/t/adding-values-using-if-statements/38130/14 "2020-09-22T07:48:45Z")

</div>

> [@mhicks1](#):
>
> Would those custom properties with indirect bindings live in the Template properties or in the Display where I am doing all of the If statements? I have tried both places but have not been successful. Thank you for your reply.

It doesn't matter where they live, both should work. Just try to be consistent across your code base. The only thing you have to watch out for with templates is that they have two types of properties: properties that come from the parent, and internal properties. So perhaps it's easier to define them on the display component.

The procedure:

- Right click on the component \> Customizers \> Custom properties
- Add a custom property for every `tag()` call in your expression (with a name you can understand, and the correct datatype)
- For every custom property, define an indirect binding with the parameters as you had the `tag()` expressions (hint, you can right click on a property, and copy the binding to another property, that may make the configuration faster)
- replace all the `tag()` functions in the expressions with references to the custom properties

That will clean up the expression a whole lot, and make it easier to see what's going wrong. If you want to clean up the expressions more, you can even add custom properties for the if clauses (one property per line on your expression).

The end result will be a lot of understandable datapoints, that can be used to troubleshoot where something is going wrong.
