Python Arithmetic

Posted on
Thu Sep 05, 2019 9:54 pm
jltnol offline
Posts: 994
Joined: Oct 15, 2013

Python Arithmetic

So I'm trying to average 2 variables, and store it in a 3rd, but keep striking out.

Here's what I've tired:
Code: Select all
theVar1 = indigo.variables[305363100] #Thermostat
theVar2 = indigo.variables[1728838522] #ExtDevice

theAverage =str(float(theVar1.value) + float(theVar2.value)) / 2 #Average

indigo.variable.updateValue(655793661, ((theAverage)))
And I've tried this with a few different placement and spacings of the "/" and the "2", but doesn't work. I even tried adding some "( )" around part of the 1st line, but that didn't work either

I also tried summing in the 1st equation, and dividing in the 2nd, like this:
Code: Select all
theSum =str(float(theVar1.value) + float(theVar2.value)) #Sum
theAverage =theSum / 2

Again trying different arrangements of the "/" and the "2", but still doesn't work.

My last attempt was this:
Code: Select all
theSum =str(float(theVar1.value) + float(theVar2.value)) #Sum
theAverage =str(float(theSum.value)/2) #Average

But again... nothing.

Any suggestions?

Posted on
Thu Sep 05, 2019 10:30 pm
jltnol offline
Posts: 994
Joined: Oct 15, 2013

Re: Python Arithmetic

Ugh

So I DO remember this problem and found my original post
https://forums.indigodomo.com/viewtopic.php?f=107&t=12368

While the original solution worked then, its not working with what I've got now.... and I'm thinking that part of my problem now is some of the variables have decimals... and some don't. (int vs float ??)

Not sure if the variable without the decimal will never have it, and vice versa, but will work on that..

Posted on
Fri Sep 06, 2019 2:23 am
FlyingDiver offline
User avatar
Posts: 7215
Joined: Jun 07, 2014
Location: Southwest Florida, USA

Re: Python Arithmetic

What's with all the extra parenthesis in your first code sample?

Code: Select all
theVar1 = indigo.variables[305363100] #Thermostat
theVar2 = indigo.variables[1728838522] #ExtDevice

theAverage = (float(theVar1.value) + float(theVar2.value)) / 2.0      #Average

indigo.variable.updateValue(655793661, str(theAverage))

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

Posted on
Fri Sep 06, 2019 7:20 am
kw123 offline
User avatar
Posts: 8363
Joined: May 12, 2013
Location: Dallas, TX

Re: Python Arithmetic

just trying to help understanding:
Code: Select all
theAverage =str(float(theVar1.value) + float(theVar2.value)) / 2 #Average
divides a string by 2.. like "12345" /2 , which does not make sense



this does 12345/2:
Code: Select all
theAverage =str(    (float(theVar1.value) + float(theVar2.value) )/ 2   )

Posted on
Sat Sep 07, 2019 1:09 pm
jltnol offline
Posts: 994
Joined: Oct 15, 2013

Re: Python Arithmetic

First of all, THANK YOU for your help. This solved my problem.

I don't pretend to understand all the intricacies of Python, but briefly, I'm assuming this type of equation

(3.5+5.3)/2
is handled differently than this one:
(3+5)/2

And different because of the decimals?

Posted on
Sat Sep 07, 2019 1:14 pm
FlyingDiver offline
User avatar
Posts: 7215
Joined: Jun 07, 2014
Location: Southwest Florida, USA

Re: Python Arithmetic

jltnol wrote:
First of all, THANK YOU for your help. This solved my problem.

I don't pretend to understand all the intricacies of Python, but briefly, I'm assuming this type of equation

(3.5+5.3)/2
is handled differently than this one:
(3+5)/2

And different because of the decimals?


The only difference between those is that since the first numbers are floats, the rest of the calculation will be done with floats. So you'll get 8.8/2 = 4.4. The second is all integers, so you get 8/2 = 4.

The key takeaway from this discussion is that if you retrieve variables using the .value property, you get a string. You can specify the data the when you retrieve the value. Starting with the data type you want makes things easier. So a "better" answer to your original code would be:

Code: Select all
theVar1 = indigo.variables[305363100] #Thermostat
theVar2 = indigo.variables[1728838522] #ExtDevice

theAverage = (theVar1.getValue(float) + theVar2.getValue(float)) / 2.0      #Average

indigo.variable.updateValue(655793661, str(theAverage))


This is documented here: https://wiki.indigodomo.com/doku.php?id ... able_class

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

Posted on
Tue Sep 10, 2019 9:09 am
howartp offline
Posts: 4559
Joined: Jan 09, 2014
Location: West Yorkshire, UK

Re: Python Arithmetic

Or

theVar1 = indigo.variables[305363100].getValue(float)
Etc

Then it’s

(theVar1 + theVar2)/2 which reads easier to me.


Sent from my iPhone using Tapatalk Pro

Posted on
Tue Sep 10, 2019 10:13 am
FlyingDiver offline
User avatar
Posts: 7215
Joined: Jun 07, 2014
Location: Southwest Florida, USA

Re: Python Arithmetic

howartp wrote:
Or

theVar1 = indigo.variables[305363100].getValue(float)
Etc

Then it’s

(theVar1 + theVar2)/2 which reads easier to me.


Agree. But, baby steps....

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

Posted on
Fri Sep 13, 2019 10:42 am
jltnol offline
Posts: 994
Joined: Oct 15, 2013

Re: Python Arithmetic

Agree. But, baby steps....



That's me. I actually can follow the logic... just hope I can remember it next time I need it. I usually "save" my python scripts and a snippet here and there can get me most of where I need to go... nothing very complicated... mostly small things easier done in Python than the GUI.

Thanks again for the help.

Page 1 of 1

Who is online

Users browsing this forum: No registered users and 5 guests