Page 1 of 2

Adding and Averaging Variables in Python.

PostPosted: Tue Jul 29, 2014 3:47 pm
by jltnol
So I've got some variable in Indigo, bring them into a Python script, and need to list them, add them, and average them, and send out via email. When I run the math part in Terminal, it works fine...
Code: Select all
>>> theVar1 = 1
>>> theVar2 = 2
>>> theVar3 = 3
>>> theSum = theVar1+theVar2+theVar3
>>> theAvg = theSum/3
>>>
>>> print theSum, theAvg
6 2
>>>


But the best I can get for adding them together in Indigo is not 1+2+3 = 6, but just the numbers sequentially - 123. And nothing I've tried for the average has worked. I have consulted this page
http://wiki.indigodomo.com/doku.php?id= ... g_tutorial
which was very helpful in getting just the listing and the emailing done properly, but don't see anything that relates to the math with the variables I'm trying to do here. I've listed a few of the things I've tried for both theSum and theAvg, but, as I said, so far, nothing.

I even tried for theSum using
Code: Select all
theSum1 = indigo.variables[2343]+indigo.variables[45643]
but that didn't work either.. :(


Hoping this is an easy one to solve.....

This is the current script, with a few variations, which don't work..



Code: Select all
# Listing variables, adding together, averaging, and emailing
theVar1 = indigo.variables[203610088]
theVar2 = indigo.variables[1949474627]
theVar3 = indigo.variables[1721091434]

theSum1 = (theVar1.value)+(theVar2.value)+(theVar3.value)
theSum2 = theVar1.value+theVar2.value+theVar3.value

theAvg1 = theSum1/3
theAvg2 = (theSum1)/3
theAvg3 = theSum1.value/3



theSubject = "Indigo Variables"
theBody = "The Variable #1 is %s\n The Variable #2 is %s\nThe Total is %s/n the Average is %s" % (theVar1.value, theVar2.value, theSum1, theAvg1)
indigo.server.sendEmailTo("jltnol@xyz.com", subject=theSubject, body=theBody)

Re: Adding and Averaging Variables in Python.

PostPosted: Tue Jul 29, 2014 4:38 pm
by FlyingDiver
Try:

Code: Select all
# Listing variables, adding together, averaging, and emailing
theVar1 = indigo.variables[203610088]
theVar2 = indigo.variables[1949474627]
theVar3 = indigo.variables[1721091434]

theSum = int(theVar1.value)+int(theVar2.value)+int(theVar3.value)

theAvg = theSum1/3.0




theSubject = "Indigo Variables"
theBody = "The Variable #1 is %s\n The Variable #2 is %s\nThe Total is %s/n the Average is %s" % (theVar1.value, theVar2.value, theSum, theAvg)
indigo.server.sendEmailTo("jltnol@xyz.com", subject=theSubject, body=theBody)


You need to convert from strings to integers. Or maybe floats, depending on what those values really are.

Re: Adding and Averaging Variables in Python.

PostPosted: Tue Jul 29, 2014 5:59 pm
by jltnol
BEAUTIFUL!!

This line kicked out during compile, but was super easy to spot, change, and fix!

Code: Select all
theAvg = theSum1/3.0


should have been

Code: Select all
theAvg = theSum/3.0



Once again... THANKS!!

Re: Adding and Averaging Variables in Python.

PostPosted: Sat Jan 03, 2015 9:20 am
by boekweg
I need to add several "current loads" from different Fibaro wall plugs, I tried the script as mentioned above but it returns an error, it does not recognize the key id from my wall plugs.
How do I address the current load and total load from these wall plugs? where do I specify that I need this particular data?

Hope anyone can help

Re: Adding and Averaging Variables in Python.

PostPosted: Sat Jan 03, 2015 9:35 am
by autolog
See this documentation :)

Sample code for two devices:
Code: Select all
calculated = (indigo.devices[12345678].energyCurLevel + indigo.devices[87654321].energyCurLevel) / 2
indigo.server.log(u'TEST VALUE = %s' % (str(calculated)))
Use energyAccumTotal instead of energyCurLevel for the total accumulated load.
Just substitute your device Ids for the 12345678 and 87654321 numbers.

Hope this helps :)

Re: Adding and Averaging Variables in Python.

PostPosted: Sat Jan 03, 2015 10:54 am
by boekweg
Thanks a lot Jon, works perfect!
Question: How do I discover the names of the variables like "energyCurLevel"
Is there a place I can find these parameters?

