Adding and Averaging Variables in Python.

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

Re: Adding and Averaging Variables in Python.

Thanks for you help, I do get the same error message. Also if I change the int for a str

Posted on
Fri May 29, 2015 6:52 am
FlyingDiver offline
User avatar
Posts: 7221
Joined: Jun 07, 2014
Location: Southwest Florida, USA

Re: Adding and Averaging Variables in Python.

boekweg wrote:
Thanks for you help, I do get the same error message. Also if I change the int for a str


Post an updated screen shot.

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

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

Re: Adding and Averaging Variables in Python.

here it is
Attachments
error.tiff
error.tiff (190.55 KiB) Viewed 3049 times

Posted on
Fri May 29, 2015 7:14 am
FlyingDiver offline
User avatar
Posts: 7221
Joined: Jun 07, 2014
Location: Southwest Florida, USA

Re: Adding and Averaging Variables in Python.

That's a very different error message.

The string variables you're trying to use aren't just numbers. They have that string (" kWh") at the end. You either need to get rid of it before you do the calculation, or you need to not have it in the variable at all. Without seeing all your code there's no way to know which is better.

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

Posted on
Fri May 29, 2015 7:48 am
autolog offline
Posts: 3991
Joined: Sep 10, 2013
Location: West Sussex, UK [GMT aka UTC]

Re: Adding and Averaging Variables in Python.

As FlyingDiver says you aren't dealing with a valid numeric field. :)

You could change your code to something like this:
Code: Select all
theVar1 = indigo.variables[258092774].value
theVar2 = indigo.variables[1797074476].value
theVar1Float = float(theVar1.split(' ')[0])  # splits the field on a space and you access the first element in the resultant list [0] and converts it to a float as your field has a decimal point
theVar2Float = float(theVar2.split(' ')[0])
theDiffFloat = theVar1Float - theVar2Float
theDiffString = str('%.3f kWh' % theDiff)  # gives you 3 decimal places
indigo.variable.updateValue(783072313, theDiffString)
Note the change to setting theVar1 and theVar2 variables to their values i.e. .value added to end

Double check my transcription of your variable Id numbers as I had to copy them from your screen capture :wink:

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

Re: Adding and Averaging Variables in Python.

Thank you both, I will have a look if I can get rid of the kWh.

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

Re: Adding and Averaging Variables in Python.

It works! I got rid of the kWh in the variables and changed the int to float.

Thanks again.
Attachments
error.tiff
error.tiff (176.43 KiB) Viewed 3026 times

Posted on
Sat Jan 19, 2019 4:55 pm
jltnol offline
Posts: 994
Joined: Oct 15, 2013

Re: Adding and Averaging Variables in Python.

So I'm trying to get this small bit of code to work, but the problem I'm having is my variables are decimal numbers, not whole numbers.

When I manually enter whole numbers into Var1 and Var2, it works fine, but am hoping to get python to deal with the decimals

Code: Select all
# Listing variables, subtracting, updating variable

theVar1 = indigo.variables[392364558]
theVar2 = indigo.variables[908595901]

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

indigo.variable.updateValue(1505943804, ((theDif)))



Here is the error code:
Code: Select all
Script Error                    embedded script: invalid literal for int() with base 10: '52.3'
   Script Error                    Exception Traceback (most recent call shown last):

     embedded script, line 6, at top level
ValueError: invalid literal for int() with base 10: '52.3'

Posted on
Sat Jan 19, 2019 4:58 pm
FlyingDiver offline
User avatar
Posts: 7221
Joined: Jun 07, 2014
Location: Southwest Florida, USA

Re: Adding and Averaging Variables in Python.

You can't do "int()" on a non-integer.

Code: Select all
# Listing variables, subtracting, updating variable

theVar1 = indigo.variables[392364558]
theVar2 = indigo.variables[908595901]

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

indigo.variable.updateValue(1505943804, theDif)

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

Posted on
Sat Jan 19, 2019 6:21 pm
jltnol offline
Posts: 994
Joined: Oct 15, 2013

Re: Adding and Averaging Variables in Python.

its the little things... I would have never figured that out.

THANKS!!

Posted on
Sun Jan 20, 2019 6:29 am
DaveL17 offline
User avatar
Posts: 6756
Joined: Aug 20, 2013
Location: Chicago, IL, USA

Re: Adding and Averaging Variables in Python.

Joe is of course right, but he means you can't do int() on a non-integer--when it's cast as a string.

Code: Select all
>>> a = '52.3'
>>> print(int(a))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: '52.3'

>>> a = 52.3
>>> print(int(a))
52

>>> a = '52'
>>> print(int(a))
52

I came here to drink milk and kick ass....and I've just finished my milk.

[My Plugins] - [My Forums]

Who is online

Users browsing this forum: No registered users and 7 guests

cron