Announcement

Collapse
No announcement yet.

Want to cycle through all devices

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

    Want to cycle through all devices

    Wondering if anyone has a thought on this.

    I have a script working that checks to see if a device is on, and if so will go ahead and turn it off.

    I want to now cycle through all the devices and if the location="lower" then run my section of script. Anyone know how to cycle through all the devices, and if the location="lower" return the ref number?



    basically, i am duplicating a script i used in hs2 where at different times i would run through and turn off all the lights for a specific location in the house.
    HS Install Date: Feb. 16, 2007

    HS3 Pro, Z-Wave, Insteon, BLStat, HS Touch Server, MyQ

    WeatherXML, BLBackup, BLLAN, BLLock, Restart, CaddX, OpenSprinkler

    #2
    The device enumerator has not really changed as far as I can see from HS2. This is what I do in a plugin which should also be possible in a script

    Code:
    Dim dv As Scheduler.Classes.DeviceClass
    Dim en As Object = hs.GetDeviceEnumerator
    
    Do While Not en.finished
    
    dv = en.getnext
                If dv.Interface(hs) = IFACE_NAME Then
                    Log("Found IFACE_NAME, exit", LogLevel.Debug)
                    Exit Do
                Else
                    cDevices = True
                End If
    Loop
    In the loop you could instead test the dv.location field was matched to your variable or not.

    Comment


      #3
      yep, i was using the getdeviceenumerator in hs2, and that still works...was just thinking it would be all different like figuring the capi stuff.
      HS Install Date: Feb. 16, 2007

      HS3 Pro, Z-Wave, Insteon, BLStat, HS Touch Server, MyQ

      WeatherXML, BLBackup, BLLAN, BLLock, Restart, CaddX, OpenSprinkler

      Comment


        #4
        CAPI is for control so yes it will be different to actually turn the devices off and on but you can still look through the devices the same way as in HS2.

        Comment


          #5
          @misraels & @mrhappy

          Misraels... Did you finish the script? I'm trying to do something needs a similar loop - but I cant get it working.

          + mrhappy

          My goal is to extend your script that makes a vDevice a 'Composite Device' for specific device types.

          I'm trying to have it loop through all devices and on-the-fly create an array based on Device Name. Then your script will use that array, instead of the array which is hard coded in the current script.

          I cant seem to get it to create the array without throwing an error on ReDim?

          Code:
          Sub Motion(ByVal Parms as Object)
          	Dim arrMotion() As String
          	
          	'Find ALL Motion Devices
              Dim objEnum As Scheduler.Classes.clsDeviceEnumeration
              Dim objDevice As Scheduler.Classes.DeviceClass
              Dim strDevName As String
              Dim strDevString As String
              Dim strListItem As String
              Dim intDevRef As Integer
              Dim dblDevValue As Double
          	
          	Dim blDimensioned As Boolean    'Is the array dimensioned?
          	Dim newDevice As String         'To temporarily hold new Devices to be added to array
          	Dim lngPosition as Long         'Counting for Array
          
          
              objEnum = hs.GetDeviceEnumerator   ' Get a list of all HS devices
              Do While Not objEnum.Finished  ' Retrieve data for each HS device
          		objDevice = objEnum.GetNext
                  If objDevice Is Nothing Then Continue Do
          			intDevRef = ObjDevice.Ref(hs)
                      dblDevValue = hs.DeviceValueEx(intDevRef)
                      strDevString = hs.DeviceString(intDevRef)
          		If objDevice.Name(hs) = "Motion" Then
          			'hs.writelog("Device Collection", "Adding: " & objDevice.Location(hs) & "," & objDevice.Location2(hs) & "," & objDevice.Name(hs))
          			newDevice = objDevice.Location2(hs) & " " & objDevice.Name(hs)
          			If newDevice <> "" Then
          				If blDimensioned = True Then
          					ReDim Preserve arrMotion(0 To UBound(arrMotion) + 1) As String
                          Else
          		            ReDim arrMotion(0 To 0) As String
          					blDimensioned = True  
          			End If
                  arrMotion(UBound(arrMotion)) = newDevice  'Add New Device to the last element in the array.        
          		End If	
          		End If
              Loop
              objDevice = Nothing
              objEnum = Nothing
          	
          	
          '
          dim motion_count as integer = 0
          dim motion_name as string = ""
          dim mstr as string = ""
          
          Try
          
           for each devw as string in arrMotion
              if hs.DeviceValueByName(devw) = 255 or hs.DeviceValueByName(devw) = 100 or hs.DeviceValueByName(devw) = 1 then
                  motion_count = motion_count + 1
                  motion_name = devw
                  mstr = mstr & motion_name & " Motion" & "<br>"
              Else If hs.DeviceValueByName(devw) = 0 then ' No Motion Detected
              end if
          next
              If motion_count > 0 then 
                  hs.SetDeviceValueByRef(187,100,true)
                  hs.SetDeviceString(187, wstr,true)
              Else
                  hs.SetDeviceValueByRef(187,0,true)
                  hs.SetDeviceString(187, "No Motion",true)
              End If
              
              hs.writelog("Array", "Motion Count: " & motion_count)
           Catch ex As Exception
          hs.WriteLog ("Array", "Error: " & ex.Message)
          End Try
          End Sub
          getting errors...

          Compiling script C:\Program Files (x86)\HomeSeer HS3\scripts\CompositeDevices_D-W.vb: 'ReDim' statements can no longer be used to declare array variables.
          Compiling script C:\Program Files (x86)\HomeSeer HS3\scripts\CompositeDevices_D-W.vb: 'ReDim' statements can no longer be used to declare array variables.
          Compiling script C:\Program Files (x86)\HomeSeer HS3\scripts\CompositeDevices_D-W.vb: Argument not specified for parameter 'Parms' of 'Public Sub Motion(Parms As Object)'.
          Compiling script C:\Program Files (x86)\HomeSeer HS3\scripts\CompositeDevices_D-W.vb: Namespace or type specified in the Imports 'System.Core' doesn't contain any public member or cannot be found. Make sure the namespace or the type is defined and contains at least one public member. Make sure the imported element name doesn't use any aliases.
          Last edited by Ltek; July 8, 2017, 05:58 PM.

          Comment


            #6
            I have a script that you can modify. Mine checks each light if it's on and dimmable it dims them. Keep in mind, this doesn't poll devices, it uses the status contained within homeseer.

            Code:
                Public Sub Main(ByVal Parms As Object)
                    ''This script is intended to be called either manually, or by an event at a specific time
                    ''when called get a list of HS devices; and if it's a dimmable light which is on reduce to the night time dimmed level
            
                    ''in the future I may elect to expand this to act on the specified floor; mainly so I can dim interior lights at one time, and exterior lights at another; for now dim them all together
                    ''In my device setup 'first' floor = inside, 'outside' floor is outside
                    ''dv.location2 = floor
            
                    ''if we are going to specify the floor/location; pass it as a single parm and uncomment the next line
                    ''Dim FloorName As String = CType(Parms, String)
            
                    '' set the dimmed level we wish to use (it's stored in a virtual device)
                    '' if dimmed has been set to 99 it's an override.  exit and no changes should be made to lighting levels
                    Dim dimmed As Integer = hs.DeviceValue(95)
                    If dimmed = 99 Then Exit Sub
            
                    Try
                        Dim dv As Scheduler.Classes.DeviceClass
                        Dim EN As Scheduler.Classes.clsDeviceEnumeration
                        EN = hs.GetDeviceEnumerator
                        If EN Is Nothing Then
                            hs.WriteLog("dim_active_lights", "Error getting Enumerator")
                            Exit Sub
                        End If
            
                        Do
                            dv = EN.GetNext
                            If dv Is Nothing Then Continue Do
            
                            '' if the name contains 'light', it's dimable, On, and current value > dimmed; then set to dimmed
                            '' if we decide to specify the floor then add in "And dv.Location2(Nothing).Contains(FloorName)"
                            If dv.Name(Nothing).ToLower.Contains("light") And dv.Can_Dim(hs) = True And hs.IsON(dv.Ref(Nothing)) And hs.DeviceValue(dv.Ref(Nothing)) > dimmed Then
            
                                Dim cc As HomeSeerAPI.CAPIControl = hs.CAPIGetSingleControl(dv.Ref(Nothing), True, "Dim (value)%", False, False)
                                cc.ControlValue = dimmed
                                Dim cr As HomeSeerAPI.CAPIControlResponse = hs.CAPIControlHandler(cc)
            
                            End If
                        Loop Until EN.Finished
            
                    Catch ex As Exception
                        hs.WriteLog("Error", "Exception in script dim_active_lights:  " & ex.Message)
                    End Try
            
                    hs.SaveEventsDevices()
                End Sub
            
                Public Sub SetEventTime(ByVal Parms As Object)
                    '' this script is intended to be called by an event which triggers when either nightmode or daymode virtual device is changed
                    '' nightmode holds the time the lighting should be dimmed
                    '' daymode is when lighting should come on normally
                    '' the script should be passed either 'nightmode' or 'daymode' as it's parm
                    '' if nightmode was changed then set the "dim lights and set nightmode" event to run the above script when nightmode begins
                    '' if daymode was changed then set the "set daymode" event time
            
                    '' nightmode virtual device ref id is 93, daymode virtual device ref id is 94
                    Dim lightingmode As String = CType(Parms, String)
                    Dim refid As Integer
                    Dim eventname As String
            
                    If lightingmode = "daymode" Then
                        refid = 94
                        eventname = "set daymode"
                    Else
                        refid = 93
                        eventname = "dim lights and set nightmode"
                    End If
            
            
                    '' nightmode/daymode, is in 24 hour time format; ie 1 am would be 0100; however homeseer converts that to 100
                    '' lets get the value and make sure the it has 4 places, if not prefix with 0's
                    Dim virtualdevicevalue As String = hs.DeviceValue(refid).ToString.PadLeft(4, "0")
            
                    '' we can now split the string into hours and minutes and insert into our time variable
                    Dim LightingModeDate = New DateTime(1, 1, 1, Left(virtualdevicevalue, 2), Right(virtualdevicevalue, 2), 0)
            
                    Dim EventRefVal As Integer = hs.GetEventRefByName(eventname)
                    Dim res As Boolean = hs.EventSetTimeTrigger(EventRefVal, LightingModeDate)
            
                    hs.SaveEventsDevices()
                End Sub
            HS4 Pro on Shuttle NC10U, Win10; Z-NET
            Number of Devices: 1005
            Number of Events: 293

            Plug-Ins: BLLock, DirecTv, EasyTrigger, Honeywell WiFi Thermostat, Marquis monoprice Amp, MeiHarmonyHub, PHLocation2, Pushover 3P, UltraM1G3, rnbWeather, Worx Landroid, Z-Wave

            External applications: Homebridge-homeseer, Geofency, EgiGeoZone.

            Comment


              #7
              For now I gave up on creating the Array - I'm just going to loop though using nested IF-Then.

              BUT, I'm getting an error telling me there is an extra "End If"... I count the correct number - its only 4... what am I missing?

              thx for the help!

              log...
              Compiling script C:\Program Files (x86)\HomeSeer HS3\scripts\CompositeDevices.vb: 'End If' must be preceded by a matching 'If'.
              Compiling script C:\Program Files (x86)\HomeSeer HS3\scripts\CompositeDevices.vb: Namespace or type specified in the Imports 'System.Core' doesn't contain any public member or cannot be found. Make sure the namespace or the type is defined and contains at least one public member. Make sure the imported element name doesn't use any aliases.
              script...
              Code:
              ' MOTION
              'Status vDevices....
              'All Motion Sensors : 187
              ' Motion multi: 7/8 = Motion, 0 = No Motion
              ' Motion Ademco Panel:  0 = Closed, 1 = Open, -1000 Bypass
              ' Motion Zwave: 1 = Motion, 0 = No Motion
              
              Sub Main(ByVal Parms as Object)
                  Dim objEnum As Scheduler.Classes.clsDeviceEnumeration  'Find ALL Motion Devices
                  Dim objDevice As Scheduler.Classes.DeviceClass
                  Dim strDevName As String
                  Dim strDevString As String
                  Dim strListItem As String
                  Dim intDevRef As Integer
                  Dim dblDevValue As Double
                  dim motion_count as integer = 0
                  dim motion_name as string = ""
                  dim mstr as string = ""
              Try
                  objEnum = hs.GetDeviceEnumerator   ' Get a list of all HS devices
                  Do While Not objEnum.Finished  ' Retrieve data for each HS device
                      objDevice = objEnum.GetNext
                      If objDevice Is Nothing Then Continue Do
                          intDevRef = ObjDevice.Ref(hs)
                          dblDevValue = hs.DeviceValueEx(intDevRef)
                          strDevString = hs.DeviceString(intDevRef)
                          If objDevice.Name(hs) = "Motion" Then
                              hs.writelog("Composite Devices", "Found Motion Device: " & objDevice.Location(hs) & "," & objDevice.Location2(hs) & "," & objDevice.Name(hs))
                              motion_name = objDevice.Location2(hs) & " " & objDevice.Name(hs)
                              If hs.DeviceValueByName(motion_name) = 7 or hs.DeviceValueByName(motion_name) = 8 or hs.DeviceValueByName(motion_name) = 1 then
                                  motion_count = motion_count + 1
                                  mstr = mstr & motion_name & " Motion" & "<br>"
                                  'ElseIf hs.DeviceValueByName(motion_name) = 0 Then hs.writelog("Composite Devices", "Motion Not Detected on " & motion_name)  'No Motion Detected
                                      If motion_count > 0 Then 
                                          hs.SetDeviceValueByRef(187,100,true)
                                          hs.SetDeviceString(187, mstr,true)
                                          Else
                                          hs.SetDeviceValueByRef(187,0,true)
                                          hs.SetDeviceString(187, "No Motion",true)
                                      End If
                              End if
                          End If    
                      End If
                  Loop
                  hs.writelog("Composite Devices", "Motion Detected on " & motion_count & "sensors")
                  objDevice = Nothing
                  objEnum = Nothing
              Catch ex As Exception
              hs.WriteLog ("Composite Devices", "Error: " & ex.Message)
              End Try
              End Sub

              Comment


                #8
                The line:

                Code:
                If objDevice Is Nothing Then Continue Do
                does not need an End If, so delete one above the Loop statement.
                Jon

                Comment


                  #9
                  Originally posted by jon00 View Post
                  The line:

                  Code:
                  If objDevice Is Nothing Then Continue Do
                  does not need an End If, so delete one above the Loop statement.
                  Much appreciated! Works now.

                  Comment

                  Working...
                  X