Python Script to Add 2 Numbers/Variables

Posted on
Sat Dec 02, 2023 8:25 pm
ckeyes888 offline
Posts: 2425
Joined: Nov 26, 2009
Location: Kalispell, MT

Python Script to Add 2 Numbers/Variables

Ugh, thought this would be an easy conversion from AS but as usual I’m not getting it.
Need to add one variable to another. In this case I need to add the SnowToday variable to the SnowSeason variable.
The existing script is:
Code: Select all
 tell application "IndigoServer"
       set snowDay to the value of variable "SnowToday" as real
       set snowSeason to the value of variable "SnowSeason" as real
       set snowSeason to snowSeason + snowDay
       set the value of variable "SnowSeason" to snowSeason
end tell


Appreciate any help.

Thanks,

Carl

Posted on
Sat Dec 02, 2023 8:33 pm
FlyingDiver offline
User avatar
Posts: 7222
Joined: Jun 07, 2014
Location: Southwest Florida, USA

Re: Python Script to Add 2 Numbers/Variables

Why don't you show what you've tried, so we can help you learn.

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

Posted on
Sat Dec 02, 2023 8:54 pm
ryanbuckner offline
Posts: 1080
Joined: Oct 08, 2011
Location: Northern Virginia

Re: Python Script to Add 2 Numbers/Variables

try this:

Code: Select all
# replace the variable numbers below with the IDs of snowSeason and SnowToday. You can go to the variable in Indigo and right click to get the ID
snowSeason = float(indigo.variables[954090031].value) + float(indigo.variables[954090032].value)
# replace the variable number below with the ID of snowSeason
indigo.variable.updateValue(954090031, value=str(snowSeason))
Last edited by ryanbuckner on Sat Dec 02, 2023 9:14 pm, edited 1 time in total.

Posted on
Sat Dec 02, 2023 9:02 pm
ckeyes888 offline
Posts: 2425
Joined: Nov 26, 2009
Location: Kalispell, MT

Re: Python Script to Add 2 Numbers/Variables

Here’s what I tried:

Code: Select all
 snowDay = indigo.variables[504418172].value
snowSeason = indigo.variables[1813022255].value
snowTotal = snowDay + snowSeason
indigo.variable.updateValue(1813022255,snowTotal)


Thanks,

Carl

Posted on
Sat Dec 02, 2023 9:04 pm
FlyingDiver offline
User avatar
Posts: 7222
Joined: Jun 07, 2014
Location: Southwest Florida, USA

Re: Python Script to Add 2 Numbers/Variables

ckeyes888 wrote:
Here’s what I tried:

Code: Select all
 snowDay = indigo.variables[504418172].value
snowSeason = indigo.variables[1813022255].value
snowTotal = snowDay + snowSeason
indigo.variable.updateValue(1813022255,snowTotal)


Thanks,

Carl


That API returns a string (all Indigo variables are strings). There's an alternate call to get an int or float, or you can cast what you got to a float when you do the addition. You will also need to convert that result to a string when you do the updatevalue() call.

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

Posted on
Sat Dec 02, 2023 9:13 pm
ckeyes888 offline
Posts: 2425
Joined: Nov 26, 2009
Location: Kalispell, MT

Re: Python Script to Add 2 Numbers/Variables

Tried this but get a “Python argument types in” error.
Code: Select all
 snowTotal = float(indigo.variables[1813022255].value) + float(indigo.variables[504418172].value)
indigo.variable.updateValue(1813022255, value=snowTotal)


Thanks,

Carl

Posted on
Sat Dec 02, 2023 9:15 pm
ryanbuckner offline
Posts: 1080
Joined: Oct 08, 2011
Location: Northern Virginia

Re: Python Script to Add 2 Numbers/Variables

Don't forget to convert the snowTotal back to a string

Posted on
Sat Dec 02, 2023 9:16 pm
FlyingDiver offline
User avatar
Posts: 7222
Joined: Jun 07, 2014
Location: Southwest Florida, USA

Re: Python Script to Add 2 Numbers/Variables

Indigo variables are strings. You're trying to set it to a float.

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

Posted on
Sat Dec 02, 2023 9:19 pm
ckeyes888 offline
Posts: 2425
Joined: Nov 26, 2009
Location: Kalispell, MT

Re: Python Script to Add 2 Numbers/Variables

Got it except it’s adding a decimal place. Adding 3 + 1 is returning 4.0.

How to remove?

Thanks,

Carl

Posted on
Sat Dec 02, 2023 9:20 pm
FlyingDiver offline
User avatar
Posts: 7222
Joined: Jun 07, 2014
Location: Southwest Florida, USA

Re: Python Script to Add 2 Numbers/Variables

Well, you're converting to floats, which always have a decimal point. If your inputs are always whole numbers, you should be using integers.

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

Posted on
Sat Dec 02, 2023 9:27 pm
ckeyes888 offline
Posts: 2425
Joined: Nov 26, 2009
Location: Kalispell, MT

Re: Python Script to Add 2 Numbers/Variables

It tried substituting “int” for “floats” but that doesn’t work.
No idea how to do that.

Carl

Posted on
Sat Dec 02, 2023 9:38 pm
FlyingDiver offline
User avatar
Posts: 7222
Joined: Jun 07, 2014
Location: Southwest Florida, USA

Re: Python Script to Add 2 Numbers/Variables


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

Posted on
Sat Dec 02, 2023 9:45 pm
ryanbuckner offline
Posts: 1080
Joined: Oct 08, 2011
Location: Northern Virginia

Re: Python Script to Add 2 Numbers/Variables

ckeyes888 wrote:
It tried substituting “int” for “floats” but that doesn’t work.
No idea how to do that.

Carl


Why don't you share your current code and any errors you're receiving. Also, what are the values of the variables right now?

Posted on
Sun Dec 03, 2023 6:36 pm
ckeyes888 offline
Posts: 2425
Joined: Nov 26, 2009
Location: Kalispell, MT

Re: Python Script to Add 2 Numbers/Variables

Sorry, tried substituting int for float again and it works fine.

Thanks a bunch!

Carl

Posted on
Tue Dec 05, 2023 9:27 pm
ryanbuckner offline
Posts: 1080
Joined: Oct 08, 2011
Location: Northern Virginia

Re: Python Script to Add 2 Numbers/Variables

ckeyes888 wrote:
Sorry, tried substituting int for float again and it works fine.

Thanks a bunch!

Carl


Great. Post your working code for the next person that has the same problem and finds this thread

Who is online

Users browsing this forum: No registered users and 8 guests