Announcement

Collapse
No announcement yet.

Persistent variables after script completes

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

    Persistent variables after script completes

    Hello-

    Using examples on this forum, I am testing a simple script that counts the number of devices in a certain state. The script works fine however it appears that a variable (NumberOfOnDevices) is persisting between individual script runs. This does not make any sense to me. Can anyone shed any light on how this variable is being incremented instead of being created new on every run of the script?

    Here is the script:

    Code:
    Dim DeviceList() As Integer = {167, 168, 169, 170, 171, 172}
    Dim CurrentStatus As CAPIStatus
    Dim NumberOfOnDevices As Integer = 0
    
    Sub Main(Parm As Object)
        
        For Each DevRef As Integer In DeviceList
            CurrentStatus = hs.CAPIGetStatus(DevRef)
            If CurrentStatus.Status.ToLower = "violated" Then NumberOfOnDevices += 1
        Next
    
        hs.writelog("OnCount", "I Count " & NumberOfOnDevices & " as open")
    
    End Sub
    Here are the log entries:

    Jun-28 11:27:04 AM Event Running script and waiting: /usr/local/HomeSeer/scripts/CountWindowOpen.vb
    Jun-28 11:27:04 AM Event Event Trigger "WHF Check for Open Windows"
    Jun-28 11:27:04 AM Event Event WHF Check for Open Windows triggered by the event page 'Run' button.
    Jun-28 11:23:53 AM OnCount I Count 20 as open
    Jun-28 11:23:53 AM Event Running script and waiting: /usr/local/HomeSeer/scripts/CountWindowOpen.vb
    Jun-28 11:23:53 AM Event Event Trigger "WHF Check for Open Windows"
    Jun-28 11:23:53 AM Event Event WHF Check for Open Windows triggered by the event page 'Run' button.
    Jun-28 11:22:07 AM OnCount I Count 16 as open
    Jun-28 11:22:07 AM Event Running script and waiting: /usr/local/HomeSeer/scripts/CountWindowOpen.vb
    Jun-28 11:22:07 AM Event Event Trigger "WHF Check for Open Windows"
    Jun-28 11:22:07 AM Event Event WHF Check for Open Windows triggered by the event page 'Run' button.
    Jun-28 11:21:34 AM OnCount I Count 12 as open
    Jun-28 11:21:34 AM Event Running script and waiting: /usr/local/HomeSeer/scripts/CountWindowOpen.vb
    Jun-28 11:21:34 AM Event Event Trigger "WHF Check for Open Windows"
    Jun-28 11:21:34 AM Event Event WHF Check for Open Windows triggered by the event page 'Run' button.
    Jun-28 11:21:13 AM OnCount I Count 8 as open
    Jun-28 11:21:13 AM Event Running script and waiting: /usr/local/HomeSeer/scripts/CountWindowOpen.vb
    Jun-28 11:21:13 AM Event Event Trigger "WHF Check for Open Windows"
    Jun-28 11:21:13 AM Event Event WHF Check for Open Windows triggered by the event page 'Run' button.
    Jun-28 11:20:58 AM OnCount I Count 4 as open




    Thanks,
    Howie

    #2
    Hi Howie,

    Try this instead:

    Code:
    Sub Main(Parm As Object)
        Dim DeviceList() As Integer = {167, 168, 169, 170, 171, 172}
        Dim CurrentStatus As CAPIStatus
        Dim NumberOfOnDevices As Integer = 0
        
        For Each DevRef As Integer In DeviceList
            CurrentStatus = hs.CAPIGetStatus(DevRef)
            If CurrentStatus.Status.ToLower = "violated" Then NumberOfOnDevices += 1
        Next
    
        hs.writelog("OnCount", "I Count " & NumberOfOnDevices & " as open")
    
    End Sub
    HS 4.2.8.0: 2134 Devices 1252 Events
    Z-Wave 3.0.10.0: 133 Nodes on one Z-Net

    Comment


      #3
      Originally posted by sparkman View Post
      Hi Howie,

      Try this instead:

      Code:
      Sub Main(Parm As Object)
          Dim DeviceList() As Integer = {167, 168, 169, 170, 171, 172}
          Dim CurrentStatus As CAPIStatus
          Dim NumberOfOnDevices As Integer = 0
          
          For Each DevRef As Integer In DeviceList
              CurrentStatus = hs.CAPIGetStatus(DevRef)
              If CurrentStatus.Status.ToLower = "violated" Then NumberOfOnDevices += 1
          Next
      
          hs.writelog("OnCount", "I Count " & NumberOfOnDevices & " as open")
      
      End Sub

      Thank You!

      I had not considered the possibility that there would variable scope outside the running script but looking at it now that makes perfect sense. I figured I was doing something dumb with this.

      Comment

      Working...
      X