adding 2 variables with python

Forum rules

This is a legacy forum which is locked for new topics. New topics should be started in one of the other forums under Extending Indigo

Posted on
Sat Jul 07, 2012 12:58 pm
ELWOOD offline
Posts: 225
Joined: Feb 11, 2007
Location: Ramsey, NJ

adding 2 variables with python

Trying to learn python by writing my first embedded python script. I want to add two numaric variables than update
the total to a third variable.

This is what I have so far:

someVar=(indigo.variables[547162679].value) ##value=400
someVar2=(indigo.variables[76708743].value) ##value=500

someVar3=someVar+someVar2

indigo.variable.updateValue(31189428, value=someVar3)

The script runs and updates the variable but what I get is 400500 not 900

So how do you add variables???

Thanks for any help

Elwood

Posted on
Sat Jul 07, 2012 1:14 pm
jay (support) offline
Site Admin
User avatar
Posts: 18260
Joined: Mar 19, 2008
Location: Austin, Texas

Re: adding 2 variables with python

All Indigo variable values are strings so when you add them it just appends the strings. To use them as numbers you need to explicitly convert them to integers, add them together, then convert back to a string when you set the other variable:

Code: Select all
try:
    someVar=int(indigo.variables[547162679].value) ##value=400
    someVar2=int(indigo.variables[76708743].value) ##value=500
    someVar3=someVar+someVar2
    indigo.variable.updateValue(31189428, value=str(someVar3))
except:
    indigo.server.log("couldn't make variable values into numbers", isError=True)


Put it into a try block so you'll get a nicer error in the event log if the variable values can't get converted to integers. Untested but it should be close.

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Posted on
Sat Jul 07, 2012 1:31 pm
ELWOOD offline
Posts: 225
Joined: Feb 11, 2007
Location: Ramsey, NJ

Re: adding 2 variables with python

Thanks Jay
That works.
If the variables are floating point, will changing the int to float do the trick.

Thanks
Elwood

Posted on
Sat Jul 07, 2012 2:26 pm
jay (support) offline
Site Admin
User avatar
Posts: 18260
Joined: Mar 19, 2008
Location: Austin, Texas

Re: adding 2 variables with python

Exactly.

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Posted on
Sat Jul 07, 2012 2:56 pm
ELWOOD offline
Posts: 225
Joined: Feb 11, 2007
Location: Ramsey, NJ

Re: adding 2 variables with python

OK, last question for today.
Want to add two variables from Nathan's eTrack script, the format is time 00:00:00 (hour-min-sec)
Do I need to convert to seconds, than add the seconds and change back to hr-min-sec.
If so how is that done.

Thanks
Elwood

Posted on
Mon Jul 09, 2012 10:21 am
jay (support) offline
Site Admin
User avatar
Posts: 18260
Joined: Mar 19, 2008
Location: Austin, Texas

Re: adding 2 variables with python

That's actually quite a bit more difficult, particularly in AppleScript. Here's a Python script that should do it:

Code: Select all
# import the python timedelta class from the datetime module
from datetime import timedelta

# get the two variable values
firstVar = indigo.variables[1199249756]
secondVar = indigo.variables[824299455]

# next convert them to python timedelta objects so they can be
# easily added together - we do this by first splitting
# the "HH:MM:SS" strings stored in the variables into
# separate strings, one each for hour, minute, second, and
# then converting them to numbers to be used to construct
# the timedelta object
(h, m, s) = [int(x) for x in firstVar.value.split(":")]
firstTimeDelta = timedelta(hours=h, minutes=m, seconds=s)
(h, m, s) = [int(x) for x in secondVar.value.split(":")]
secondTimeDelta = timedelta(hours=h, minutes=m, seconds=s)

# add them together
newTimeDelta = firstTimeDelta + secondTimeDelta

# create the new HH:MM:SS string to set the third variable to
h = (newTimeDelta.days*24)+newTimeDelta.seconds//3600
m = (newTimeDelta.seconds//60)%60
s = ((newTimeDelta.days*24*60*60) + (newTimeDelta.seconds)) - ((m*60) + (h*60*60))
newValue = "%02i:%02i:%02i" % (h, m, s)

# finally, set the third indigo variable to newString
indigo.variable.updateValue(1213278799, value=newValue)

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Page 1 of 1

Who is online

Users browsing this forum: No registered users and 3 guests

cron