#! Version 103
import subprocess, sys
heat = str(indigo.devices[16890506].states["hvacHeaterIsOn"]) # 03-Thermostat
cool = str(indigo.devices[16890506].states["hvacCoolerIsOn"]) # 03-Thermostat
hvacMode = str(indigo.devices[16890506].states["hvacOperationMode"]) # 03-Thermostat
outdoorTempF = indigo.devices[475937690].states["temperatureF"] # 02-Horry County Airport
supplyTempF = indigo.devices[1918428069].states["sensorValue"] # Supply Duct Sensor
returnTempF = indigo.devices[830102769].states["sensorValue"] # Return Duct Sensor
thermostatTempF = indigo.devices[16890506].states["temperatureInput1"] # Thermostat
if hvacMode == "1":
hvacMode = "Cool"
elif hvacMode == "2":
hvacMode = "Heat/Cool"
elif hvacMode == "3":
hvacMode = "Heat"
# if heat or cool is true, then that mode is now working
file = open("/Users/WilliamBoatright/Documents/Indigo/HVAC-EnergyUsageText.txt","w")
file.write(str(heat)+"\n"+str(cool)+"\n"+str(outdoorTempF)+"\n"+str(hvacMode)+"\n"+str(supplyTempF)+"\n"+str(returnTempF)+"\n"+str(thermostatTempF))
file.close()
indigo.server.log("Heat - "+str(heat)+" Cool - "+str(cool)+" Temp - "+str(outdoorTempF)+" Mode - "+str(hvacMode)+
" Supply - "+str(supplyTempF)+" Return - "+str(returnTempF)+" Thermostat - "+str(thermostatTempF))
applescript = '''
-- get date string
set rightNow to (current date)
set theTime to text -8 thru -1 of ((current date) as «class isot» as string)
set YR to ((year of rightNow) as text)
set MO to ((month of rightNow) as integer)
if MO < 10 then set MO to "0" & MO as text
set MO to MO as text
set DA to ((day of rightNow) as integer)
if DA < 10 then set DA to "0" & DA as text
set DA to DA as text
set rightNow to YR & "/" & MO & "/" & DA & " " & theTime
set rightNow to rightNow as text -- YYYY/MM/DD HH:MM:SS in 24 hour mode
set filePath to "/Users/williamboatright/Documents/Indigo/HVAC-EnergyUsage-Heat&Cool-" & MO & "-" & YR & ".numbers" as string
set newFile to ("Macintosh HD:Users:WilliamBoatright:Documents:Indigo:HVAC-EnergyUsageText.txt")
set theFileContents to paragraphs of (read file newFile)
set theList to theFileContents
set heatStatus to item 1 of theList
set coolStatus to item 2 of theList
set outdoorTempF to item 3 of theList
set mode to item 4 of theList
set supplyTempF to item 5 of theList
set returnTempF to item 6 of theList
set thermostatTempF to item 7 of theList
if heatStatus is equal to "True" or coolStatus is equal to "True" then
set status to "ON "
else
set status to "OFF "
end if
try
POSIX file filePath as alias
true
on error
-- false
tell application "Finder"
set master_file to POSIX file "/Users/williamboatright/Documents/Indigo/HVAC-EnergyUsage-Heat&Cool-Template-v2.0.numbers"
set master_dir to (container of file master_file)
set root_nm to the name of file master_file
set ext to the name extension of file master_file
set root_nm to characters 1 thru ((the offset of ext in root_nm) - 2) of root_nm as text
set duplicated_file to duplicate master_file to master_dir
set the name of duplicated_file to ("HVAC-EnergyUsage-Heat&Cool-" & MO & "-" & YR & ".numbers")
end tell
tell application "Numbers"
activate
open filePath
tell document 1
tell sheet mode
tell table "HVAC-Energy-Usage"
tell cell 6 of column "A"
set value to rightNow
set alignment to center
set vertical alignment to center
end tell
tell cell 6 of column "C"
set the value of cell "C6" to the value of the cell "F1"
set alignment to center
set vertical alignment to center
end tell
end tell
end tell
end tell
save every document with saving
end tell
end try
tell application "Numbers"
activate
open filePath
tell table "HVAC-Energy-Usage" of sheet mode of document 1
add row below last row
tell column "A"
set value of last cell to rightNow
set alignment to center
set vertical alignment to center
end tell
tell column "B"
set value of last cell to status
set alignment to center
set vertical alignment to center
end tell
tell column "I"
set value of last cell to outdoorTempF
set alignment to center
set vertical alignment to center
end tell
tell column "J"
set value of last cell to mode
set alignment to center
set vertical alignment to center
end tell
tell column "K"
set value of last cell to supplyTempF
set alignment to center
set vertical alignment to center
end tell
tell column "L"
set value of last cell to returnTempF
set alignment to center
set vertical alignment to center
end tell
tell column "M"
set value of last cell to thermostatTempF
set alignment to center
set vertical alignment to center
end tell
end tell
save every document with saving
--quit
end tell
'''
args = [item for x in [("-e",l.strip()) for l in applescript.split('\n') if l.strip() != ''] for item in x]
proc = subprocess.Popen(["osascript"] + args ,stdout=subprocess.PIPE )
progname = proc.stdout.read().strip()
sys.stdout.write(str(progname))
wiliamboatright wrote:If I understand your question correctly, I execute this Python 3.7 generated script, with embedded AppleScript (Script Editor 2.7), in a Indigo 7.4 Trigger Action using file action in the type:" Execute Script (Script or file action)". This posted script was working on Catalina OS OK. If it was not something that changed on the Indigo server or app update, then I just don't know why the AppleScript part of this posted Python script just stop working.
wiliamboatright wrote:New to posting in Forums, but have been using Indigo app for several years. As requested, where do I find instructions how to move this Topic Post to a more appropriate forum.
Try changing your AppleScript to do something small and easy to verify -- see if that executes.
import subprocess, sys
doors = ["16-Front Door Sensor" , "17-Garage Door Sensor" , "18-Back Door Sensor" , "19-Breezeway Door Sensor" ,
"20-Z-wave Motion Sensor" , "04-Front Door Lock" , "05-Garage Door Opener"]
scan = 0
file = open("/Users/WilliamBoatright/Documents/Indigo/AwayArmed.txt","w")
for cnt , x in enumerate(doors):
lamp = indigo.devices[x]
if hasattr(lamp, "onState"):
isOn = lamp.onState
if isOn == True and cnt+1 < 6:
file.write(str(x))
scan = 1
break
if isOn == False and cnt+1 > 5:
file.write(str(x))
scan = 1
break
file.close()
indigo.server.log("cnt= " + str(cnt+1) + " Device= " + x + " Status=" + str(isOn) + " scan= " + str(scan))
if cnt + 1 < 6:
applescript="""
set newFile to ("Macintosh HD:Users:XXXXXXXXXXXXX:Documents:Indigo:AwayArmed.txt")
set theFileContents to paragraphs of (read file newFile)
tell application "Messages"
set theNumber to buddy "+xxxxxxxxxx" of service "E:xxxxx@xxx.com"
send "AWAY ARMED ALERT - " & theFileContents & " Is Open" to theNumber
end tell
"""
args = [item for x in [("-e",l.strip()) for l in applescript.split("\n") if l.strip() != ""] for item in x]
proc = subprocess.Popen(["osascript"] + args ,stdout=subprocess.PIPE )
progname = proc.stdout.read().strip()
sys.stdout.write(str(progname))
if cnt + 1 > 5:
applescript="""
set newFile to ("Macintosh HD:Users:xxxxxxxxxxxxx:Documents:Indigo:AwayArmed.txt")
set theFileContents to paragraphs of (read file newFile)
tell application "Messages"
set theNumber to buddy "+xxxxxxxxxx" of service "E:xxxx@xxx.com"
send "AWAY ARMED ALERT - " & theFileContents & " Is Open" to theNumber
end tell
"""
args = [item for x in [("-e",l.strip()) for l in applescript.split("\n") if l.strip() != ""] for item in x]
proc = subprocess.Popen(["osascript"] + args ,stdout=subprocess.PIPE )
progname = proc.stdout.read().strip()
sys.stdout.write(str(progname))
if scan == 0:
applescript="""
tell application "Messages"
set theNumber to buddy "+1xxxxxxx7" of service "E:xxxxx@xxx.com"
send "AWAY ARMED CONFIRMED - All Doors are closed" to theNumber
end tell
"""
args = [item for x in [("-e",l.strip()) for l in applescript.split("\n") if l.strip() != ""] for item in x]
proc = subprocess.Popen(["osascript"] + args ,stdout=subprocess.PIPE )
progname = proc.stdout.read().strip()
sys.stdout.write(str(progname))
'''
#if scan > 0:
applescript="""
set newFile to ("Macintosh HD:Users:WilliamBoatright:Documents:Indigo:AwayArmed.txt")
set theFileContents to paragraphs of (read file newFile)
tell application "Messages"
set theNumber2 to buddy "911" of service "E:xxxxx@xxx.com"
send "POSSIBLE INTRUDER ALARM at xxxxxxxxxxxx xxx xxx xxx Road, xxxxx xx xxxxx - Activated at " & theFileContents & ". Front Door entry code is xxxx - My Cell No: xxxxxxxxxxx7" to theNumber2
end tell
"""
args = [item for x in [("-e",l.strip()) for l in applescript.split("\n") if l.strip() != ""] for item in x]
proc = subprocess.Popen(["osascript"] + args ,stdout=subprocess.PIPE )
progname = proc.stdout.read().strip()
sys.stdout.write(str(progname))
'''
Are you running this script as an external file (i.e. the script action runs a file you select) or embedded into the Action itself?
import subprocess, sys
applescript = 'say "testing"'
args = [item for x in [("-e",l.strip()) for l in applescript.split('\n') if l.strip() != ''] for item in x]
proc = subprocess.Popen(["osascript"] + args ,stdout=subprocess.PIPE )
import subprocess, sys
applescript = '''tell application "Numbers"
say "testing from numbers"
end tell'''
args = [item for x in [("-e",l.strip()) for l in applescript.split('\n') if l.strip() != ''] for item in x]
proc = subprocess.Popen(["osascript"] + args ,stdout=subprocess.PIPE )
wiliamboatright wrote:I've found the culprit. It is a part of the AppleScript code that works OK on Python 3, but not on Python 2.
set OFF to " \"OFF\" "-- for "OFF" in the formula
set END1 to " \"\" " -- for "" in the formula
ell application "Numbers"
activate
open filePath
tell table "HVAC-Energy-Usage" of sheet "Heat" of document 1
add row below last row
--set rowCount to last row's address
set rowCount to row count
set rowcount2 to rowCount - 1
set OFF to " \"OFF\" "
set ON1 to "\"ON\" "
set END1 to " \"\" "
tell column "A"
set value of last cell to rightNow
end tell
tell column "B"
set value of last cell to status
end tell
tell column "C"
set value of last cell to "=IF(B" & rowCount & "=" & OFF & "," & "A" & rowCount & "−" & "A" & rowcount2 & "," & END1 & ")"
end tell
tell column "D"
set value of last cell to "=IF(B" & rowCount & "=" & ON1 & "," & "A" & rowCount & "−" & "A" & rowcount2 & "," & END1 & ")"
end tell
tell column "E"
set value of last cell to "=IF(C" & rowCount & "=" & END1 & "," & END1 & ",DUR2HOURS(C" & rowCount & "))"
end tell
tell column "F"
set value of last cell to "=IF(C" & rowCount & "=" & END1 & "," & END1 & ",E" & rowCount & "×$B$1)"
end tell
tell column "G"
set value of last cell to "=IF(C" & rowCount & "=" & END1 & "," & END1 & ",F" & rowCount & "×$B$2)"
end tell
tell column "H"
set value of last cell to "=IF(C" & rowCount & "=" & END1 & "," & END1 & ",(($D$1×0.75)×E" & rowCount & ")×B$2)"
end tell
tell column "I"
set value of last cell to outdoorTempF
end tell
tell column "J"
set value of last cell to mode
end tell
tell column "K"
set value of last cell to supplyTempF
end tell
tell column "L"
set value of last cell to returnTempF
end tell
tell column "M" # set "Delta-T tempF" formula
set value of last cell to "=IF(B" & rowCount & "=" & OFF & "," & "K" & rowCount & "−" & "L" & rowcount2 & "," & END1 & ")"
end tell
tell column "N" # set "Thermostat tempF"
set value of last cell to thermostatTempF
end tell
tell row rowCount
set alignment to center
set vertical alignment to center
end tell
end tell
save every document with saving
--quit
end tell
Mac-mini:Volumes jay$ python
Python 2.7.16 (default, Jan 27 2020, 04:46:15)
[GCC 4.2.1 Compatible Apple LLVM 10.0.1 (clang-1001.0.37.14)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> using_single_quotes = '=IF(B44= "OFF" ,A44−A43, "" )'
>>> using_multiline_single_quotes = '''=IF(B44= "OFF" ,A44−A43, "" )'''
>>> using_multiline_double_quotes = """=IF(B44= "OFF" ,A44−A43, "" )"""
>>> escaping_double_quotes = "=IF(B44= \"OFF\" ,A44−A43, \"\" )"
>>> escaping_double_quotes == using_multiline_double_quotes == using_multiline_single_quotes == using_single_quotes
True
>>>
Mac-mini:Volumes jay$ python3
Python 3.7.7 (v3.7.7:d7c567b08f, Mar 10 2020, 02:56:16)
[Clang 6.0 (clang-600.0.57)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> using_single_quotes = '=IF(B44= "OFF" ,A44−A43, "" )'
>>> using_multiline_single_quotes = '''=IF(B44= "OFF" ,A44−A43, "" )'''
>>> using_multiline_double_quotes = """=IF(B44= "OFF" ,A44−A43, "" )"""
>>> escaping_double_quotes = "=IF(B44= \"OFF\" ,A44−A43, \"\" )"
>>> escaping_double_quotes == using_multiline_double_quotes == using_multiline_single_quotes == using_single_quotes
True
>>>
Mac-mini:Volumes jay$ python
Python 2.7.16 (default, Jan 27 2020, 04:46:15)
[GCC 4.2.1 Compatible Apple LLVM 10.0.1 (clang-1001.0.37.14)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> original_string = "=IF(B44= "OFF" ,A44−A43, "" )"
File "<stdin>", line 1
original_string = "=IF(B44= "OFF" ,A44−A43, "" )"
^
SyntaxError: invalid syntax
>>> ^D
Mac-mini:Volumes jay$ python3
Python 3.7.7 (v3.7.7:d7c567b08f, Mar 10 2020, 02:56:16)
[Clang 6.0 (clang-600.0.57)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> original_string = "=IF(B44= "OFF" ,A44−A43, "" )"
File "<stdin>", line 1
original_string = "=IF(B44= "OFF" ,A44−A43, "" )"
^
SyntaxError: invalid syntax
>>>
Users browsing this forum: No registered users and 5 guests