Floorplan Scripts4

From XAP Automation
Jump to: navigation, search

This script dims a light in a series of steps. It does this by creating a series of timers. Each timer dims the light by a few more percent. At the end of the sequence the lights state is set to off. The script also checks to see if the light is already off. If it is then it exits without doing anything
Each timer that is created contains a block of code that looks like this:

Sub Main()
     x=xap.sendbsclevel("a.b.c:d","50%")
End Sub

There are 3 settings at the top of the script

  • The xAP name of the device to control
  • The number of steps that are made between the current dim level and 0%
  • The delay in seconds between each step

Sub Main()

'xAP Name of device to dim
DeviceName="ersp.x10.server:hall.lights"

'Number of steps
DimOver=10
'Delay Between Steps in seconds
Delay=5



'----If device is off then exit, as there is no point in dimming
x=xap.getstate(DeviceName)
if x="off" then exit sub

'----Get the current dim level
DimLevel=xap.getlevel(DeviceName)
DimLevelMax=xap.getlevelmax(DeviceName)

'----Convert to %
if DimLevelmax=0 then
	DimPercent=dimLevel
else
	DimPercent=DimLevel/DimLevelMax*100
end if


'The scripts that will be added to make the timers
scriptstartstate="Sub Main()" & vbcrlf &  "x=xap.sendbscstate(""" &  DeviceName & ""","""
scriptstartlevel="Sub Main()" & vbcrlf &  "x=xap.sendbsclevel(""" &  DeviceName & ""","""
scriptend=""")" & vbcrlf & "End Sub"


for a=1 to DimOver
   DimTo=DimPercent-(a*DimPercent/DimOver)
   x=xap.addtimer("Dim to " & DimTo,dateadd("s",Delay*a,now),"",scriptstartLevel & DimTo & "%" & scriptend)	
next

'Send a state off now the dimming is done
x=xap.addtimer("Switch Off",dateadd("s",delay*DimOver,now),"",scriptstartstate & "OFF" & scriptend)

End Sub