Announcement

Collapse
No announcement yet.

Counter Script example

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

    Counter Script example

    Does anyone have a sample of a script that accesses and uses counters .. I have tried various ways and all I get is error messages .. I can see that the counter is defined within HS3 but I do not seem to be able to increment or display in a log message....

    Thanks for any help that can be given.

    #2
    Click image for larger version

Name:	HS3 Counter Set.JPG
Views:	157
Size:	66.9 KB
ID:	1369800Click image for larger version

Name:	Counter Off.JPG
Views:	165
Size:	211.1 KB
ID:	1369798
    Attached Files

    Comment


      #3
      I think the op wants to access counters via scripts

      Comment


        #4
        I increment a counter in my scripts for example hs.CounterIncrement "Kitchen Light Off"

        These should work for you

        hs.CounterDecrement(CounterName As string)
        hs.CounterIncrement(CounterName As string)
        hs.CounterReset(CounterName As string)
        hs.CounterValue(CounterName As string)

        Comment


          #5
          The above is correct. But one major deficiency with using counters within scripts is that there is no way to set a counter to a specific value (i.e set it to "10").

          I have set up virtual switches to act as counters for that reason, along with a few other advantages virtual switches give you.

          Comment


            #6
            I use this to scroll through weather animations I download on a daily and hourly basis. This accesses counters to display where I am in the loop (if move up to 20th go to 0 and if going below 0 set to 20). You set the counter to the value you need by incrementing in a do loop. Brute force and stupidity method. I use HTML file to tag to the current image file which reduces the size of moving files around. I also display this in HSTouch in a text box with the Temperature.html file being the file to display. Hope this helps.

            '12/23/2019 ScrollAnimation.vb
            'Script to scroll through static pictures and write html file to unique filename.

            IMPORTS System.IO
            IMPORTS System.Net
            IMPORTS System.Threading

            Sub Main(ByVal Parm As String) 'There are 0 parameters

            Dim sPath, sDisplayFile, Counter, Source, Destination As String
            Dim sFile(19) As String
            Dim dCounter As Double
            Dim iCnt, iMax, iIncCnt As Integer

            iMax = 19 'Set the maximum number of maps
            Counter = "TemperatureCntr" 'Created Counter

            'set the files and pathname for maps
            sPath = "/opt/HomeSeer/html/" 'The Path Name to the files
            sFile(0) = "Chart486.html"
            sFile(1) = "Chart492.html"
            sFile(2) = "Chart497.html"
            sFile(3) = "Chart688.html"
            sFile(4) = "Chart690.html"
            sFile(5) = "Chart691.html"
            sFile(6) = "Chart695.html"
            sFile(7) = "Chart706.html"
            sFile(8) = "Chart3285.html"
            sFile(9) = "Chart3374.html"
            sFile(10)= "Chart3409.html"
            sFile(11)= "Chart3497.html"
            sFile(12)= "Chart3618.html"
            sFile(13)= "Chart3679.html"
            sFile(14)= "Chart3683.html"
            sFile(15)= "Chart3687.html"
            sFile(16)= "Chart3691.html"
            sFile(17)= "Chart3736.html"
            sFile(18)= "Chart3787.html"
            sFile(19)= "Chart3837.html"

            sDisplayFile= "Temperature.html"

            dCounter = hs.CounterValue(Counter) 'Get the Map Counter Value
            iCnt = CInt(dCounter) ' Set the double to an integer

            'Make the list a loop and set the Counter correctly

            If iCnt < 0 Then 'Went through the floor value so set to ceiling value.
            hs.CounterReset(Counter)
            For iIncCnt = 1 To iMax+1
            hs.CounterIncrement(Counter)
            Next
            iCnt = iMax
            End If

            If iCnt > iMax Then 'Went through the ceiling value so set to the floor value
            hs.CounterReset(Counter)
            iCnt = 0
            End If

            Source = sPath & sFile(iCnt)
            Destination = sPath & sDisplayFile

            'Overwrite or create the file that will be displayed.
            System.IO.File.Copy(Source, Destination, True)

            End Sub

            Comment


              #7
              Originally posted by AllHailJ View Post
              You set the counter to the value you need by incrementing in a do loop. Brute force and stupidity method.
              When I tried that, I found that the script was to fast for the increment. Some of the increments were missed. In other words, after doing the increment 10 times, the counter would be at a value less than 10 (like 6, for example). If I put in a short wait statement, it worked (but who wants to wait 10 seconds, 1 second per increment, just to get the counter to increment properly). Which is why when I need to set a counter to a specific value (like for a count down count), I use a virtual switch or a global variable instead. If I do not need to do that, I do use counters.

              (Note that this was on a Windows machine. A Raspberry Pi might behave differently.)

              Comment


                #8
                I am running Ubuntu linux server on supermicro 1u 1/2 depth box with the latest stable mono. I do not see the issue you describe. Odd???

                Comment


                  #9
                  Originally posted by aa6vh View Post

                  When I tried that, I found that the script was to fast for the increment. Some of the increments were missed. In other words, after doing the increment 10 times, the counter would be at a value less than 10 (like 6, for example). If I put in a short wait statement, it worked (but who wants to wait 10 seconds, 1 second per increment, just to get the counter to increment properly). Which is why when I need to set a counter to a specific value (like for a count down count), I use a virtual switch or a global variable instead. If I do not need to do that, I do use counters.

                  (Note that this was on a Windows machine. A Raspberry Pi might behave differently.)
                  You must be doing something wrong. Takes about a second to increment from 0 to 10000

                  Script below:

                  Code:
                  Imports System.Diagnostics
                  
                      Sub Main(ByVal Parms As Object)
                          Dim sw as new stopwatch
                          sw.start()
                          Dim CV As Integer = 10000
                          hs.CounterReset("MyCounter") ' Set to 0
                          While hs.CounterValue("MyCounter") <> CV
                              hs.CounterIncrement("MyCounter")
                          End While
                          hs.writelog("Time", sw.ElapsedMilliseconds)
                      End Sub
                  (Windows system)
                  Jon

                  Comment


                    #10
                    I just ran this code, and the FDCount counter had a value of 28 when finished.

                    Code:
                      For I As Integer = 1 to 100
                        hs.CounterIncrement("FDCount")
                      Next I
                    Edited to add: I wonder if the "While hs.CounterValue("MyCounter") <> CV" allowed enough time for the counter to actually increment?

                    Whatever, since it fails for me, I consider it unreliable.

                    Comment


                      #11
                      Originally posted by jon00 View Post

                      You must be doing something wrong. Takes about a second to increment from 0 to 10000

                      Script below:

                      Code:
                      Imports System.Diagnostics
                      
                      Sub Main(ByVal Parms As Object)
                      Dim sw as new stopwatch
                      sw.start()
                      Dim CV As Integer = 10000
                      hs.CounterReset("MyCounter") ' Set to 0
                      While hs.CounterValue("MyCounter") <> CV
                      hs.CounterIncrement("MyCounter")
                      End While
                      hs.writelog("Time", sw.ElapsedMilliseconds)
                      End Sub
                      (Windows system)
                      It doesn't (from above). Looked at your code again: Realize that you only exit the loop when the counter reaches 10000. So that loop may have actually run 50000 times before reaching 10000 (due to the missed increments).

                      Comment

                      Working...
                      X