Announcement

Collapse
No announcement yet.

Script for checking doors and windows.

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

    Script for checking doors and windows.

    Hi

    Came across this script for checking doors and windows.

    Code:
    'these are the virtual devices to be updated by the script
    '1027 Windows
    '1026   Doors
    
    'Use the reference ID of the door or window sensor devices in the arrays below
    
    dim array_windows() as string = {"1018"}
    
    dim array_doors() as string = {"1015"}
    
    dim windows_count as integer = 0
    dim window_name as string = ""
    dim wstr as string = ""
    
    dim doors_count as integer = 0
    dim door_name as String = ""
    dim dstr as string = ""
    
    Dim dv as Object
    
    'Using 2 subs, one for doors and the other for windows
    
    'WINDOWS
    sub windows(ByVal Parms as Object)
    Try
    windows_count = 0
    window_name = ""
    wstr = ""
    for each devw as string in array_windows
            'hs.writelog("Array", "Window " & window_name & " | Value " & hs.DeviceValue(devw))
    if hs.DeviceValue(devw) = 1 then
                    windows_count = windows_count + 1
    dv = hs.GetDeviceByRef(devw)
    window_name =  dv.Name(hs)
           wstr = wstr & window_name & ",<br>"
    hs.writelog("DoorWindow", window_name & " Open")
    'hs.writelog("DoorWindow", array_windows)
            end if
    next
    If windows_count > 0 then
    hs.SetDeviceValueByRef(1027,100,true)
    hs.SetDeviceString(1027, wstr,true)
    Else
    hs.SetDeviceValueByRef(1027,0,true)
    hs.SetDeviceString(1027, "All Windows Closed",true)
    End If
    
    hs.writelog("DoorWindow", "Window Count: " & windows_count)
    
    Catch ex As Exception
    hs.WriteLog ("DoorWindow", "Error Window: " & ex.Message)
    End Try
    
    End Sub
    
    'DOORS
    sub doors(ByVal Parms as Object)
    Try
    doors_count = 0
    door_name = ""
    dstr = ""
    for each devd as string in array_doors
            'hs.writelog("Array", "Door " & door_name & " | Value " & hs.DeviceValue(devd))
    if hs.DeviceValue(devd) = 1 then
    dv = hs.GetDeviceByRef(devd)
    door_name =  dv.Name(hs)
    doors_count = doors_count + 1
    dstr = dstr & door_name & ",<br>"
    hs.writelog("DoorWindow", door_name & " Open")
            end if
    next
    If doors_count > 0 then
    hs.SetDeviceValueByRef(1026,100,true)
    hs.SetDeviceString(1026, dstr,true)
    Else
    hs.SetDeviceValueByRef(1026,0,true)
    hs.SetDeviceString(1026, "All Doors Closed",true)
    End If
    
    hs.writelog("DoorWindow", "Door Count: " & doors_count)
    
    Catch ex As Exception
    hs.WriteLog ("DoorWindow", "Error Door: " & ex.Message)
    End Try
    
    End Sub
    I have created two virtual devices with ref id 1027 (windows) and 1026 (doors).
    But i get this error message: VB.Net script exception(0), re-starting: Object reference not set to an instance of an object

    Running HS3 pro on win10.

    #2
    Looking at the script, did you populate the reference IDs of your door and window sensors in these arrays?

    Code:
     
     dim array_windows() as string = {"1018"}  dim array_doors() as string = {"1015"}
    HS 3.0.0.548: 1990 Devices 1172 Events
    Z-Wave 3.0.1.262: 126 Nodes on one Z-Net

    Comment


      #3
      Yes. I tried to add all my windows and doors. And then just 2 window and 2 door.

      dim array_windows() as string = {"1018", "1024"}

      dim array_doors() as string = {"1015", "995"}

      I get the same result.

      Comment


        #4
        I rewrote it a bit in the way I write scripts and the below works for me. To suppress all of the log messages, change the line that says Dim Debug as Boolean = True to Dim Debug As Boolean = False.

        Code:
        Sub Main(ByVal Parms As String)
        
            'Use the reference ID of the door or window sensor devices in the arrays below
            'This script work for devices that have a value of 0 when they are secure and greater than 0 If they are not
        
            Dim WindowArray() As Integer = {8152,8129,8086,8123,8158}
            Dim DoorArray() As Integer = {1017,1018,1146,6370}
            Dim DoorDev As Integer = 8259
            Dim WindowDev As Integer = 8260
            Dim Debug As Boolean = True
            Dim LogName As String = "Door-Window"
        
            Dim WindowCount As integer = 0
            Dim WindowName As string = ""
            Dim WindowDevString As string = ""
        
            Dim DoorCount As integer = 0
            Dim DoorName As String = ""
            Dim DoorDevString As string = ""
        
            Dim dv As Scheduler.Classes.DeviceClass
        
            Try
                'Windows
                For Each Window As Integer In WindowArray
                    dv = hs.GetDeviceByRef(Window)
                    WindowName =  dv.Name(hs)
                    If Debug Then hs.writelog(LogName, "Window " & WindowName & " | Value " & hs.DeviceValue(Window))
                    If hs.DeviceValue(Window) > 0 Then
                        WindowCount = WindowCount + 1
                        WindowDevString = WindowDevString & WindowName & ",<br>"
                        If Debug Then hs.writelog(LogName, WindowName & " Open")
                    End If
        
                Next
                If WindowCount > 0 Then
                    hs.SetDeviceValueByRef(WindowDev,100,True)
                    hs.SetDeviceString(WindowDev, WindowDevString,True)
                Else
                    hs.SetDeviceValueByRef(WindowDev,0,True)
                    hs.SetDeviceString(WindowDev, "All Windows Closed",True)
                End If
        
                If Debug Then hs.writelog(LogName, "Window Count: " & WindowCount)
        
                'Door
                For Each Door As Integer In DoorArray
                    dv = hs.GetDeviceByRef(Door)
                    DoorName =  dv.Name(hs)
                    If Debug Then hs.writelog(LogName, "Door " & DoorName & " | Value " & hs.DeviceValue(Door))
                    If hs.DeviceValue(Door) > 0 Then
                        DoorCount = DoorCount + 1
                        DoorDevString = DoorDevString & DoorName & ",<br>"
                        If Debug Then hs.writelog(LogName, DoorName & " Open")
                    End If
                Next
                If DoorCount > 0 Then
                    hs.SetDeviceValueByRef(DoorDev,100,True)
                    hs.SetDeviceString(DoorDev, DoorDevString,True)
                Else
                    hs.SetDeviceValueByRef(DoorDev,0,True)
                    hs.SetDeviceString(DoorDev, "All Doors Closed",True)
                End If
        
                If Debug Then hs.writelog(LogName, "Door Count: " & DoorCount)
        
            Catch ex As Exception
                hs.WriteLog (LogName, "Error Door: " & ex.Message)
            End Try
        
        End Sub
        HS 3.0.0.548: 1990 Devices 1172 Events
        Z-Wave 3.0.1.262: 126 Nodes on one Z-Net

        Comment


          #5
          Thanks. That worked perfectly!

          Comment


            #6
            Originally posted by Radhoo View Post
            Thanks. That worked perfectly!
            Great and you’re welcome!
            HS 3.0.0.548: 1990 Devices 1172 Events
            Z-Wave 3.0.1.262: 126 Nodes on one Z-Net

            Comment


              #7
              Note that the original script had the subroutine named 'windows' rather than 'Main'.
              Code:
              sub windows(ByVal Parms as Object)
              When there is no 'Main' routine in the file, you need to remember to specify the entry point ('windows' in this case) in the 'Sub or Function' box of the script command action, else you get the "VB.Net script exception(0), re-starting" error.

              This had me tearing my hair out a couple days ago; I copied a script to a new event but didn't set the entry point!

              Comment


                #8
                Originally posted by zwolfpack View Post
                This had me tearing my hair out a couple days ago; I copied a script to a new event but didn't set the entry point!
                I wish I had a nickel for every time I've done this to myself.
                Real courage is not securing your Wi-Fi network.

                Comment

                Working...
                X