Dim con As New OleDb.OleDbConnection("PROVIDER=Microsoft.jet.oledb.4.0;Data Source =\\HomeseerPC\HomeSeer HS3\scripts\sample.mdb") Dim Cmd As OleDb.OleDbCommand Dim Reader As OleDb.OleDbDataReader Const sRefMapFileName = "HS2_2_HS3_ref_map.ini" Public Sub Main(parm As Object) Dim dv As Scheduler.Classes.DeviceClass Dim dv2 As Scheduler.Classes.DeviceClass Dim EN As Scheduler.Classes.clsDeviceEnumeration Dim sAdrArray() As String Dim NodeNr As Integer Dim ParentHS2Ref As Integer Dim i As Integer Try con.Open() EN = hs.GetDeviceEnumerator dv = EN.GetNext Do While Not EN.Finished If dv.Interface(hs) = "Z-Wave" Then ' Only Z-Wave devices sAdrArray = Split(dv.Address(hs), "-") NodeNr = Val(sAdrArray(1)) If NodeNr = 48 Then NodeNr = NodeNr End If Debug.WriteLine(dv.Name(hs) & " - " & dv.Relationship(hs).ToString) Select Case dv.Relationship(hs) Case HomeSeerAPI.Enums.eRelationship.Standalone LookUpHS2(dv, NodeNr, 0, 0) Case HomeSeerAPI.Enums.eRelationship.Parent_Root ParentHS2Ref = LookUpHS2(dv, NodeNr, 0, 0) ' Map Parent For i = 0 To dv.AssociatedDevices_Count(hs) - 1 dv2 = hs.GetDeviceByRef(dv.AssociatedDevices(hs)(i)) LookUpHS2(dv2, NodeNr, i + 1, ParentHS2Ref) ' Map Child Debug.WriteLine("#Child:" & dv2.Name(hs)) Next Case HomeSeerAPI.Enums.eRelationship.Child ' Skip, Handeled under root Case Else hs.WriteLog("HS2->HS3 Error", "Unhandled Relationship: " & dv.Relationship(hs).ToString) End Select End If dv = EN.GetNext Loop con.Close() Catch ex As Exception hs.WriteLog("HS2->HS3 Error", "Exception in script: " & ex.Message) End Try End Sub Function LookUpHS2(DV As Scheduler.Classes.DeviceClass, NodeNr As Integer, ByVal iChildInst As Integer, ParentHS2Ref As Integer) As Integer ' Returns Parent HS2 Dev Dim sql As String = "" Dim DeviceType As String = DV.Device_Type_String(hs) LookUpHS2 = 0 ' Decide how to look for the Device in HS2 Select Case DeviceType Case "Z-Wave Switch Binary Root Device", "Z-Wave Switch Multilevel Root Device" DeviceType = "Z-Wave Root Device" sql = "Select * from Devices where dev_type_string = '" & DeviceType & "' AND zwave_nodeid = " & NodeNr & " AND ZWave_CommandClass_Instance = 0" Case "Z-Wave Switch Multilevel" If iChildInst = 0 Then sql = "Select * from Devices where dev_type_string = '" & DeviceType & "' AND zwave_nodeid = " & NodeNr & " AND ZWave_CommandClass_Instance = 0" Else sql = "Select * from Devices where dev_type_string = '" & DeviceType & "' AND AssociatedDevices = '" & ParentHS2Ref & "' AND ZWave_CommandClass_Instance = " & CStr(iChildInst) End If Case "Z-Wave Power" ' This works for the Figaro RGB Device DeviceType = "Z-Wave Sensor Multilevel Power" iChildInst = -1 sql = "Select * from Devices where dev_type_string = '" & DeviceType & "' AND AssociatedDevices = '" & ParentHS2Ref & "' AND ZWave_CommandClass_Instance = " & CStr(iChildInst) Case "Z-Wave Switch Binary" ' Convert to HS2 inst (This is an error in HS2 for Fibaro Devices) If iChildInst = 1 Then iChildInst = -1 End If sql = "Select * from Devices where dev_type_string = '" & DeviceType & "' AND AssociatedDevices = '" & ParentHS2Ref & "' AND ZWave_CommandClass_Instance = " & CStr(iChildInst) Case "Z-Wave kW Hours" ' Don't know how to find this in HS2 Database Case "Z-Wave Door Lock" ' Don't know how to find this in HS2 Database Case "Z-Wave Watts" ' Don't know how to find this in HS2 Database Case "Z-Wave Alarm" ' Don't know how to find this in HS2 Database Case Else ' Try to find it useing same Devicetype If iChildInst = 0 Then ' Parent or standalone sql = "Select * from Devices where dev_type_string = '" & DeviceType & "' AND zwave_nodeid = " & NodeNr & " AND ZWave_CommandClass_Instance = 0" Else ' child, use parent ref sql = "Select * from Devices where dev_type_string = '" & DeviceType & "' AND AssociatedDevices = '" & ParentHS2Ref & "' AND ZWave_CommandClass_Instance = " & CStr(iChildInst) End If End Select ' try to find the device If sql = "" Then hs.WriteLog("HS2->HS3 Error", "Don't know how to find '" & DV.Name(hs) & "' in HS2") Debug.WriteLine("Don't know how to find '" & DV.Name(hs) & "' in HS2") DV.Location2(hs) = "Not Found in HS2" ' Set floor to "Not Found in HS2" to be able to quickly map them manually Else Cmd = New OleDb.OleDbCommand(sql, con) Reader = Cmd.ExecuteReader() If Reader.Read() Then Update_device_and_map_refs(Reader, DV) If iChildInst = 0 Then LookUpHS2 = Reader.Item("ref").ToString ' return parent Ref to be used for looking up the children End If Else hs.WriteLog("HS2->HS3 Error", "Cant find HS2 Device '" & DV.Name(hs) & "' by : '" & sql & "'") Debug.WriteLine("Cant find HS2 Device '" & DV.Name(hs) & "' by : '" & sql & "'") DV.Location2(hs) = "Not Found in HS2" ' Set floor to "Not Found in HS2" to be able to quickly map them manually End If End If End Function Sub Update_device_and_map_refs(ByRef Reader As OleDb.OleDbDataReader, ByRef DV As Scheduler.Classes.DeviceClass) Dim s As String hs.WriteLog("HS2->HS3", "# Updating Device: " & DV.Name(hs)) ' Name s = Reader.Item("Name").ToString.Trim hs.WriteLog("HS2->HS3", "Setting name to:" & s) DV.Name(hs) = s Debug.WriteLine(DV.Name(hs).ToString) ' Location s = Reader.Item("Location").ToString.Trim hs.WriteLog("HS2->HS3", "Setting Location to:" & s) DV.Location(hs) = s ' Location2 s = Reader.Item("Location2").ToString.Trim hs.WriteLog("HS2->HS3", "Setting Location2 to:" & s) DV.Location2(hs) = s ' HC & DC -> Code s = Trim(Reader.Item("hc").ToString) & Right("00" & Trim(Reader.Item("dc").ToString), 2) hs.WriteLog("HS2->HS3", "Setting Code to:" & s) DV.Code(hs) = s ' Ref mapping s = Reader.Item("ref").ToString hs.SaveINISetting("Device Ref HS2 to HS3", s, DV.Ref(hs), sRefMapFileName) hs.SaveINISetting("Device Ref HS3 to HS2", DV.Ref(hs), s, sRefMapFileName) End Sub