Announcement

Collapse
No announcement yet.

finding if last Device String text = some specific text

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

    finding if last Device String text = some specific text

    I have a script that appends a device string as such... mstr = mstr & "<br>"& just_loc1

    I'd like to ensure I dont write duplicate text at the end. For instance... if the text after the last <BR> is "stairs", I dont want it to again write "stairs" to the device string.

    I appreciate the help!


    #2
    Not tested, but a place to start: I'll call your sting 'StringOfStuff'

    Code:
    Dim StringSections As String()
    Dim Sections As Integer
    
    StringSections = StringOfStuff.Split({"<br>"}, StringSplitOptions.None)
    Sections = StringSections.Length
    Then the last string added should be StringSections(Sections) [or maybe Sections-1]
    Mike____________________________________________________________ __________________
    HS3 Pro Edition 3.0.0.548, NUC i3

    HW: Stargate | NX8e | CAV6.6 | Squeezebox | PCS | WGL 800RF | RFXCOM | Vantage Pro | Green-Eye | Edgeport/8 | Way2Call | Ecobee3 | EtherRain | Ubiquiti

    Comment


      #3
      Uncle Michael thx for the help. I'm getting errors...

      Compiling script C:\Users\smarthome\Documents\Homeseer\scripts\CompositeDevic es.vb: Syntax error.
      Compiling script C:\Users\smarthome\Documents\Homeseer\scripts\CompositeDevic es.vb: Value of type '1-dimensional array of String' cannot be converted to 'String'.
      Compiling script C:\Users\smarthome\Documents\Homeseer\scripts\CompositeDevic es.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.



      my Dims are all at the top of the script.
      Here's the code logic I'm using...

      mstr :: existing text string pulled from the HS3 Device Text field.
      just_loc1 :: the new text string to be added

      StringSections = mstr.Split({"<br>"}, StringSplitOptions.None)

      Sections = StringSections.Length

      If StringSections(Sections-1) <> just_loc1
      Then mstr = mstr & just_loc1 & "<br>"
      End If

      Comment


        #4
        What is the Dim statement for the array 'StringSections' ?
        What is the content of the 'mstr' string that you are using for the test?
        Mike____________________________________________________________ __________________
        HS3 Pro Edition 3.0.0.548, NUC i3

        HW: Stargate | NX8e | CAV6.6 | Squeezebox | PCS | WGL 800RF | RFXCOM | Vantage Pro | Green-Eye | Edgeport/8 | Way2Call | Ecobee3 | EtherRain | Ubiquiti

        Comment


          #5
          Have you considered using regular expressions?


          Code:
          Imports System.Text.RegularExpressions
          
          Sub Main(byval parms As Object) ' Match ignoring case of letters.
          
            Dim match As Match
            Dim mstr, parm, sParm(), snippet As String
          
            If parms.GetType().ToString = "System.String" Then 'Called from an Event
              parm = parms.ToString()
            Else ' Called from HSTouch
               parm = parms(0).ToString() & "|" & parms(1).ToString()
            End If 'Now use parm as your input variable...
          
            sParm = Split(parm, "|", -1, 1)
          
          'transfer the string into conventional variables
            mstr = sParm(0)
            snippet = sParm(1)
          
            match = Regex.Match(mstr, snippet, RegexOptions.IgnoreCase)
          
            If match.Success Then ' don't Concatenate
              Exit Sub ' already has location
            End If
          
            mstr = mstr & snippet & "<br>"
          
          End Sub
          I haven't compiled and run the script. I assume you are passing the string to concatenate and the snippet you wish to check for. You will have to write to a global variable or virtual device to save the string.

          Comment


            #6
            Originally posted by Uncle Michael View Post
            What is the Dim statement for the array 'StringSections' ?
            What is the content of the 'mstr' string that you are using for the test?
            I was missing the () on the Dim... still getting 2 errors:
            Code:
            Compiling script C:\Users\smarthome\Documents\Homeseer\scripts\TEST.vb: Syntax error.
            
            Compiling script C:\Users\smarthome\Documents\Homeseer\scripts\TEST.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.
            mstr string is built from the code... mstr = mstr & just_loc1 & "<br>"
            just adds normal text and line breaks so it looks nice in the HS3 GUI and Imperihome UI

            in the GUI it looks ...
            Stairs
            Living Room
            Media Room
            Playroom

            Comment


              #7
              This is a syntax error
              Code:
                  If StringSections(Sections-1) <> just_loc1
                  Then mstr = mstr & just_loc1 & "<br>"
                  End If
              Instead use
              Code:
                  If StringSections(Sections-1) <> just_loc1 Then
                      mstr = mstr & just_loc1 & "<br>"
                  End If
              Or simply
              Code:
                  If StringSections(Sections-1) <> just_loc1 Then mstr &= just_loc1 & "<br>"

              Comment


                #8
                Just noticed you switched the order of the concatenation from post #1 to post #3. That may break it.

                Post #1: mstr = mstr & "<br>"& just_loc1
                Post #3: mstr = mstr & just_loc1 & "<br>"

                Comment


                  #9
                  Originally posted by zwolfpack View Post
                  This is a syntax error
                  Code:
                  If StringSections(Sections-1) <> just_loc1
                  Then mstr = mstr & just_loc1 & "<br>"
                  End If
                  Instead use
                  Code:
                  If StringSections(Sections-1) <> just_loc1 Then
                  mstr = mstr & just_loc1 & "<br>"
                  End If
                  Or simply
                  Code:
                  If StringSections(Sections-1) <> just_loc1 Then mstr &= just_loc1 & "<br>"
                  Sorry, my error. I bet that's the problem.
                  Mike____________________________________________________________ __________________
                  HS3 Pro Edition 3.0.0.548, NUC i3

                  HW: Stargate | NX8e | CAV6.6 | Squeezebox | PCS | WGL 800RF | RFXCOM | Vantage Pro | Green-Eye | Edgeport/8 | Way2Call | Ecobee3 | EtherRain | Ubiquiti

                  Comment


                    #10
                    Originally posted by zwolfpack View Post
                    Just noticed you switched the order of the concatenation from post #1 to post #3. That may break it.

                    Post #1: mstr = mstr & "<br>"& just_loc1
                    Post #3: mstr = mstr & just_loc1 & "<br>"
                    I did that because in the UI if I didnt put a break in the UI has an extra blank line. I dont know why but it was easier to do a line break and not have a blank line. I'm thinking the split should still work and it would be Sections instead of Sections-1 ?

                    Comment


                      #11
                      Uncle Michael


                      If I log StringSections(Sections-1) .. the item is blank / empty text (see log below)
                      If I try StringSections(Sections) or StringSections(Sections-2) I get "Error: Index was outside the bounds of the array."

                      I think I need code like this to ensure I'm not checking an empty array ...

                      Code:
                       If Sections < 1 Then mstr &= just_loc1 & "<br>" 'add new text if the array is empty
                      If Sections = 1 and StringSections(Sections) <> just_loc1 Then mstr &= just_loc1 & "<br>" 'add new text if it does not match the last added
                      If Sections > 1 and StringSections(Sections-1) <> just_loc1 Then mstr &= just_loc1 & "<br>" 'add new text if it does not match the last added
                      The device define has items as shown in this test loop:
                      Code:
                      For Each item As String in StringSections
                      hs.writelog("Composite Devices", "String:" & item)
                      Next
                      In the HS3 UI, this is in the Device text...

                      Media Room
                      Stairs
                      Stairs
                      Stairs
                      Stairs
                      Stairs
                      Stairs
                      Tylers Room
                      Master
                      Office
                      Master Bath << "Master Bath" shows here but not in the Log's top entry, which should be the last added item in the stack?

                      Log...

                      String:Office
                      String:Master
                      String:Tylers Room
                      String:Stairs
                      String:Stairs
                      String:Stairs
                      String:Stairs
                      String:Stairs
                      String:Stairs
                      String:Stairs
                      String:Media Room
                      String: << I dont understand why this is empty but there is always an empty line in this spot when I dump the array to the log?

                      Comment


                        #12
                        Originally posted by Ltek View Post
                        String: << I dont understand why this is empty but there is always an empty line in this spot when I dump the array to the log?
                        I agree. If the string could be empty, then you will need to account for that possibility. Your test seems like a reasonable way to do that.

                        Because you moved the break insertion to be appended after each new entry, the last line is always going to be a blank line, since each new entry gets placed in the blank line defined by the previous break insertion.

                        I am not sure why the print loop is skipping the first entry. Could it be that you are missing element (0) in your 'For Each' loop?

                        Mike____________________________________________________________ __________________
                        HS3 Pro Edition 3.0.0.548, NUC i3

                        HW: Stargate | NX8e | CAV6.6 | Squeezebox | PCS | WGL 800RF | RFXCOM | Vantage Pro | Green-Eye | Edgeport/8 | Way2Call | Ecobee3 | EtherRain | Ubiquiti

                        Comment


                          #13
                          Originally posted by Uncle Michael View Post
                          I agree. If the string could be empty, then you will need to account for that possibility. Your test seems like a reasonable way to do that.

                          Because you moved the break insertion to be appended after each new entry, the last line is always going to be a blank line, since each new entry gets placed in the blank line defined by the previous break insertion.

                          I am not sure why the print loop is skipping the first entry. Could it be that you are missing element (0) in your 'For Each' loop?
                          I changed the code to this ...
                          Code:
                           If Sections < 1 Then mstr &= just_loc1 'add new text if the array is empty, do not add line break
                          If Sections >= 1 and StringSections(Sections-1) <> just_loc1 Then mstr &= "<br>" & just_loc1 'add new text if it does not match the last added
                          its doing what we want, not adding duplicate Devices to the list
                          BUT it still has 1 empty item (blank text) in the array.

                          LOG...
                          Nov-10 8:23:35 AM Composite Devices location: Master Bath
                          Nov-10 8:23:35 AM Composite Devices section item: Living Room
                          Nov-10 8:23:35 AM Composite Devices section item: Master
                          Nov-10 8:23:35 AM Composite Devices section item: Stairs
                          Nov-10 8:23:35 AM Composite Devices section item: Dining Room
                          Nov-10 8:23:35 AM Composite Devices section item:
                          Nov-10 8:23:35 AM Composite Devices # of items: 5

                          HS3 Device text...
                          <empty>
                          Dining Room
                          Stairs
                          Master
                          Living Room
                          Master Bath

                          Comment


                            #14
                            Is the list correct? That is, do you expect 5 items, or should the blank line have a 6th entry in it?
                            Mike____________________________________________________________ __________________
                            HS3 Pro Edition 3.0.0.548, NUC i3

                            HW: Stargate | NX8e | CAV6.6 | Squeezebox | PCS | WGL 800RF | RFXCOM | Vantage Pro | Green-Eye | Edgeport/8 | Way2Call | Ecobee3 | EtherRain | Ubiquiti

                            Comment


                              #15
                              Originally posted by Uncle Michael View Post
                              Is the list correct? That is, do you expect 5 items, or should the blank line have a 6th entry in it?
                              I dont understand it either. The array always seems to think it has 1 item.

                              To get around it I decided to use a Count that was already counting the # of sensors found while inside the detection loop

                              So instead of this...
                              Code:
                               If Sections < 1 Then mstr &= just_loc1 'add new text if the array is empty, do not add line break
                              If Sections >= 1 and StringSections(Sections-1) <> just_loc1 Then mstr &= "<br>" & just_loc1 'add new text if it does not match the last added
                              I use...

                              Code:
                               If motion_count = 1 Then mstr = just_loc1 'add new text if the array is empty, do not add line break
                              If motion_count > 1 and StringSections(Sections-1) <> just_loc1 Then mstr &= "<br>" & just_loc1 'add new text if it does not match the last added

                              Comment

                              Working...
                              X