A friend wanted to use his KPL to control the thermostat heat setpoint. He created a simple trigger to raise of lower the set point by 1 degree on each key press (One key was "up" another was "down"). This more-or-less worked unless someone pressed the buttons too quickly in succession, in which case the thermostat failed to make the change. I came up with the following scheme for him, and thought others might benefit from it. The basic idea is to wait until the user is done raising or lowering the set point (I.e. stops pressing a KPL button) and then set the set point.
A) Create 2 variables:
theSetPoint and
tempChangeLock. Set the value of
theSetPoint to the current set point and set
tempChangeLock to
falseB) Create two triggers. One increments
theSetPoint when the
up key is pressed, the other decrements
theSetPoint when the
down key is pressed.
C) Create a third trigger. This trigger looks for any change in
theSetPoint and has the condition that
tempChangeLock must be false. The trigger action is an external script. This condition check is to keep from having multiple instances of the script running at the same time. The script also checks
tempChangeLock just to be safe.
For Indigo 5 and later, here is the script in Python:
- Code: Select all
delayTime = 15
tempOld = int(indigo.variables['theSetPoint'].value)
if indigo.variables['tempChangeLock'].value == 'false':
indigo.variable.updateValue('tempChangeLock', "true")
loopAgain = True
while loopAgain:
indigo.activePlugin.sleep(delayTime)
tempNew = int(indigo.variables['theSetPoint'].value)
if tempNew == tempOld:
loopAgain = False
tempOld = tempNew
indigo.thermostat.setHeatSetpoint(123, value=tempNew)
indigo.variable.updateValue('tempChangeLock', "false")
This could probably be done a bit better, but I am still familiarizing myself with Python scripting for Indigo. In particular, It would be nice to read the Indigo variable as a boolean.
For Indigo 4.x and earlier, here is the same logic in an AppleScript:
- Code: Select all
set the value of delayTime to 15
tell application "IndigoServer"
set tempOld to the value of variable "theSetPoint" as number
set tempChangeLock to the value of variable "tempChangeLock" as boolean
if not tempChangeLock then
set the value of variable "tempChangeLock" to true
set loopAgain to true
repeat while loopAgain
delay delayTime
set tempNew to the value of variable "theSetPoint" as number
if tempNew is equal to tempOld then set loopAgain to false
set tempOld to tempNew
end repeat
end if
set heat setpoint of device "Thermostat" to tempNew
set the value of variable "tempChangeLock" to false
end tell
Both scripts should probably have some logging, especially at the end.
Comments?
EDITED to fix variable name errors noted by Jay