Page 1 of 1

Convert Variable Modification to Python

PostPosted: Wed Oct 02, 2019 7:23 am
by Betacruxis
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

Re: Convert Variable Modification to Python

PostPosted: Wed Oct 02, 2019 3:52 pm
by jay (support)
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.

Re: Convert Variable Modification to Python

PostPosted: Mon Oct 07, 2019 3:31 pm
by Betacruxis
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.

Re: Convert Variable Modification to Python

PostPosted: Tue Nov 26, 2019 7:11 pm
by mgolden50
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

Re: Convert Variable Modification to Python

PostPosted: Tue Nov 26, 2019 7:35 pm
by matt (support)
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))