Announcement

Collapse
No announcement yet.

Help in changing a script slightly

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

  • Help in changing a script slightly

    Hi,

    I've found this great script from MyHappy back in 2014 that I'd like to use slightly differently to fix devices being orphaned devices when Zwave is restarted.

    Basically all of my child devices are 6 consecutive numbers after the parent, so parent = 88 and the 6 child devices will be 89,90,91,92,93,94. What I'd like is to have the RootDV in the script below as an array of all my parent devices (RootDV) (88, 100, 110, 120 etc) and then the script to cycle through each Child device not using the array as in the script but simply as RootDV +1, +2, + 3, +4, +5, +6 and then continue to add them as per the script to the parent.


    My coding skills are limited so hoping someone can just let me know what can be easily changed to serve this purpose........please...

    Thanks in advance


    Code:
    Dim RootDV As Integer = 88
    Dim ChildDV() As Integer = {89,90,91,92,93,94}
    
    Dim rDV As Scheduler.Classes.DeviceClass
    Dim cDV As Scheduler.Classes.DeviceClass
    
    Sub Main(ByVal Parm As Object)
    
        For i As Byte = 0 To ChildDV.GetUpperBound(0) + 1
            If i = 0 Then
                If hs.deviceexistsref(RootDV) Then
                    rDV = hs.GetDeviceByRef(RootDV)
                    hs.writelog("Root Device", "Root Device Name: " & rDV.Name(hs))
                    hs.writelog("Root Device", "Current Root Relationship: " & rDV.Relationship(hs))
                    If rDV.Relationship(hs) = 0 Then
                        rdv.Relationship(hs) = Enums.eRelationship.Parent_Root
                        hs.writelog("Root Device", "New Root Relationship: " & rDV.Relationship(hs))
                    End If
                Else
                    hs.writelog("Error", "Device Reference " & RootDV & " does not exist")
                End If
            Else
                If hs.deviceexistsref(ChildDV(i - 1)) Then
                    If rdv IsNot Nothing Then rdv.AssociatedDevice_Add(hs, ChildDV(i - 1))
                    cDV = hs.GetDeviceByRef(ChildDV(i - 1))
                    hs.writelog("Child Device", "Child Device Name: " & cDV.Name(hs))
                    hs.writelog("Child Device", "Current Child Relationship: " & cDV.Relationship(hs))
                    If cDV.Relationship(hs) = 0 Then
                        cdv.Relationship(hs) = Enums.eRelationship.Child
                        hs.writelog("Child Device", "New Child Relationship: " & cDV.Relationship(hs))
                    End If
                    cdv.AssociatedDevice_Add(hs, RootDV)
                Else
                    hs.writelog("Error", "Device Reference " & ChildDV(i - 1) & " does not exist")
                End If
            End If
        Next
    
    End Sub

  • #2
    This should do it:

    Code:
    Sub Main(ByVal Parm As String)
    
        Dim Debug As Boolean = True                    
        Dim logName As String = "Device Grouping"
    
        Dim RootDevices() As Integer = {6849,6859}
        Dim NumChildren As Integer = 6
        Dim i As Integer = 0
        Dim j As Integer = 0
    
        Try
            Dim dv As Scheduler.Classes.DeviceClass
            hs.writelog(logName, "Fixing Devices")
            For i = 0 To uBound(RootDevices)
                dv = hs.GetDeviceByRef(RootDevices(i))
                For j = RootDevices(i) + 1 To RootDevices(i) + NumChildren
                    If Debug Then hs.writelog(logName,CStr(RootDevices(i)) & " " & CStr(j))
                    dv.AssociatedDevice_Add(Nothing,j)
                Next
            Next
            hs.writelog(logName, "Finished Fixing Devices")
        Catch ex As Exception : hs.writelog(logName, "Exception: " & ex.message)
        End Try
    
    End Sub
    HS 3.0.0.548: 1976 Devices 1156 Events
    Z-Wave 3.0.1.262: 123 Nodes on one Z-Net

    Comment


    • #3
      AWESOME x 100 !! - Thank you SO much, that's is so AWESOME. It works, first time and has fixed 23 of my TRVs that are losing their parent/root device everytime Z-wave (or Homeseer) is restarted.

      Just going to pop it into a script to auto run when z-wave is restarted now......thank you, thank you!!!

      Comment


      • #4
        You're welcome and glad it worked for you. If you don't want the log entries every time it runs, just change

        Code:
         
         Dim Debug As Boolean = True
        to
        Code:
           
         Dim Debug As Boolean = False
        Cheers
        Al
        HS 3.0.0.548: 1976 Devices 1156 Events
        Z-Wave 3.0.1.262: 123 Nodes on one Z-Net

        Comment


        • #5
          Got it, thanks. I've also modded your code so the root device is set back to be being a parent too if necessary.....and it works :-) Thanks again SO much - I am very grateful.

          Code:
          Sub Main(ByVal Parm As String)
          
              Dim Debug As Boolean = True                    
              Dim logName As String = "Device Grouping"
          
              Dim RootDevices() As Integer = {19,26,33,68,76,88,95,136,152,160,191,267,341,348,355,487,516}
              Dim NumChildren As Integer = 6
              Dim i As Integer = 0
              Dim j As Integer = 0
              Dim rDV As Scheduler.Classes.DeviceClass
          
              Try
                  Dim dv As Scheduler.Classes.DeviceClass
                  hs.writelog(logName, "Fixing Devices")
          
                  For i = 0 To uBound(RootDevices)
                      dv = hs.GetDeviceByRef(RootDevices(i))
          
            If hs.deviceexistsref(RootDevices(i)) Then
                          rDV = hs.GetDeviceByRef(RootDevices(i))
                          hs.writelog("Root Device", "Root Device Name: " & rDV.Name(hs))
                          hs.writelog("Root Device", "Current Root Relationship: " & rDV.Relationship(hs))
                          If rDV.Relationship(hs) <> 2 Then
                              rdv.Relationship(hs) = Enums.eRelationship.Parent_Root
                              hs.writelog("Root Device", "New Root Relationship: " & rDV.Relationship(hs))
                          End If
          End If
                      For j = RootDevices(i) + 1 To RootDevices(i) + NumChildren
                          If Debug Then hs.writelog(logName,CStr(RootDevices(i)) & " " & CStr(j))
                          dv.AssociatedDevice_Add(Nothing,j)
                      Next
                  Next
                  hs.writelog(logName, "Finished Fixing Devices")
              Catch ex As Exception : hs.writelog(logName, "Exception: " & ex.message)
              End Try
          
          End Sub

          Comment


          • #6
            Great addition to the script! You'll be a coder soon

            Cheers
            Al
            HS 3.0.0.548: 1976 Devices 1156 Events
            Z-Wave 3.0.1.262: 123 Nodes on one Z-Net

            Comment

            Working...
            X