Coding help!

doczaius

Member
I started out with the Occupancy and Automatic Lights module from the help file and have slowly been adding/modifying to make the automatic on/off/dim a bit less harsh and more of a seamless experience. 
 
Below is code for an Automatic Off function that works as a house keeping type task, turning off lights in rooms that have been unoccupied for a specified amount of time. 
Rather than turn the lights completely off immediately, I want to dim them to a low level (0.15) and then have them turn off one minute later.  The code below throws a "unsupported method" error when the timer expires.  Any thoughts? I know I'm probably missing something obvious with the expression. Eventually I want to have it dial down over a specific period using multiple timers.
Code:
ElseIf this.AutomaticOff = True Then
		debugout "OnChangeOccupancy(): Automatic Off"
		If sysevent.newVal = False Then
	          For Each obj In this.GetObjectsByType(Schema.Device.Lighting.Lighting.Path, False)
				If SupportsMember(obj, "Brightness") Then
					debugout "OnchangeOccupancy(): Set Brightness=0"
					obj.Brightness = 0.15 
					addTimer 60, "obj.Brightness = 0", 1, this.ObjectID
				Else
					debugout "OnchangeOccupancy(): Set Powerstate=0"
					obj.PowerState = 0
				End If
			  Next
		End If
	End If
 

etc6849

Senior Member
It does this because the timer is not passed the meaning of "obj."  However, the meaning of this would be retained when the timer's code is ran.  "This" is an exception :)
 
It is best to write a global function that will return an object's dotted path, then call it like this:

addTimer 60, gDottedPath(obj) & ".Brightness = 0", 1, this.ObjectID

If you search for scriptools on here, one of the versions has functions that show how to make the gDottedPath function above.
 

doczaius

Member
Ty etc. Chuck, it was throwing the error when the timer expired and attempted to execute the expression.
 
Here is the working code:
 
Code:
	ElseIf this.AutomaticOff = True Then
		debugout "OnChangeOccupancy(): Automatic Off"
		If sysevent.newVal = False Then
	          For Each obj In this.GetObjectsByType(Schema.Device.Lighting.Lighting.Path, False)
				If SupportsMember(obj, "Brightness") Then
					debugout "OnchangeOccupancy(): Set Brightness=0"
					obj.Brightness = .15
					objExpression = DotThePath(obj.Path) & ".Brightness = 0"					
					addTimer 60, objExpression, 1, obj.Parent.Name & "_" &obj.Name & "_soft_off"
				Else
					debugout "OnchangeOccupancy(): Set Powerstate=0"
					obj.PowerState = 0
				End If
			  Next
		End If
	End If
 

doczaius

Member
Ok new question --
 
I've been messing with the idea of polling devices in a room when the occupancy state changes...
 
I started with creating a PollOccupied method in Modules.Leviton.Classes.Polling with code harvested from PollDevices, modified to look for devices within the room (passed as an argument)... I think.
 
The second part would be to call the method in OnChangeOccupancy, and that's where I'm stuck. 
In OnChangeOccupancy I tried providing a complete dotted path to PollOccupied but nothing happens when the code runs, no error, no execution.  I added a breakpoint to the call, to try and follow the code, but the pointer just goes to the next line (doesn't drill into the method)... I also don't see any of my debug comments in PollOccupied come through.
 
Is it even possible to call my polling script from within the Occupancy class?
 
Top