My script performs the following:
1) turns on my espresso machine
2) calculates time of day when machine will be ready
3) when warmed up, it turns on two lamp to indicate ready
4) it calculates an Auto Off time based on the cost of electricity (from variables)
A lot of the code is dealing with time arithmetic - e.g., adding 15 minutes to 50 minutes, results in 1 Hr and 5 Min. I'm guessing there is an easier way to do that.
I attempted to assist by documenting the script with comments.
Thank you, in advance, for helping the code challenged user.
Bob
- Code: Select all
set theHour to (do shell script "date +%H") as integer
set theMinute to (do shell script "date +%M") as integer
tell application "IndigoServer"
--gather variables
disable time date action "espMachOff"
set hseVacant to the value of variable "hseVacant" as boolean
set espMachHot to the value of variable "espMachHot" as string
set espMachWarmupTime to the value of variable "espMachWarmupTime"
set coldSeason to the value of variable "coldSeason" as boolean
set espMachAvailH to the value of variable "espMachAvailH"
set espMachAvailM to the value of variable "espMachAvailM"
set espMachAvailH_PEAK to the value of variable "espMachAvailH_PEAK"
set espMachAvailM_PEAK to the value of variable "espMachAvailM_PEAK"
set espMachON_AM to the value of variable "espMachON_AM" as boolean
set pgeSched to the value of variable "pgeSched"
set espMachOffHour to the value of variable "espMachOffHour"
set espMachOffMin to the value of variable "espMachOffMin"
if not hseVacant then
--turn on appliance if the house is vacant
turn on "kEspressoMachine"
log "Espresso Machine Turned On" using type "espMachineON.scpt"
log theHour using type "Hour on - espMachineON.scpt"
log theMinute using type "Minute on - espMachineON.scpt"
--calculate time when machine will be ready:
set hotMin to theMinute + espMachWarmupTime
set hotHour to theHour
if hotMin ≥ 60 then
set hotMin to hotMin - 60
set hotHour to theHour + 1
if hotHour > 23 then
set hotHour to hotHour - 24
end if
end if
if hotHour < 10 then
set hotHour to hotHour as string
set hotHour to "0" & hotHour
else
set hotHour to hotHour as string
end if
if hotMin < 10 then
set hotMin to hotMin as string
set hotMin to "0" & hotMin
else
set hotMin to hotMin as string
end if
log hotHour using type "esp mach ready as of (hours)"
log hotMin using type "esp mach ready as of (minutes)"
--modify variables
set the value of variable "espMachHot" to "ON - will be ready as of " & hotHour & ":" & hotMin as string
set the value of variable "espTime" to the value of variable "espMachHot" --used by ctrl page
--light up various keypad buttons
send insteon group instnTurnOn using index 148 --kKpl#3 btn#8
--Using light bulbs to indicate status, turn off "nOffLamp", kTell in warmup mode (white)
execute group "espMachWarmingUP lamp"
turn off "nOffRmDeskLamp"
--calculate time when machine will be turned off:
--delay ((espMachWarmupTime * 60) - 60)
if not espMachON_AM then
if pgeSched ≠ "$.33" then
--for when PGE is selling off peak power
set offHour to hotHour + espMachAvailH
set offMin to hotMin + espMachAvailM
if offMin ≥ 60 then
set offMin to offMin - 60
set offHour to offHour + 1
if offMin < 10 then
set offMin to "0" & offMin as string
end if
end if
--set offMin to offMin as string
--for when PGE is selling peak power
else if pgeSched = "$.33" then
set offHour to hotHour + espMachAvailH_PEAK
set offMin to hotMin + espMachAvailM_PEAK
if offMin ≥ 60 then
set offMin to offMin - 60
set offHour to offHour + 1
if offMin < 10 then
set offMin to "0" & offMin as string
end if
end if
set offMin to offMin as string
end if
if offHour > 24 then
set offHour to offHour - 24
end if
if offHour < 10 then
set offHour to offHour as string
set offHour to "0" & offHour
else
set offHour to offHour as string
log offHour using type "esp mach will turn off (hours)"
log offMin using type "esp mach will turn off (minutes)"
end if
else
--special case - turns off in morning at 11am
set offHour to "11" as string
set offMin to "00" as string
end if
execute group "kEspMachREADY" in (espMachWarmupTime * 60)
set the value of variable "espMachOff" to offHour & ":" & offMin as string
log offHour using type "Off H, espMachineON.scpt"
log offMin using type "Off M, espMachineON.scpt"
enable time date action "espMachOff"
set offSec to "00" as string
set offTime to offHour & ":" & offMin & ":" & offSec
set the value of variable "espMachOffHour" to offHour as number
set the value of variable "espMachOffMin" to offMin
log offTime
set the time trigger type of time date action "espMachOff" to absolute
set the date trigger type of time date action "espMachOff" to everyDay
set the absolute trigger time of time date action "espMachOff" to date offTime
--get properties of time date action "espMachOff"
end if
end tell