Adding and Averaging Variables in Python.

Posted on
Tue Jul 29, 2014 3:47 pm
jltnol offline
Posts: 994
Joined: Oct 15, 2013

Adding and Averaging Variables in Python.

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)

Posted on
Tue Jul 29, 2014 4:38 pm
FlyingDiver offline
User avatar
Posts: 7213
Joined: Jun 07, 2014
Location: Southwest Florida, USA

Re: Adding and Averaging Variables in Python.

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.

joe (aka FlyingDiver)
my plugins: http://forums.indigodomo.com/viewforum.php?f=177

Posted on
Tue Jul 29, 2014 5:59 pm
jltnol offline
Posts: 994
Joined: Oct 15, 2013

Re: Adding and Averaging Variables in Python.

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!!

Posted on
Sat Jan 03, 2015 9:20 am
boekweg offline
Posts: 70
Joined: Oct 02, 2010
Location: Netherlands

Re: Adding and Averaging Variables in Python.

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

Posted on
Sat Jan 03, 2015 9:35 am
autolog offline
Posts: 3988
Joined: Sep 10, 2013
Location: West Sussex, UK [GMT aka UTC]

Re: Adding and Averaging Variables in Python.

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 :)

Posted on
Sat Jan 03, 2015 10:54 am
boekweg offline
Posts: 70
Joined: Oct 02, 2010
Location: Netherlands

Re: Adding and Averaging Variables in Python.

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?

Posted on
Sat Jan 03, 2015 10:58 am
autolog offline
Posts: 3988
Joined: Sep 10, 2013
Location: West Sussex, UK [GMT aka UTC]

Re: Adding and Averaging Variables in Python.

Yes - the first line of my previous response - the documentation link :D

EDIT: Device Base Class Properties :)

Posted on
Sat Jan 03, 2015 11:55 am
boekweg offline
Posts: 70
Joined: Oct 02, 2010
Location: Netherlands

Re: Adding and Averaging Variables in Python.

Thanks a lot, that has been very helpful !

Posted on
Sun Jan 11, 2015 9:56 am
richo offline
Posts: 158
Joined: Nov 25, 2014
Location: Pomorskie, Poland

Re: Adding and Averaging Variables in Python.

Is it possible to gent Energy Level in apple script?

Ryszard

Posted on
Sun Jan 11, 2015 6:02 pm
jay (support) offline
Site Admin
User avatar
Posts: 18216
Joined: Mar 19, 2008
Location: Austin, Texas

Re: Adding and Averaging Variables in Python.

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.

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Posted on
Sun Apr 05, 2015 10:42 am
Korey offline
User avatar
Posts: 813
Joined: Jun 04, 2008
Location: Henderson, NV

Re: Adding and Averaging Variables in Python.

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!

--
Korey

Posted on
Sun Apr 05, 2015 11:07 am
autolog offline
Posts: 3988
Joined: Sep 10, 2013
Location: West Sussex, UK [GMT aka UTC]

Re: Adding and Averaging Variables in Python.

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 :)

Posted on
Sun Apr 05, 2015 1:33 pm
Korey offline
User avatar
Posts: 813
Joined: Jun 04, 2008
Location: Henderson, NV

Re: Adding and Averaging Variables in Python.

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

--
Korey

Posted on
Fri May 29, 2015 5:23 am
boekweg offline
Posts: 70
Joined: Oct 02, 2010
Location: Netherlands

Re: Adding and Averaging Variables in Python.

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?
Attachments
error.tiff
error.tiff (186.31 KiB) Viewed 5144 times

Posted on
Fri May 29, 2015 5:27 am
FlyingDiver offline
User avatar
Posts: 7213
Joined: Jun 07, 2014
Location: Southwest Florida, USA

Re: Adding and Averaging Variables in Python.

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)))

joe (aka FlyingDiver)
my plugins: http://forums.indigodomo.com/viewforum.php?f=177

Who is online

Users browsing this forum: No registered users and 2 guests