Announcement

Collapse
No announcement yet.

Display Array Results in Device String

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

  • Display Array Results in Device String

    I am very green when it comes to scripting, and asking some guidance here. I have created a script to monitor my doors and windows in a single virtual device. I have it all working as expected, but have decided to go one step further and have the device string display which door/window is actively open. I can get it to show one door open (lowest #'d device), but it won't show multiple doors. I suspect this is because the way I have it setup, it's looking at the first entry from the Array I built. (It's also just showing the device #, but that's because I am referencing the devices by their ref ID. I didn't think of this new requirement to display the door names until after the fact . I think I just have to change the script to look at the names of the devices, vs the ID).

    Can anyone help?

    Code:
    'Define Windows/Doors Array
    
    '604 Master Bath Right Window
    '600 Master Bath Left WIndow
    '502 Master Bedroom Right Window
    '498 Master Bedroom Left Window
    '394 Loft Window
    '55 Dining/Piano Windows
    '53 Laundry/TV Windows
    
    '58 Garage Door from Laundry
    '54 Front Door
    '57 Back Door
    
    dim array_windows() as integer = {604,600,502,498,394,55,53}
    dim array_doors() as integer = {58,54,57}
    dim windows_count as integer = 0
    dim doors_count as integer = 0
    dim locked as integer = 0
    dim door_name as string = ""
    
    sub windows(ByVal Parms as Object)
    Try
    windows_count = 0
    'Counts thru array to check if any windows are opened
    for each devw as integer in array_windows
        if hs.DeviceValueEx(devw) = 255 or hs.DeviceValueEx(devw) = 100 then
            windows_count = windows_count + 1
        Else If hs.DeviceValueEx(devw) = 0 then 'No windows open
        end if
    next
        If windows_count > 0 then 
            'hs.writelog ("Window Count", windows_count & " windows are opened")
            hs.SetDeviceValueByRef(171,100,true)
            hs.SetDeviceString(171, "Windows Opened",true)
        Else
            'hs.writelog ("Window Count", "All windows are closed")
            hs.SetDeviceValueByRef(171,0,true)
            hs.SetDeviceString(171, "All Windows Closed",true)
        End If
    
    Catch ex As Exception
    hs.WriteLog ("Array", "Error: " & ex.Message)
    End Try
    End Sub
    
    sub doors(ByVal Parms as Object)
    Try
    doors_count = 0
    locked = hs.DeviceValueEx(1069)
    for each devd as integer in array_doors
        if hs.DeviceValueEx(devd) = 255 or hs.DeviceValueEx(devd) = 100 then
            doors_count = doors_count + 1
            door_name = devd
        Else If hs.DeviceValueEx(devd) = 0 then 'No doors open
        end if
    next
        If doors_count > 0 then 
            hs.SetDeviceValueByRef(170,100,true)
            hs.SetDeviceString(170, door_name & " Opened",true)
            ElseIf (doors_count = 0 and locked = 255) then
                    hs.SetDeviceValueByRef(170,150,true)
                    hs.SetDeviceString(170, "All Doors Closed and Locked",true)
        Else
            hs.SetDeviceValueByRef(170,0,true)
            hs.SetDeviceString(170, "All Doors Closed",true)
        End If
    Catch ex As Exception
    hs.WriteLog ("Array", "Error: " & ex.Message)
    End Try
    End Sub
    Attached Files

  • #2
    The easiest would be to build your string in your "for each" loop. Just start with a blank string prior to the loop and add to the string in the loop if the device is open.

    Cheers
    Al
    HS 3.0.0.531: 1985 Devices 1128 Events
    Z-Wave 3.0.1.260: 122 Nodes on one Z-Net

    Comment


    • #3
      Ok, I think I have though, unless I am missing something. I added log writing and it shows the two doors currently opened (and also changed the reference to show the names instead of numbers) Its still only showing the one door that is open in the device, but you can see the log is showing both.

      Code:
      sub doors(ByVal Parms as Object)
      Try
      doors_count = 0
      locked = hs.DeviceValueEx(1069)
      door_name = ""
      for each devd as string in array_doors
          if hs.DeviceValueByName(devd) = 255 or hs.DeviceValueByName(devd) = 100 then
              doors_count = doors_count + 1
              door_name = devd
              hs.writelog("Array", door_name)
          Else If hs.DeviceValueByName(devd) = 0 then 'No doors open
          end if
      next
          If doors_count > 0 then 
              hs.SetDeviceValueByRef(170,100,true)
              hs.SetDeviceString(170, door_name & " Opened",true)
              ElseIf (doors_count = 0 and locked = 255) then
                      hs.SetDeviceValueByRef(170,150,true)
                      hs.SetDeviceString(170, "All Doors Closed and Locked",true)
          Else
              hs.SetDeviceValueByRef(170,0,true)
              hs.SetDeviceString(170, "All Doors Closed",true)
          End If
      Catch ex As Exception
      hs.WriteLog ("Array", "Error: " & ex.Message)
      End Try
      End Sub
      Attached Files

      Comment


      • #4
        You are going to do this with a little bit of HTML, as you go through the loop you build the string and add a line break at the end of each bit.

        HTML Code:
        Dim dstr as string = ""
        
        for each devd as string in array_doors
            if hs.DeviceValueByName(devd) = 255 or hs.DeviceValueByName(devd) = 100 then
                doors_count = doors_count + 1
                door_name = devd
                hs.writelog("Array", door_name)
        dstr = dstr & door_name & "<br>"
            Else If hs.DeviceValueByName(devd) = 0 then 'No doors open
            end if
        next
        
         If doors_count > 0 then 
                hs.SetDeviceValueByRef(170,100,true)
                hs.SetDeviceString(170, dstr,true)
                ElseIf (doors_count = 0 and locked = 255) then
                        hs.SetDeviceValueByRef(170,150,true)
                        hs.SetDeviceString(170, "All Doors Closed and Locked",true)
            Else
                hs.SetDeviceValueByRef(170,0,true)
                hs.SetDeviceString(170, "All Doors Closed",true)
            End If
        Might work...
        My Plugins:

        Pushover 3P | DoorBird 3P | Current Cost 3P | Velleman K8055 3P | LAMetric 3P | Garadget 3P | Hive 3P |
        Yeelight 3P | Nanoleaf 3P

        Comment


        • #5
          That is exactly what I wanted... Thank you. Makes sense now that I see it. It's my first time building an Array. I modified slightly just to get the wording I wanted (put opened at the end of each entry). Thanks Again!!

          Code:
          sub doors(ByVal Parms as Object)
          Try
          doors_count = 0
          locked = hs.DeviceValueEx(1069)
          door_name = ""
          dstr = ""
          for each devd as string in array_doors
              if hs.DeviceValueByName(devd) = 255 or hs.DeviceValueByName(devd) = 100 then
                  doors_count = doors_count + 1
                  door_name = devd
                  dstr = dstr & door_name & " Opened" & "<br>"
                  hs.writelog("Array", door_name)
              Else If hs.DeviceValueByName(devd) = 0 then 'No doors open
              end if
          next
              If doors_count > 0 then 
                  hs.SetDeviceValueByRef(170,100,true)
                  hs.SetDeviceString(170, dstr,true)
                  ElseIf (doors_count = 0 and locked = 255) then
                          hs.SetDeviceValueByRef(170,150,true)
                          hs.SetDeviceString(170, "All Doors Closed and Locked",true)
              Else
                  hs.SetDeviceValueByRef(170,0,true)
                  hs.SetDeviceString(170, "All Doors Closed",true)
              End If
          Catch ex As Exception
          hs.WriteLog ("Array", "Error: " & ex.Message)
          End Try
          End Sub
          Attached Files

          Comment


          • #6
            I am trying to adapt your script for myself, but struggle with a few bits. I have created 2 virtual devices to display the door status #438 and the window status #439. Then I edited the script with my device names, taken out all the temperature references. But where do I now put my device numbers in ? I do not have any door locks, so probably have to take out
            locked = hs.DeviceValueEx(1069)
            This is my script so far:

            Code:
            'Define Windows/Doors Array
             '242 Bedroom Right Window
            '252 Bedroom Left Window
             
            '268 Garage Door
            '199 Porch Door
            '262 Patio Door
            '189 Kitchen Back Door
            '335 Bedroom Door
             dim array_windows() as string = {"Bedroom Right Window","Bedroom Left Window"}
            dim array_doors() as string = {"Garage Door","Porch Door","Kitchen Back Door","Patio Door","Bedroom Door"}
            dim windows_count as integer = 0
            dim doors_count as integer = 0
            dim locked as integer = 0
            dim door_name as String = ""
            dim dstr as string = ""
            dim window_name as string = ""
            dim wstr as string = ""
             
            sub windows(ByVal Parms as Object)
            Try
            windows_count = 0
            window_name = ""
            wstr = ""
             for each devw as string in array_windows
                if hs.DeviceValueByName(devw) = 255 or hs.DeviceValueByName(devw) = 100 then
                    windows_count = windows_count + 1
                    window_name = devw
                    wstr = wstr & window_name & " Opened" & "<br>"
                Else If hs.DeviceValueByName(devw) = 0 then 'No windows open
                end if
            next
                If windows_count > 0 then 
                    hs.SetDeviceValueByRef(439,100,true)
                    hs.SetDeviceString(439, wstr,true)
                Else
                    hs.SetDeviceValueByRef(439,0,true)
                    hs.SetDeviceString(439, "All Windows Closed",true)
                End If
                
                hs.writelog("Array", "Window Count: " & windows_count)
             Catch ex As Exception
            hs.WriteLog ("Array", "Error: " & ex.Message)
            End Try
            End Sub
             sub doors(ByVal Parms as Object)
            Try
            doors_count = 0
            'locked = hs.DeviceValueEx(1069)
            door_name = ""
            dstr = ""
            for each devd as string in array_doors
                if hs.DeviceValueByName(devd) = 255 or hs.DeviceValueByName(devd) = 100 then
                    doors_count = doors_count + 1
                    door_name = devd
                    dstr = dstr & door_name & " Opened" & "<br>"
                Else If hs.DeviceValueByName(devd) = 0 then 'No doors open
                end if
            next
                If doors_count > 0 then 
                    hs.SetDeviceValueByRef(170,100,true)
                    hs.SetDeviceString(170, dstr,true)
                    ElseIf (doors_count = 0 and locked = 255) then
                            hs.SetDeviceValueByRef(438,150,true)
                            hs.SetDeviceString(438, "All Doors Closed and Locked",true)
                Else
                    hs.SetDeviceValueByRef(438,0,true)
                    hs.SetDeviceString(438, "All Doors Closed",true)
                End If
            Catch ex As Exception
            hs.WriteLog ("Array", "Error: " & ex.Message)
            End Try
            End Sub
            I have tried running the script manually as above, it runs without any errors in the log, but the virtual switches are empty
            Actually I just tried running it again, and I got this warning in the log:
            Warning VB.Net script exception(0), re-starting: Object reference not set to an instance of an object.
            Attached Files

            Comment


            • #7
              Can you tell me the closed/open values of your actual sensors? Is it 0 when closed and 255 when opened? Or is it 100 when opened?

              And you want the values to go into the devices you have referenced in your pic?

              Comment


              • #8
                Also can you show me the event you are using to run this script? I suspect you might also be missing calling the different subs.

                I have modified a couple things based off what I am seeing you did, i saw a couple errors, but nothing major. I think its just a setup issue.

                Comment


                • #9
                  Try this script, make sure to setup two events, one for the doors and one for windows, change the sub to doors for one and windows for the other.

                  Code:
                  'Define Windows/Doors Array
                   '242 Bedroom Right Window
                  '252 Bedroom Left Window
                   
                  '268 Garage Door
                  '199 Porch Door
                  '262 Patio Door
                  '189 Kitchen Back Door
                  '335 Bedroom Door
                   dim array_windows() as string = {"Bedroom Right Window","Bedroom Left Window"}
                  dim array_doors() as string = {"Garage Door","Porch Door","Kitchen Back Door","Patio Door","Bedroom Door"}
                  dim windows_count as integer = 0
                  dim doors_count as integer = 0
                  dim door_name as String = ""
                  dim dstr as string = ""
                  dim window_name as string = ""
                  dim wstr as string = ""
                   
                  sub windows(ByVal Parms as Object)
                  Try
                  windows_count = 0
                  window_name = ""
                  wstr = ""
                   for each devw as string in array_windows
                      if hs.DeviceValueByName(devw) = 255 or hs.DeviceValueByName(devw) = 100 then
                          windows_count = windows_count + 1
                          window_name = devw
                          wstr = wstr & window_name & " Opened" & "<br>"
                      Else If hs.DeviceValueByName(devw) = 0 then 'No windows open
                      end if
                  next
                      If windows_count > 0 then 
                          hs.SetDeviceValueByRef(439,100,true)
                          hs.SetDeviceString(439, wstr,true)
                      Else
                          hs.SetDeviceValueByRef(439,0,true)
                          hs.SetDeviceString(439, "All Windows Closed",true)
                      End If
                      
                      hs.writelog("Array", "Window Count: " & windows_count)
                   Catch ex As Exception
                  hs.WriteLog ("Array", "Error: " & ex.Message)
                  End Try
                  End Sub
                  
                  sub doors(ByVal Parms as Object)
                  Try
                  doors_count = 0
                  door_name = ""
                  dstr = ""
                  for each devd as string in array_doors
                      if hs.DeviceValueByName(devd) = 255 or hs.DeviceValueByName(devd) = 100 then
                          doors_count = doors_count + 1
                          door_name = devd
                          dstr = dstr & door_name & " Opened" & "<br>"
                      Else If hs.DeviceValueByName(devd) = 0 then 'No doors open
                      end if
                  next
                      If doors_count > 0 then 
                          hs.SetDeviceValueByRef(438,100,true) 'was 170
                          hs.SetDeviceString(438, dstr,true) 'was 170
                      Else
                          hs.SetDeviceValueByRef(438,0,true)
                          hs.SetDeviceString(438, "All Doors Closed",true)
                      End If
                  Catch ex As Exception
                  hs.WriteLog ("Array", "Error: " & ex.Message)
                  End Try
                  End Sub
                  
                  My event for doors looks like this, for the windows, create another event that is similar but change the sub to windows instead of door
                  Attached Files

                  Comment


                  • #10
                    I am going to give that a try. I had run the whole script from the 'run' button to test, did not notice or know that I had to run seperate subs. Only just started with scripting so pretty green...
                    As I was only testing the script firstof all I hadnt created events either. I'll do all that (shouldnt take long) and give it another try.
                    Just noticed a slight hitch which is going to make it more complicated. I have a Sensative strip sensor. That gives 22 as open, and 23 as closed. I have tried to change it to 0 and 255 but that didnt work.
                    All the other door sensores give 0 as closed and 255 as open.
                    The result is supposed to go into the 2 devices 438 and 439

                    Comment


                    • #11
                      You can modify the script to accomadate that if you want.

                      hs.DeviceValueByName(devw) = 255 or hs.DeviceValueByName(devw) = 100

                      just add (either under window or door sub) another or hs.devicevaluebyname(devw) and the device value (devw will be different if this is a door).

                      Also make sure the device name you want to look at is also listed in the array so that its looked at.

                      Comment


                      • #12
                        Have just tried it with this test event, which I triggered by 'run'

                        first error was:

                        Jul-28 19:33:21 Warning Not running script since its already running: C:/Program Files (x86)/HomeSeer HS3/scripts/multistatustest.vb Single instance option enabled in event properties

                        Not sure why this came, so I disabled 'only allow single instance...'

                        Jul-28 19:34:25 Error Compiling script C:\Program Files (x86)\HomeSeer HS3\scripts\multistatustest.vb: Declaration expected.
                        Jul-28 19:34:25 Error Compiling script C:\Program Files (x86)\HomeSeer HS3\scripts\multistatustest.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.


                        For the moment I have taken the bedroom door sensor out of the script, as that is the one with 22 and 23 as open/closed

                        I have also tried running it with 'windows' in sub, same result

                        Code:
                        'Define Windows/Doors Array
                         '242 Bedroom Right Window
                        '252 Bedroom Left Window
                         
                        '268 Garage Door
                        '199 Porch Door
                        '262 Patio Door
                        '189 Kitchen Back Door
                        '335 Bedroom Door
                         dim array_windows() as string = {"Bedroom Right Window","Bedroom Left Window"}
                        dim array_doors() as string = {"Garage Door","Porch Door","Kitchen Back Door","Patio Door"}
                        dim windows_count as integer = 0
                        dim doors_count as integer = 0
                        dim door_name as String = ""
                        dim dstr as string = ""
                        dim window_name as string = ""
                        dim wstr as string = ""
                         
                        sub windows(ByVal Parms as Object)
                        Try
                        windows_count = 0
                        window_name = ""
                        wstr = ""
                         for each devw as string in array_windows
                            if hs.DeviceValueByName(devw) = 255 or hs.DeviceValueByName(devw) = 100 then
                                windows_count = windows_count + 1
                                window_name = devw
                                wstr = wstr & window_name & " Opened" & "<br>"
                            Else If hs.DeviceValueByName(devw) = 0 then 'No windows open
                            end if
                        next
                            If windows_count > 0 then 
                                hs.SetDeviceValueByRef(439,100,true)
                                hs.SetDeviceString(439, wstr,true)
                            Else
                                hs.SetDeviceValueByRef(439,0,true)
                                hs.SetDeviceString(439, "All Windows Closed",true)
                            End If
                            
                            hs.writelog("Array", "Window Count: " & windows_count)
                         Catch ex As Exception
                        hs.WriteLog ("Array", "Error: " & ex.Message)
                        End Try
                        End Sub
                        
                        sub doors(ByVal Parms as Object)
                        Try
                        doors_count = 0
                        door_name = ""
                        dstr = ""
                        for each devd as string in array_doors
                            if hs.DeviceValueByName(devd) = 255 or hs.DeviceValueByName(devd) = 100 then
                                doors_count = doors_count + 1
                                door_name = devd
                                dstr = dstr & door_name & " Opened" & "<br>"
                            Else If hs.DeviceValueByName(devd) = 0 then 'No doors open
                            end if
                        next
                            If doors_count > 0 then 
                                hs.SetDeviceValueByRef(438,100,true) 'was 170
                                hs.SetDeviceString(438, dstr,true) 'was 170
                            Else
                                hs.SetDeviceValueByRef(438,0,true)
                                hs.SetDeviceString(438, "All Doors Closed",true)
                            End If
                        Catch ex As Exception
                        hs.WriteLog ("Array", "Error: " & ex.Message)
                        End Try
                        End Sub
                        
                        My event for doors looks like this, for the windows, create another event that is similar but change the sub to windows instead of door
                        Attached Files

                        Comment


                        • #13
                          Looks like what I was trying to type into my previous post ended up in the script

                          Remove this from the bottom of the script and try again

                          My event for doors looks like this, for the windows, create another event that is similar but change the sub to windows instead of door

                          Comment


                          • #14
                            Originally posted by waynehead99 View Post
                            Looks like what I was trying to type into my previous post ended up in the script

                            Remove this from the bottom of the script and try again

                            My event for doors looks like this, for the windows, create another event that is similar but change the sub to windows instead of door
                            lol

                            did not notice, just did a blind copy and paste...

                            this is the result now, but I still have to untick the single instance.

                            Mega pleased.

                            Next mission, I want to create the same thing for my TRV setpoints, so I can see all current TRV setpoints in 1 device
                            That should be possible, shouldnt it ?
                            Attached Files

                            Comment


                            • #15
                              Yea just create a new script and use this one as a base. Put the names of the devices in like you did for the doors and windows, take out the if statements and counts, and have it output what you want.

                              If that doesn't make sense, I can help out with that. I am also new at scripting, but I can do the basics.

                              Comment

                              Working...
                              X