Announcement

Collapse
No announcement yet.

Need Help with Scrip to Control Lights

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Need Help with Scrip to Control Lights

    So I had an idea to clean up my events (cut down the number) and make it easier when I want to make global changes to the way the lights work in the house. I built this script to basically get triggered anytime motion device has its value changed. It passes parameters to build arrays. It creates a delayed event that gets deleted after firing if there is no motion... or deletes the delayed event if motion is detected and the event exists, and turns on the light... depending on the time of day (condition of the house modes).

    The issue I am having is that when it works, it works great... but I have noticed that sometimes there is a delay... or it didn't trigger at all. I am sure its because my scripting abilities are being tested here and there is a much more efficient way of doing what I want. Can someone point me in the right direction to clean this up or show me where I am wrong in what I am do?

    PHP Code:
    'Parameters Sent Motion Device RefId,Light Device RefId,Delay In Minutes,Dimmable(0/1),Door1(0 if no door),Door2(0 if no door)
    Public Dim House_Mode_Device as string = 1060 '
    Change this to your house mode device
    Public Dim Debug as Boolean False 'Change For Debug Logs
    '
    House ModeDay/0 Evening/10 Sleep/20 Morning/30 Day Light/40 Day Heavy/50 No Motion/60 Dawn/70 Cleaning?Entertain/80
    'Set below for the light level desired for each house mode
    Public Dim Morning_Dim_Level as string = 15
    Public Dim Evening_Dim_Level as string = 10
    Public Dim Sleep_Dim_Level as string = 6



    '
    ---------------------------------------------------
    '-------Do Not Modify Below-------------------------
    '
    ---------------------------------------------------


    Public 
    Dim logName "Motion Script"
    '---------------------------------------------------
    '
    -------Build Initial Array-------------------------
    '---------------------------------------------------

    Sub Main(ByVal parms As Object)
    Dim parmArray() As String
    parmArray = parms.ToString().Split(",")
    Dim delay_time = parmArray(2)
    Dim Dimmable as string = parmArray(3)
    Dim Door1 as string = parmArray(4)
    Dim Door2 as string = parmArray(5)
    Dim House_Mode as string
    House_Mode = hs.deviceValueEX(House_Mode_Device)
    If Debug then hs.writelog(logName, "Build Array Sub Called")

    '
    Motion Device Name
    Dim Motion_Device 
    parmArray(0)
    Dim Motion_Name As Scheduler.Classes.DeviceClass 
    Motion_Name 
    hs.GetDeviceByRef(Motion_Device)
    Dim Total_Motion_Name Motion_Name.Location(hs) & " " Motion_Name.Name(hs)

    'Light Device Name
    Dim Light_Device = parmArray(1)
    Dim Light_Name As Scheduler.Classes.DeviceClass 
    Light_Name = hs.GetDeviceByRef(Light_Device)
    Dim Total_Light_Name = Light_Name.Location(hs) & " " & Light_Name.Name(hs)

    If Debug then hs.writelog(logName, "Array Received(Initial Array): Motion Device: " & Motion_Device & " Light Device: " & Light_Device & " Delay Time: " & delay_time & " Door 1: " & Door1 & " Door 2: " & Door2)

    '
    Send Array to Sub Light_Decision
    Light_Decision 
    (Light_Device "," Dimmable "," Motion_Device "," House_Mode "," Total_Light_Name "," Door1 "," Door2)
    'Send Array to Sub Event_Decision
    Event_Decision (Motion_Device & "," & Light_Device & "," & Total_Light_Name & "," & House_Mode & "," & delay_time)

    If Debug then hs.writelog(logName, "Script Call Completed")

    End Sub

    '
    -------------------------------------------------------------------------------------
    '------------------------------ EVENT DECISION ---------------------------------------
    '
    -------------------------------------------------------------------------------------

    'This Will Create/Destroy Event depending on Needs
    Sub Event_Decision(ByVal parms as Object)
    Dim parmArray() As String
    parmArray = parms.ToString().Split(",")
    Dim Motion_Device as string = parmArray(0)
    Dim Light_Device as string = parmArray(1)
    Dim Total_Light_Name as string = parmArray(2)
    Dim House_Mode as string = parmArray(3)
    Dim delay_time as string = parmArray(4)

    If Debug then hs.writelog(logName, "Event Decision Sub Called")

    If hs.DeviceValueEX(Motion_Device) = 0 Then
        If Debug then hs.writelog(logName, "Array Received(Event_Decision): Motion Device: " & Motion_Device & " Light Device: " & Light_Device & " Total Light Name: " & Total_Light_Name & " House Mode: " & House_Mode & " Delay: " & delay_time)
        If hs.EventExists(Total_Light_Name & " Delayed") then
            If Debug then hs.writelog(logName, Total_Light_Name & " Delayed event already exists.")
        Else
            If (House_Mode <> 80) orElse (House_Mode = 80 and (Instr(Total_Light_Name,"Laundry") orElse Instr(Total_Light_Name,"Garage"))) then
                If hs.DeviceValueEX(Light_Device) <> 0 then
                    Dim Event_Ref
                    Dim etime = Date.Now().AddMinutes(delay_time)
                    Event_Ref = hs.newEventEx(Total_Light_Name & " Delayed", "Delayed Events", "")
                    hs.EventSetTimeTrigger(Event_Ref,etime)
                    hs.DeleteAfterTrigger_Set(Event_Ref)
                    hs.AddDeviceActionToEvent(Event_Ref,hs.CAPIGetSingleControl(Light_Device, False, "off", False, False))
                    If Debug = 1 then 
                        hs.writelog (logName, Total_Light_Name & " Delayed event created.")
                    End If
                End If
            Else
                If Debug then hs.writelog(logName, "Current House Mode setting won'
    t allow for Event Creation")
            End If
        End If
    Else
        If hs.EventExists(Total_Light_Name & " 
    Delayed") then
            hs.DeleteEvent(Total_Light_Name & " 
    Delayed")
            If Debug then hs.writelog(logName, "
    Motion DetectedDelay Event Deleted")
        Else
            If Debug then hs.writelog(logName, "
    Motion DetectedNo Event to Delete")
        End If
    End If

    If Debug then hs.writelog(logName, "
    Exit Event Decision Sub")

    End Sub

    '-------------------------------------------------------------------------------------
    '------------------------------ LIGHT DECISION ---------------------------------------
    '-------------------------------------------------------------------------------------

    Sub Light_Decision(ByVal parms as Object)
    Dim parmArray() As String
    parmArray = parms.ToString().Split("
    ,")
    Dim Light_Device as string = parmArray(0)
    Dim Dimmable as string = parmArray(1)
    Dim Motion_Device as string = parmArray(2)
    Dim House_Mode as string = parmArray(3)
    Dim Total_Light_Name as string = parmArray(4)
    Dim Door1 as string = parmArray(5)
    Dim Door2 as string = parmArray(6)
    Dim Request_Status as string
    Dim Light_Decision as string

    If Door1 <> 0 then 
        If hs.deviceValueEX(Door1) <> 0 then
            Light_Decision = 1
        End If
    ElseIf Door2 <> 0 then
        If hs.deviceValueEX(Door2) <> 0 then
            Light_Decision = 1
        End If
    ElseIf Motion_Device <> 0 then
        If hs.deviceValueEX(Motion_Device) <> 0 then
            Light_Decision = 1
        End If
    Else
        Light_Decision = 0
    End If

    If Debug then hs.writelog(logName, "
    Light Decision Sub Called")

    If Light_Decision <> 0 then
        If House_Mode <> 0 then
            If House_Mode <> 40 then 
                If hs.DeviceValueEX(Light_Device) <> 0 then
                    If Debug then hs.writelog(logName, Total_Light_Name & " 
    is already on")
                Else
                    If Dimmable = 0
                        hs.CAPIControlHandler(hs.CAPIGetSingleControl(Light_Device,true ,"
    on",false,true))
                        If Debug then hs.writelog(logName, Total_Light_Name & " 
    was turned on")
                    Else
                        If House_Mode = 30 then
                            'Morning Lights
                            Request_Status = Morning_Dim_Level
                        ElseIf House_Mode = 10 then
                            'Evening Lights
                            Request_Status = Evening_Dim_Level
                        ElseIf House_Mode = 20 then
                            'Sleep Lights
                            Request_Status = Sleep_Dim_Level
                        ElseIf House_Mode = 60 then
                            'Night No Motion Lights
                            Request_Status = Sleep_Dim_Level
                        ElseIf House_Mode = 70 then
                            'Dawn Lights
                            Request_Status = Sleep_Dim_Level                        
                        Else
                            'All Others
                            Request_Status = 98
                        End If
                        hs.CAPIControlHandler(hs.CAPIGetSingleControl(Light_Device, false, Request_Status, false, true))
                        If Debug then hs.writelog(logName, Total_Light_Name & " 
    was turned on with Dim Level" & Request_Status)
                    End If
                End If
            Else
                If Debug then hs.writelog(logName, "
    Current House Mode setting (" & House_Mode & "won't allow for Light to be turned on")
            End If
        Else 
            If Debug then hs.writelog(logName, "Current House Mode setting (" & House_Mode & ") won'
    t allow for Light to be turned on")
        End If
    End If

    End Sub 
    Attached Files
Working...
X