Page 1 of 1

Applescript Conversion to Python

PostPosted: Tue Apr 27, 2021 2:26 pm
by ckeyes888
Hey,

Lil help converting this:
Code: Select all
set value of variable "wsTemp_Out" to (value of variable "wsTemp_Out" as integer)

to python?

Thanks,

Carl

Re: Applescript Conversion to Python

PostPosted: Tue Apr 27, 2021 9:07 pm
by whmoorejr
ckeyes888 wrote:
Code: Select all
set value of variable "wsTemp_Out" to (value of variable "wsTemp_Out" as integer)


I think....

Code: Select all
wsTempOut = indigo.variables[123456]

# Getting the integer value
intValue = wsTempOut.getValue(int)   # 0 if it can't be converted
intValue = wsTempOut.getValue(int, default=10) # 10 if it can't be converted

indigo.variable.updateValue(123456, value=intValue)


However, I think a variable is always a string. You can convert it to a integer to use somewhere else in a phthon code, but I don't think you can save it back as an indigo variable that way.... but not sure.

Also... take a look at this... It goes into way more detail... https://wiki.indigodomo.com/doku.php?id=indigo_5_documentation:variable_class

Re: Applescript Conversion to Python

PostPosted: Wed Apr 28, 2021 7:15 am
by ckeyes888
That script returns: "Python argument types in"

I'll have a look at your link.

Thanks,

Carl

Re: Applescript Conversion to Python

PostPosted: Wed Apr 28, 2021 10:26 am
by jay (support)
whmoorejr wrote:
However, I think a variable is always a string. You can convert it to a integer to use somewhere else in a phthon code, but I don't think you can save it back as an indigo variable that way.... but not sure.


You need to cast it to a string when setting the value:

Code: Select all
indigo.variable.updateValue(123456, value=str(intValue))

Re: Applescript Conversion to Python

PostPosted: Wed Apr 28, 2021 10:27 am
by jay (support)
ckeyes888 wrote:
Hey,

Lil help converting this:
Code: Select all
set value of variable "wsTemp_Out" to (value of variable "wsTemp_Out" as integer)

to python?


