# Trouble with aggerage functions

**URL:** <https://forum.inductiveautomation.com/t/trouble-with-aggerage-functions/2995>\
**Category:** Ignition\
**Created:** [June 6, 2011, 10:06pm UTC](https://forum.inductiveautomation.com/t/trouble-with-aggerage-functions/2995 "2011-06-06T22:06:32Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Matthew\_Temple](https://avatars.discourse-cdn.com/v4/letter/m/7ea924/32.png) [@Matthew\_Temple](https://forum.inductiveautomation.com/u/Matthew_Temple)\
**Post date:** [June 6, 2011, 10:06pm UTC](https://forum.inductiveautomation.com/t/trouble-with-aggerage-functions/2995/1 "2011-06-06T22:06:32Z")

</div>

I am trying to do some math in my application.

I get name errors on sum() and mean() functions.

When I try the basic sum(1,2,3) or mean(1,2,3) it still pops the name error.

I am using Ignition 7.2.6(b128)

Matt

---

<div class="post-metadata">

**Author:** ![Travis.Cox](https://avatars.discourse-cdn.com/v4/letter/t/b9bd4f/32.png) [@Travis.Cox](https://forum.inductiveautomation.com/u/Travis.Cox)\
**Post date:** [June 9, 2011, 4:44pm UTC](https://forum.inductiveautomation.com/t/trouble-with-aggerage-functions/2995/2 "2011-06-09T16:44:18Z")

</div>

Where are you using the functions? Can you post the exact error message (with stack trace if applicable)?

---

<div class="post-metadata">

**Author:** ![Matthew\_Temple](https://avatars.discourse-cdn.com/v4/letter/m/7ea924/32.png) [@Matthew\_Temple](https://forum.inductiveautomation.com/u/Matthew_Temple)\
**Post date:** [June 12, 2011, 8:07pm UTC](https://forum.inductiveautomation.com/t/trouble-with-aggerage-functions/2995/3 "2011-06-12T20:07:35Z")

</div>

Travis,

I was trying to code something instead of binding number displays to an expression. The sum function did not work within the coding part, but when I did bind it to an expression, it worked just fine.

I have made a test button on my project to demonstrate an example of the problem.

The code…

nsum = sum(1,2,3)  
system.gui.messageBox(“The answer is %d” % nsum)

Traceback (innermost last):  
File “event:actionPerformed”, line 1, in ?  
NameError: sum

Ignition v7.2.6 (b128)  
Java: Sun Microsystems Inc. 1.6.0\_24

---

<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:** [June 13, 2011, 11:15am UTC](https://forum.inductiveautomation.com/t/trouble-with-aggerage-functions/2995/4 "2011-06-13T11:15:01Z")

</div>

Perhaps we’re missing some sort of import , but in the meantime, I got around it by creating a script in the Module editor:

 ![](https://us1.discourse-cdn.com/inductiveautomation/original/1X/964cdacd306746892409e58e5ad6fe23ea988ee3.png)  

That way you can use it globally:  
 ![](https://us1.discourse-cdn.com/inductiveautomation/original/1X/eb9a499de002a6721fb64067e215da5fc9044046.png)

I defined sumargs as float, because if all the arguments are integers, then the result will also be an integer-- and a truncated one at that. Not so much a problem for sums, but for means that can be annoying! 😆

You can simply import this file into the Script Module Editor…  
[math.py](https://us1.discourse-cdn.com/inductiveautomation/original/1X/006b2ca4bca81e7f7eddd93bf4fbf5a69ee8eaf8.py) (222 Bytes)

…or, if you’re a copy and paste kind of guy: ![:mrgreen:](https://us1.discourse-cdn.com/inductiveautomation/original/1X/d23659b203d2d24d394be983a52adb2fa6fe23d4.gif "Mr. Green")

[code]def sum(\*args):  
sumargs=float(0)  
for i in args:  
sumargs += i  
return sumargs

def mean(\*args):  
sumargs=float(0)  
countargs=0  
for i in args:  
sumargs += i  
countargs += 1  
meanargs=sumargs/countargs  
return meanargs[/code]

---

<div class="post-metadata">

**Author:** ![Matthew\_Temple](https://avatars.discourse-cdn.com/v4/letter/m/7ea924/32.png) [@Matthew\_Temple](https://forum.inductiveautomation.com/u/Matthew_Temple)\
**Post date:** [June 13, 2011, 2:39pm UTC](https://forum.inductiveautomation.com/t/trouble-with-aggerage-functions/2995/5 "2011-06-13T14:39:30Z")

</div>

Jordan,

Thanks. I guess I got confused on the manual. Since this is listed under the expression functions appendix I automatically assumed that it was available to the scripting.

---

<div class="post-metadata">

**Author:** ![Travis.Cox](https://avatars.discourse-cdn.com/v4/letter/t/b9bd4f/32.png) [@Travis.Cox](https://forum.inductiveautomation.com/u/Travis.Cox)\
**Post date:** [June 13, 2011, 3:10pm UTC](https://forum.inductiveautomation.com/t/trouble-with-aggerage-functions/2995/6 "2011-06-13T15:10:23Z")

</div>

It is available as an expression function. Expression functions can only be used in expression bindings not in scripting. Appendix B of the user manual are expression functions and Appendix C are scripting functions. All scripting functions start with system… Jordan just created a couple of functions to do the same thing as the expression functions. Hope this helps.

---

<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:** [June 13, 2011, 6:59pm UTC](https://forum.inductiveautomation.com/t/trouble-with-aggerage-functions/2995/7 "2011-06-13T18:59:10Z")

</div>

I found out why it wouldn’t work originally. The current python implementation in Ignition is v2.1-- sum() wasn’t added until v2.3.
