Applescript to Python Conversion

Posted on
Fri Dec 13, 2019 10:07 am
mpleeds offline
Posts: 57
Joined: Jul 02, 2012

Applescript to Python Conversion

I Finally found some time to upgrade from 2008 Mac pro to 2018 Mac Mini i7 6 core / Indigo 7.4 so will need to convert Applescripts to Python. Can anyone please help in providing some basic syntax conversion guidance for the following Applescript that is used primarily as differential temperature control for whole house fan / HVAC system:
Code: Select all
set insideTempVal to value of variable "2251_indoorTemp" as number
if insideTempVal is 32 then
   return
end if

-- Set the value of a variable.
-- If the variable doesn't exist, create it.
on stringForHVACMode(inMode)
   if (inMode = offMode) then
      return "offMode"
   else if (inMode = heatOn) then
      return "heatOn"
   else if (inMode = coolOn) then
      return "coolOn"
   else if (inMode = heatCoolOn) then
      return "heatCoolOn"
   else if (inMode = runProgramHeat) then
      return "runProgramHeat"
   else if (inMode = runProgramCool) then
      return "runProgramCool"
   else if (inMode = runProgramAuto) then
      return "runProgramAuto"
   end if
end stringForHVACMode

set ACstate to value of variable "NorthAC" as boolean
set NACstandby to value of variable "NAC_standby" as boolean

set outsideTempVal to value of variable "2251_outdoorTemp" as number
set fanIsOn to on state of device "Whole House Switch"
set windowIsOpen to on state of device "triggerlinc"
set indoorTempHi to value of variable "2251_indoorTempHi" as boolean

if insideTempVal ≤ 73.8 or outsideTempVal - 0.5 > insideTempVal or not windowIsOpen then
   if fanIsOn then
      turn off "Whole House Switch"
      log "1.inside temp lower than outside-fan off"
   end if
else if windowIsOpen and insideTempVal > 74 and outsideTempVal + 0 ≤ insideTempVal then
   if not fanIsOn then
      turn on "Whole House Switch"
      log "2.Fan going on"
   end if
end if
Last edited by mpleeds on Sun Dec 15, 2019 7:16 pm, edited 1 time in total.

Posted on
Fri Dec 13, 2019 10:32 am
whmoorejr offline
User avatar
Posts: 762
Joined: Jan 15, 2013
Location: Houston, TX

Re: Applescript to Python Conversion

I don't know if this will help or not.... I do something similar, but instead of my stuff happening automatically, I get a pushover message letting me know that the AC is on and it's cooler outside than inside. (Then I can decide if I want to open a door/window/etc.). I have a similar set up for when it's hotter outside and the heater is on.

I use triggers.
1) Temp warmer outside.
2) Temp cooler outside.

Example of "Temp cooler outside"

Trigger:
Type: Variable Changed
Variable: outsideTempF changes

Condition: All
If device Thermostat Mode is Cool is true
if variable outsideTempF is less than variable InsideTempF

Actions:
disable trigger "Temp cooler outside" (auto-enable after 6 hours) So the trigger doesn't keep firing all day with the temp difference.
execute embedded python script:
Code: Select all
theMode = indigo.variables[143562231].value
isCool = indigo.variables[285071090].value
isHeat = indigo.variables[1231095420].value
Temp = indigo.variables[796298002].value
Outside = indigo.variables[1927657870].value


TheMessage = '%s\n%s \n%s %s\n%s %s' % ("Cool Day", "AC is on and it's cooler outside.", "Inside Temp:", Temp, "Outside Temp:", Outside)

indigo.variable.updateValue(195713191, value=TheMessage)

#Send Pushover message
import requests
r = requests.post("https://api.pushover.net/1/messages.json", data = {
  "token": "YOUR_TOKEN_NUMBER_HERE",
  "user": "YOUR_PUSHOVER_USER_ID_HERE",
  "message": TheMessage
})
print(r.text)

indigo.server.log(TheMessage)

Sorry I couldn't be more of a help. Maybe this will get you going in the right direction.

Bill
My Plugin: My People

Posted on
Fri Dec 13, 2019 11:17 am
jay (support) offline
Site Admin
User avatar
Posts: 18199
Joined: Mar 19, 2008
Location: Austin, Texas

Re: Applescript to Python Conversion

For completeness, here's a straight conversion of the script (with a few changes - look for NOTE in the comments about those):

Code: Select all
# set insideTempVal to value of variable "2251_indoorTemp" as number
inside_temp_val = indigo.variables[IDOF_2251_indoorTemp].getValue(float)

# if insideTempVal is 32 then
#    return
# end if
# NOTE: rather than bail if the temp is equal to 32, we'll only continue
# if the temp is not equal to 32
if inside_temp_val != 32:
    # NOTE: I didn't convert the stringForHVACMode handler in AppleScript because
    # it's not used in the script. If it is, it can be converted to something much
    # simpler in Python.
   
    # set ACstate to value of variable "NorthAC" as boolean
    ac_state = indigo.variables[IDOF_NorthAC].getValue(bool)
    # set NACstandby to value of variable "NAC_standby" as boolean
    nac_standby = indigo.variables[IDOF_NAC_standby].getValue(bool)

    # set outsideTempVal to value of variable "2251_outdoorTemp" as number
    outside_temp_val = indigo.variables[IDOF_2251_outdoorTemp].getValue(float)

    # set fanIsOn to on state of device "Whole House Switch"
    whole_house_switch = indigo.devices[IDOF_Whole_House_witch]
    # set windowIsOpen to on state of device "triggerlinc"
    window_is_open = indigo.devices[IDOF_triggerlinc].onState
    # set indoorTempHi to value of variable "2251_indoorTempHi" as boolean
    indoor_temp_hi = indigo.variables[IDOF_2251_indoorTempHi].getValue(bool)

    # if insideTempVal ≤ 73.8 or outsideTempVal - 0.5 > insideTempVal or not windowIsOpen then
    #    if fanIsOn then
    #       turn off "Whole House Switch"
    #       log "1.inside temp lower than outside-fan off"
    #    end if
    # else if windowIsOpen and insideTempVal > 74 and outsideTempVal + 0 ≤ insideTempVal then
    #    if not fanIsOn then
    #       turn on "Whole House Switch"
    #       log "2.Fan going on"
    #    end if
    # end if
    if inside_temp_val <= 73.8 or ((outside_temp_val-0.5) > inside_temp_val) or not window_is_open:
        if whole_house_switch.onState:
            indigo.device.turnOff(whole_house_switch)
            indigo.server.log("1.inside temp lower than outside-fan off")
    elif window_is_open and inside_temp_val > 74 and ((outside_temp_val+0) <= inside_temp_val):
        if not whole_house_switch.onState:
            indigo.device.turnOn(whole_house_switch)
            indigo.server.log("2.Fan going on")


Untested, but it should be pretty close.

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Posted on
Thu Dec 26, 2019 9:28 pm
mpleeds offline
Posts: 57
Joined: Jul 02, 2012

Re: Applescript to Python Conversion

Works like a charm, thank you Jay.

Page 1 of 1

Who is online

Users browsing this forum: No registered users and 1 guest