Announcement

Collapse
No announcement yet.

I need to truncate a string

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

  • I need to truncate a string

    I have a device (Jon00Uptime) which stores a elapsed time in the device string. Unfortunately there is a green dot before, which I would like to get rid of...

    I am using this in the script to retrieve the string:

    Code:
    Dim TotalHotWaterTime As String
    
            TotalHotWaterTime = hs.DeviceString(593)
    I am using this to email the value (string) of that device. The result looks like this:

    accumulated 0 days 0 hrs 0 mins 32 secs

    I would like to just have


    0 days 0 hrs 0 mins 32 secs

    How can I do that ?

    Attached Files

  • #2
    Is the dot always the same? If so, I would use the vb.net replace method to replace the text associated with the dot with nothing: https://docs.microsoft.com/en-us/dot...ramework-4.7.2. If the text changes, then I would either find a character at the end of it using the Instr method (https://docs.microsoft.com/en-us/dot...ramework-4.7.2) and then taking all text to the right of it (https://docs.microsoft.com/en-us/dot...ramework-4.7.2) or use a regular expression to find the text that matches what you need.
    HS 3.0.0.548: 1965 Devices 1146 Events
    Z-Wave 3.0.1.262: 122 Nodes on one Z-Net

    Comment


    • #3
      I am not very good with vb... I am not sure how to find the unicode for the 'dot'. But what could work, if I can split the strine, I could try to separate it after character 1, then take the remaining string if thats possible ?

      Comment


      • #4
        Take a look at the html for that page to see what the string is composed of as it will include some html code. Then your code would be something like this:

        Code:
        Dim x as Integer = Instr(TotalHotWaterTime,"text to find")
        TotalHotWaterTime = Right(TotalHotWaterTime,Len(TotalHotWaterTime)-x-1)
        HS 3.0.0.548: 1965 Devices 1146 Events
        Z-Wave 3.0.1.262: 122 Nodes on one Z-Net

        Comment


        • #5
          You can get the uptime from the Jon00UptimeDataHS3.ini file:
          Code:
          Dim TotalHotWaterTime As String
          TotalHotWaterTime = hs.GetIniSetting("Device1","TotalUptime,"","Jon00UptimeDataHS3.ini")
          Change "Device1" to the device number you are using for the Hot Water.
          Jon

          Comment


          • #6
            Originally posted by jon00 View Post
            You can get the uptime from the Jon00UptimeDataHS3.ini file:
            Code:
            Dim TotalHotWaterTime As String
            TotalHotWaterTime = hs.GetIniSetting("Device1","TotalUptime,"","Jon00UptimeDataHS3.ini")
            Change "Device1" to the device number you are using for the Hot Water.
            Ok thats easier. But still have learnt something along the way which is good

            Comment


            • #7
              I have tried it like this

              TotalHotWaterTime = hs.GetIniSetting(593,"TotalUptime","","Jon00UptimeDataHS3.in i")

              and like this

              TotalHotWaterTime = hs.GetIniSetting("593","TotalUptime","","Jon00UptimeDataHS3. ini")

              there are no errors in the log, but it does not show any value. Something is not quite right. #593 is the uptime device ref for Hot water

              Comment


              • #8
                No....

                You defined your Hot Water uptime under a [DeviceX] heading in the main ini file (where X is a number). For example [Device6]

                Therefore to extract the uptime for [Device6] you would use:

                Code:
                 
                 Dim TotalHotWaterTime As String TotalHotWaterTime = hs.GetIniSetting("Device6","TotalUptime,"","Jon00UptimeDataHS3.ini")
                This has nothing to do with device reference numbers.
                Jon

                Comment


                • #9
                  Yes it is working now. Great

                  Comment


                  • #10
                    Originally posted by mikee123 View Post
                    I have a device (Jon00Uptime) which stores a elapsed time in the device string. Unfortunately there is a green dot before, which I would like to get rid of...
                    How can I do that ?
                    Although this particular problem has been resolved, I've encountered this problem with other device strings. To get around it I use a REGEX function to remove the extra characters in the string. My script is below for reference in case it is useful to others. It assumes there is a virtual device that will receive the cleaned up string.

                    Code:
                        Public Sub Main(ByVal Parms As Object)
                    
                        'Creates devices with "clean" device strings for HSTouch
                    
                        Imports System.Text.RegularExpressions
                        Dim imgRegex As New Regex("<[^>]*>", RegexOptions.IgnoreCase)
                    
                        Dim strLine As String
                        Dim dblNumber As Double
                    
                        'Outside Humidity
                    
                            strLine = imgRegex.Replace(hs.DeviceString(62), "")
                            hs.SetDeviceString(150, strLine.Trim(), True)
                            dblNumber = Val(strLine)
                            hs.SetDeviceValueByRef(150, dblNumber, True)    
                            'hs.WriteLog("Clean RH", strLine.Trim())        'debug
                    
                        'Outside Temperature
                    
                            strLine = imgRegex.Replace(hs.DeviceString(59), "")
                            hs.SetDeviceString(146, strLine.Trim(), True)
                            'hs.WriteLog("Clean Temp", strLine)        'debug
                    
                        'Daily Rain
                    
                            strLine = imgRegex.Replace(hs.DeviceString(64), "")
                            hs.SetDeviceString(152, strLine.Trim(), True)
                            dblNumber = Val(strLine)
                            hs.SetDeviceValueByRef(152, dblNumber, True)
                    
                        'Yearly Rain
                    
                            strLine = imgRegex.Replace(hs.DeviceString(67), "")
                            hs.SetDeviceString(149, strLine.Trim(), True)
                    
                        'Living Room Temperature
                    
                            strLine = imgRegex.Replace(hs.DeviceString(1098), "")
                            hs.SetDeviceString(1238, strLine.Trim(), True)
                            'hs.WriteLog("Clean Temp", strLine)        'debug
                    
                        End Sub
                    Mike____________________________________________________________ __________________
                    HS3 Pro Edition 3.0.0.548

                    HW: Stargate | NX8e | CAV6.6 | Squeezebox | PCS | WGL 800RF, Rain8Net+ | RFXCOM | QSE100D | Vantage Pro | Green-Eye | X10: XTB-232, -IIR | Edgeport/8 | Way2Call | Ecobee3

                    Comment


                    • #11
                      Now I have a new 'problem'. I need to convert or retrieve the TotalHotWaterTime which is stored in the Jon00UptimeDataHS3.ini as a number (for a calculation, subtraction). It is stored as a string, so subtraction does not work. But can I either convert it to for example number of minutes, or is the total time stored somewhere as a number ?

                      Comment


                      • #12
                        Originally posted by mikee123 View Post
                        Now I have a new 'problem'. I need to convert or retrieve the TotalHotWaterTime which is stored in the Jon00UptimeDataHS3.ini as a number (for a calculation, subtraction). It is stored as a string, so subtraction does not work. But can I either convert it to for example number of minutes, or is the total time stored somewhere as a number ?
                        Assuming the structure of the input string is always the same, then you could do something like below to get the total number of seconds.

                        Code:
                         Function CalcTotalSeconds(sInput As String) As Long
                                If sInput.Length > 0 Then
                                    Try
                                        Dim arr() As String = sInput.Split(" ")
                                        Dim iDays, iHours, iMinutes, iSeconds As Integer
                        
                                        For i As Integer = 0 To UBound(arr)
                                            Select Case arr(i)
                                                Case "days"
                                                    iDays = arr(i - 1)
                        
                                                Case "hrs"
                                                    iHours = arr(i - 1)
                        
                                                Case "mins"
                                                    iMinutes = arr(i - 1)
                        
                                                Case "secs"
                                                    iSeconds = arr(i - 1)
                                            End Select
                                        Next
                        
                                        Return (iDays * 86400) + (iHours * 3600) + (iMinutes * 60) + iSeconds
                        
                                    Catch ex As Exception
                                        Return -1
                                    End Try
                                Else
                                    Return -1
                                End If
                        
                            End Function
                        Paul..

                        Comment


                        • #13
                          I think I have found (with your help) an easier solution, 4:02am minus the actual stop time, which is I wanted this here. But your solution gives me something else to play with, I am sure I can use that for something else... :-)

                          Comment


                          • #14
                            I'm at work at the moment but there are numerous metrics stored in theJon00UptimeDataHS3.ini

                            Open it up in a text editor and have a peek.
                            Jon

                            Comment


                            • #15
                              Originally posted by jon00 View Post
                              I'm at work at the moment but there are numerous metrics stored in theJon00UptimeDataHS3.ini

                              Open it up in a text editor and have a peek.
                              Yes I am at work too, but will have a look

                              Comment

                              Working...
                              X