What are you trying to do with this script? It doesn't appear to do anything because variable values are always strings. So, basically, you're just setting it to exactly what it was (unless it wasn't something that can be converted to an integer in which case the script will fail).

[EDIT] - ok, so maybe if something else sets it and it's a float value, then this AppleScript would convert it to an integer. If that's the case, then the posted script (with the string cast) would work.

Re: Applescript Conversion to Python

PostPosted: Mon Jun 07, 2021 1:19 pm
by rszmetro
Hi,

I'm upgrading from 7.1 to the latest version and need to convert Applescripts to Python scripts. I've never done this before and an example would help me understand. Hers is an AppleScript I need to convert.
----------
-- Update Upstairs Temperature

tell application "IndigoServer"
set myThermoDev to device "Thermostat - A/C"
set tempList to temperatures of myThermoDev
set TempUpStairs to first item of tempList as real
set value of variable "tempUpstairs" to first item of tempList as real
end tell
----------

Thanks!
Rob Z

Re: Applescript Conversion to Python

PostPosted: Mon Jun 07, 2021 2:14 pm
by FlyingDiver
rszmetro wrote:
Hi,

I'm upgrading from 7.1 to the latest version and need to convert Applescripts to Python scripts. I've never done this before and an example would help me understand. Hers is an AppleScript I need to convert.
----------
-- Update Upstairs Temperature

tell application "IndigoServer"
set myThermoDev to device "Thermostat - A/C"
set tempList to temperatures of myThermoDev
set TempUpStairs to first item of tempList as real
set value of variable "tempUpstairs" to first item of tempList as real
end tell
----------

Thanks!
Rob Z


Where are you running this script? Inside Indigo? If so, is there some reason you can't use the built-in Indigo action to copy the temperature from the device to a variable?

And what's the point of set TempUpStairs to first item of tempList as real? You never use the variable you just set.

Re: Applescript Conversion to Python

PostPosted: Mon Jun 14, 2021 4:11 pm
by rszmetro
I have two thermostats. The downstairs is connected to and controls the heating system. The upstairs master bedroom thermostat is connected to and controls the air conditioning system. Both are Insteon thermostats. I set up Applescripts that determine which thermostat to read and set. I have a variable called TempSet that holds the actual temperature I want to maintain. And, I have a Balancing script that determines which thermostat is read for the actual temperature. When using air-conditioning during the day, I read the temp off the downstairs thermostat and set the upstairs ac thermostat to maintain the temp downstairs according to TempSet. During the night, it reads the temperature off the upstairs thermostat and sets it to maintain the TempSet upstairs.

Re: Applescript Conversion to Python

PostPosted: Mon Jun 14, 2021 4:14 pm
by rszmetro
The scripts are run in a scheduled Action Group


Code: Select all
-- Update Upstairs Temperature

tell application "IndigoServer"
   set myThermoDev to device "Thermostat - A/C"
   set tempList to temperatures of myThermoDev
   set TempUpStairs to first item of tempList as real
   set value of variable "tempUpstairs" to first item of tempList as real
end tell


-- Update Downstairs Temperature

tell application "IndigoServer"
   set myThermoDev to device "Thermostat - HEAT"
   set tempList to temperatures of myThermoDev
   set TempDownStairs to first item of tempList as real
   set value of variable "tempDownstairs" to first item of tempList as real
end tell


-- Calculate tempSetDif

tell application "IndigoServer"
   if value of variable "runClimateControl" = "Cool" then
      set value of variable "tempSetDif" to ((value of variable "tempSet") - (value of variable "tempDownstairs"))
   else if value of variable "runClimateControl" = "Heat" then
      set value of variable "tempSetDif" to ((value of variable "tempSet") - (value of variable "tempUpstairs"))
   end if
end tell



-- Calculate tempSetBal NEW 3/11/13

tell application "IndigoServer"
   if value of variable "runClimateControl" = "Cool" then
      if value of variable "tempDownstairs" is less than value of variable "tempSet" then
         set value of variable "tempSetBal" to ((value of variable "tempUpstairs") + (5))
      else if value of variable "tempDownstairs" is greater than value of variable "tempSet" then
         set value of variable "tempSetBal" to ((value of variable "tempSet") - (5))
      else if value of variable "tempDownstairs" is equal to value of variable "tempSet" then
         set value of variable "tempSetBal" to value of variable "tempSet"
      end if
   else if value of variable "runClimateControl" = "Heat" then
      if value of variable "tempUpstairs" is less than value of variable "tempSet" then
         set value of variable "tempSetBal" to ((value of variable "tempUpstairs") + 5)
      else if value of variable "tempUpstairs" is greater than value of variable "tempSet" then
         set value of variable "tempSetBal" to ((value of variable "tempSet") + 1)
      else if value of variable "tempUpstairs" is equal to value of variable "tempSet" then
         set value of variable "tempSetBal" to value of variable "tempSet"
      end if
   end if
end tell

Re: Applescript Conversion to Python

PostPosted: Mon Jun 14, 2021 9:35 pm
by kw123
try this. it compiles, but I have not executed it. put it into action/execute script/embedded python
Code: Select all
# Update Upstairs Temperature
myThermoDev = indigo.devices["Thermostat - A/C"]
temp = float(dev.states["Temperature"])
indigo.variable.updateValue("tempUpstairs", str(temp))
## or
indigo.variable.updateValue("tempUpstairs", str(indigo.devices["Thermostat - A/C"].states["Temperature"]))


# Update Downstairs Temperature
myThermoDev = indigo.devices["Thermostat - HEAT"]
temp = float(dev.states["Temperature"])
indigo.variable.updateValue("tempDownstairs", str(temp))

#  Calculate tempSetDif
if indigo.variable["runClimateControl"].value == "Cool":
    indigo.variable.updateValue( "tempSetDif", str( float(indigo.variable["tempSet"].value) - float(indigo.variable["tempDownstairs"].value)) )
elif indigo.variable["runClimateControl"].value == "Heat":
    indigo.variable.updateValue( "tempSetDif", str( float(indigo.variable["tempSet"].value) - float(indigo.variable["tempUpstairs"].value)) )

#   Calculate tempSetBal NEW 3/11/13
if indigo.variable["runClimateControl"].value == "Cool":
    if float(indigo.variable["tempDownstairs"].value) < float(indigo.variable["tempSet"].value):
        indigo.variable.updateValue( "tempSetBal", str( float(indigo.variable["tempUpstairs"].value) + 5 ) )
    elif float(indigo.variable["tempDownstairs"].value) > float(indigo.variable["tempSet"].value):
        indigo.variable.updateValue( "tempSetBal", str( float(indigo.variable["tempSet"].value) - 5 ) )
    else:
        indigo.variable.updateValue( "tempSetBal", indigo.variable["tempSet"].value )

elif indigo.variable["runClimateControl"].value == "Heat":
    if float(indigo.variable["tempUpstairs"].value) < float(indigo.variable["tempSet"].value):
        indigo.variable.updateValue( "tempSetBal", str( float(indigo.variable["tempUpstairs"].value) + 5 ) )
    elif float(indigo.variable["tempUpstairs"].value) > float(indigo.variable["tempSet"].value):
        indigo.variable.updateValue( "tempSetBal", str( float(indigo.variable["tempSet"].value) +1 ) )
    else:
        indigo.variable.updateValue( "tempSetBal", indigo.variable["tempSet"].value )

you could replace "name of device" with devNumber w/o quotes
Karl

Re: Applescript Conversion to Python

PostPosted: Tue Jun 15, 2021 10:15 am
by rszmetro
Thanks! I think the right side of the scripts are cut off?

Rob

Re: Applescript Conversion to Python

PostPosted: Tue Jun 15, 2021 10:20 am
by FlyingDiver
rszmetro wrote:
Thanks! I think the right side of the scripts are cut off?

Rob


There's a horizontal scroll bar below the code block. Or just use the Select All button to copy and paste.

Re: Applescript Conversion to Python

PostPosted: Tue Jun 15, 2021 2:55 pm
by kw123
And please note. The the first section is converted into 2 options.
One several lines and the second a one liner.
Only use one of the options.. just delete one of them

Karl.


Sent from my iPhone using Tapatalk