Convert Variable Modification to Python

Posted on
Wed Oct 02, 2019 7:23 am
Betacruxis offline
Posts: 56
Joined: Apr 03, 2010

Convert Variable Modification to Python

Hello,
This is an embedded Applescript I use in several triggers (when some Indigo variables change).
It simply takes variables values, processes, converts and re-writes them.
Would you please be so kind to translate it into Python script?
I'm not familiar with Python, but I intend to get my hands on it soon.
Thanks in advance!

Code: Select all
set Hum to round ((value of variable "Suite_Hum") as integer) + 0
if Hum > 100 then
   set value of variable "Suite_Hum" to 100
else
   set value of variable "Suite_Hum" to Hum
end if

set lum to round ((value of variable "Suite_Lum") as integer)
set value of variable "Suite_Lum" to lum

set temp to round ((((value of variable "Suite_Temp") as integer) - 32) * 5 / 9) - 1
set value of variable "Suite_Temp" to temp

Posted on
Wed Oct 02, 2019 3:52 pm
jay (support) offline
Site Admin
User avatar
Posts: 18200
Joined: Mar 19, 2008
Location: Austin, Texas

Re: Convert Variable Modification to Python

First, a note about your script: the first two uses of round don't seem to do anything at all since you are rounding AFTER you convert to an integer. Calling round() on an integer will just return the integer. The first section then adds 0, which again does nothing. So, I'll just skip these parts.

Code: Select all


Here's a pretty straight-forward conversion:

Code: Select all
suite_hum_var = indigo.variables[ID_OF_Suite_Hum]
hum = suite_hum_var.getValue(int)

if hum > 100:
    indigo.variable.updateValue(suite_hum_var, value="100")
else:
    indigo.variable.updateValue(suite_hum_var, str(hum))

lum = indigo.variables[ID_OF_Suite_Lum]
indigo.variables.updateValue(lum, value=str(lum.getValue(int)))

suite_temp_var = indigo.variables[ID_OF_Suite_Temp]
old_temp = suite_temp_var.getValue(int)
temp = round((old_temp - 32) * 5 / 9) - 1
indigo.variable.updateValue(suite_temp_var, value=str(temp))


I think that's pretty close.

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Posted on
Mon Oct 07, 2019 3:31 pm
Betacruxis offline
Posts: 56
Joined: Apr 03, 2010

Re: Convert Variable Modification to Python

Perfect!
Thank you very much.

I'm starting to read and study Python online and is pretty straightforward.
I'm also using your reference page on IndigoDomo.
I thought the change would be difficult, but it isn't.
Yes, I noticed the issue of the rounding after converting... I'm a rookie!

Thanks again.

Posted on
Tue Nov 26, 2019 7:11 pm
mgolden50 offline
User avatar
Posts: 247
Joined: Jan 29, 2007
Location: Chandler, AZ

Re: Convert Variable Modification to Python

I'm having great difficulty with Python in converting an Indigo variable value to an integer. Here is the code that fails
and the error message. I know that the Indigo variable is a numeric string.

TemperatureOutsideDailyHighPointSun = int(indigo.variables["TemperatureOutsideDailyHighPointSun"].value)
indigo.server.log(TemperatureOutsideDailyHighPointSun)

This is the error I get.

ArgumentError: Python argument types in
ServerInfo.log(int)
did not match C++ signature:
log(CCString message, CCString type='', bool isError=False)

What am I doing wrong?
Thanks

Posted on
Tue Nov 26, 2019 7:35 pm
matt (support) offline
Site Admin
User avatar
Posts: 21411
Joined: Jan 27, 2003
Location: Texas

Re: Convert Variable Modification to Python

The problem is actually with your second line that tries to log the value. The argument to indigo.server.log() is always a string, but you are passing it an integer. So to get it working cast it to a unicode string (could cast it just to a normal string, but unicode is generally always best):

indigo.server.log(unicode(TemperatureOutsideDailyHighPointSun))

Image

Page 1 of 1

Who is online

Users browsing this forum: No registered users and 0 guests