I was writing code for the HR12a keypad to turn on a lamp on the first press, but also turn on a ceiling fan if the button was held down for three seconds. However, I was having problems with a scriptMacro that handled the delayed action.
It seems "this" was changing meaning within the script macro depending on the location of the program counter in SYS during execution!?! Initially if I used "this" within the script macro SYS would stop execution due to an error and "this" would point to: sys://Home/HR12aZoneA/Button_3On/delayedAction and not the intended sys://Home/HR12aZoneA/Button_3On.
Next, I tried this.parent, but SYS would stop execution due to an error and this would point to sys://Home/HR12aZoneA/ .
What gives? It seems this can have more than one meaing in a script macro that is triggered by a timer? Is this always the case for script macros? I've never run into this issue with onpropertychange scripts. I remember reading something about this in the old forum, but can't seem to find it, so sorry if this has been covered before.
My working code to solve the problem:
Code for OnChangeButtonState:
Code for delayedAction scriptMacro that works:
Non working trials:
Code for delayedAction that didn't work (error: object doesn't support this property or method: 'this.Parent.ButtonState'):
Code for delayedAction that also didn't work (error: object doesn't support this property or method: 'this.ButtonState'):
It seems "this" was changing meaning within the script macro depending on the location of the program counter in SYS during execution!?! Initially if I used "this" within the script macro SYS would stop execution due to an error and "this" would point to: sys://Home/HR12aZoneA/Button_3On/delayedAction and not the intended sys://Home/HR12aZoneA/Button_3On.
Next, I tried this.parent, but SYS would stop execution due to an error and this would point to sys://Home/HR12aZoneA/ .
What gives? It seems this can have more than one meaing in a script macro that is triggered by a timer? Is this always the case for script macros? I've never run into this issue with onpropertychange scripts. I remember reading something about this in the old forum, but can't seem to find it, so sorry if this has been covered before.
My working code to solve the problem:
Code for OnChangeButtonState:
Code:
If this.ButtonState = 1 Then
'set the last button object pressed
setStoredObject this
Home.Theater.FloorLamp.PowerState = True
system.addTimer 3, "this.delayedAction.Trigger = True", 1, this.Name & "_delayedAction_" & this.ObjectID
End If
Code for delayedAction scriptMacro that works:
Code:
If this.Parent.IsOfExplicitType(sys.Schema.Device.Keypads.Button.Path) Then
If this.Parent.ButtonState = 2 Then
Home.Theater.CeilingFan.PowerState = True
End If
End If
Non working trials:
Code for delayedAction that didn't work (error: object doesn't support this property or method: 'this.Parent.ButtonState'):
Code:
If this.Parent.ButtonState = 2 Then
Home.Theater.CeilingFan.PowerState = True
End If
Code for delayedAction that also didn't work (error: object doesn't support this property or method: 'this.ButtonState'):
Code:
If this.ButtonState = 2 Then
Home.Theater.CeilingFan.PowerState = True
End If