Re: Adding and Averaging Variables in Python.

PostPosted: Sat Jan 03, 2015 10:58 am
by autolog
Yes - the first line of my previous response - the documentation link :D

EDIT: Device Base Class Properties :)

Re: Adding and Averaging Variables in Python.

PostPosted: Sat Jan 03, 2015 11:55 am
by boekweg
Thanks a lot, that has been very helpful !

Re: Adding and Averaging Variables in Python.

PostPosted: Sun Jan 11, 2015 9:56 am
by richo
Is it possible to gent Energy Level in apple script?

Re: Adding and Averaging Variables in Python.

PostPosted: Sun Jan 11, 2015 6:02 pm
by jay (support)
AppleScript doesn't have access to the energy fields, but you can use the Insert Device State into Variable action whenever the energy field you're interested in changes so that you can get the value from the variable in AppleScript.

Re: Adding and Averaging Variables in Python.

PostPosted: Sun Apr 05, 2015 10:42 am
by Korey
How about some help for a Python newbie on this one:

I'm trying to take an average of 6 Variables and write them back to another Variable in Indigo.

Code: Select all
# Listing variables, adding together, averaging.
theVar1 = indigo.variables[895595021]
theVar2 = indigo.variables[1244034327]
theVar3 = indigo.variables[545475570]
theVar4 = indigo.variables[1191740502]
theVar5 = indigo.variables[142629916]
theVar6 = indigo.variables[1877833097]
theSum = int(theVar1.value)+int(theVar2.value)+int(theVar3.value)+int(theVar4.value)+int(theVar5.value)+int(theVar6.value)
theAvg = theSum/6.0
indigo.variable.updateValue(59002876, theAvg) # Write the updated value back to Indigo



Script fails on line 10, trying to write the average back to a variable. :(

Code: Select all
 Script Error                    embedded script: Python argument types in
    VariableCmds.updateValue(VariableCmds, int, float)
did not match C++ signature:
    updateValue(_VariableCmds {lvalue}, boost::python::api::object elem, CCString value)
  Script Error                    Exception Traceback (most recent call shown last):

     embedded script, line 10, at top level
ArgumentError: Python argument types in
    VariableCmds.updateValue(VariableCmds, int, float)
did not match C++ signature:
    updateValue(_VariableCmds {lvalue}, boost::python::api::object elem, CCString value)


Thanks in advance!

Re: Adding and Averaging Variables in Python.

PostPosted: Sun Apr 05, 2015 11:07 am
by autolog
All Indigo variables are stored as strings, even if they look like an int or float - see Indigo Variable Documentation :)

So you need to change the line:
Code: Select all
theAvg = theSum/6.0
to
Code: Select all
theAvg = str(theSum/6.0)
Hope this helps :)

Re: Adding and Averaging Variables in Python.

PostPosted: Sun Apr 05, 2015 1:33 pm
by Korey
autolog wrote:
All Indigo variables are stored as strings, even if they look like an int or float - see Indigo Variable Documentation :)

So you need to change the line:
Code: Select all
theAvg = theSum/6.0
to
Code: Select all
theAvg = str(theSum/6.0)
Hope this helps :)



Thanks Jon!! Works perfect!

Now I have a master volume value for my 6 Airfoil Volumes!

:D :D :D

Re: Adding and Averaging Variables in Python.

PostPosted: Fri May 29, 2015 5:23 am
by boekweg
I have another one, I just cam't figure out why it's not working.

I want to extract one variable value from another and put the result in a third variable.

theVar1 = indigo.variables[258092774]
theVar2 = indigo.variables[1797074476]

theDif = str(theVar1.value)-str(theVar2.value)

indigo.variable.updateValue(783072313,'%s kWh' % ((theDif)))

When I replace the - for a plus it does update the variable theDif but displays in this variable the value of theVar1 and theVar2 together
When I use the - operand it returns an error. It is probably something with the value of the indigo.variables but I can't find it.

Anyone an idea?

Re: Adding and Averaging Variables in Python.

PostPosted: Fri May 29, 2015 5:27 am
by FlyingDiver
You need to pay attention to when the data is in string form vs integer form.

Code: Select all
theVar1 = indigo.variables[258092774]
theVar2 = indigo.variables[1797074476]

theDif =str( int(theVar1.value) - int(theVar2.value))

indigo.variable.updateValue(783072313,'%s kWh' % ((theDif)))