Announcement

Collapse
No announcement yet.

I need to truncate a string

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

  • #16
    These are the metrics available:

    [Device1]
    LastOn=02/02/2019 07:36:12
    LastOff=03/02/2019 22:10:09
    Status=0
    LastTotalUptime=1.14:33:57
    CurrentUptime=1.14:33:57
    CurrentDays=1
    CurrentHours=14
    CurrentMinutes=33
    CurrentSeconds=57
    TotalDays=1
    TotalHours=38
    TotalMinutes=2313
    TotalSeconds=138837
    TotalUptime=1 day 14 hrs 33 mins 57 secs
    Counter=1
    LastReset=02/02/2019 07:36:12

    All can be read using the hs.GetIniSetting method I showed you earlier.
    Jon

    Comment


    • #17
      OK that is great. Just got in so did not have a chance to look at the .ini, but that gives me another option of getting the total time for today, just retrieve LastOn and LastOff and subtract.

      Comment


      • #18
        Something is not quite right yet. I am at work so cannot check the Jon00UptimeDataHS3.ini, but LastOn and LastOff should be populated. But my script returns 0, I guess there is something else wrong

        Code:
        Dim LastOn as Date
        Dim LastOff as Date
        
        TotalHotWaterTime = hs.GetIniSetting("Device6","LastOn","","Jon00UptimeDataHS3.ini")
        TotalHotWaterTime = hs.GetIniSetting("Device6","LastOff","","Jon00UptimeDataHS3.ini")
        
        Dim TimeCalc As Long = DateDiff(DateInterval.Minute, LastOn, LastOff)

        Comment


        • #19
          TotalHotWaterTime = hs.GetIniSetting("Device6","LastOn","","Jon00UptimeDataHS3.i ni")
          TotalHotWaterTime = hs.GetIniSetting("Device6","LastOff","","Jon00UptimeDataHS3. ini")

          Should be:

          LastOn = hs.GetIniSetting("Device6","LastOn","","Jon00UptimeDataHS3.i ni")
          Lastoff = hs.GetIniSetting("Device6","LastOff","","Jon00UptimeDataHS3. ini")
          Jon

          Comment


          • #20
            Multitasking - FAIL

            Trying to program while at work... copy pasted and forgot to change the variables, and didnt even see it

            Now its working

            Comment


            • #21
              Just a programming note: You are taking a string ("02/02/2019 07:36:12"), which is not a date field, and which needs to be converted into a date field before you can do the subtraction. (Basically a datefield internally is a number counting a number of time units, usually seconds or microseconds, from some arbitrary but consistant time in the past). Fortunately, your compiler is doing that conversion for you, based on how your code is written. But if you are not aware of this silent conversion, and you change the flow, things might stop working.

              The code could be written like this so that an explicit conversion is done:

              LastOn = DateTime.Parse(hs.GetIniSetting("Device6","LastOn","","Jon00 UptimeDataHS3.ini"))

              You can find documentation on the parse statement here:

              https://docs.microsoft.com/en-us/dot...ramework-4.7.2

              Comment


              • #22
                Yes I thought there was an issue, I tried just subtracting LastOn and LastOff, the resulting error messages pointed me in the direction of a wrong format. You cannot subtract strings I think. But even with Dim LastOn and LastOff as date, I could not subtract them.
                So if I did this:

                Code:
                LastOn = DateTime.Parse(hs.GetIniSetting("Device6","LastOn","","Jon00 UptimeDataHS3.ini"))
                LastOff = DateTime.Parse(hs.GetIniSetting("Device6","LastOn","","Jon00 UptimeDataHS3.ini"))
                I should be able to say

                Code:
                TimeCalc = LastOn - LastOff
                ?

                Comment


                • #23
                  Originally posted by mikee123 View Post
                  Yes I thought there was an issue, I tried just subtracting LastOn and LastOff, the resulting error messages pointed me in the direction of a wrong format. You cannot subtract strings I think. But even with Dim LastOn and LastOff as date, I could not subtract them.
                  So if I did this:

                  Code:
                  LastOn = DateTime.Parse(hs.GetIniSetting("Device6","LastOn","","Jon00 UptimeDataHS3.ini"))
                  LastOff = DateTime.Parse(hs.GetIniSetting("Device6","LastOn","","Jon00 UptimeDataHS3.ini"))
                  I should be able to say

                  Code:
                  TimeCalc = LastOn - LastOff
                  ?
                  You need to use the DateDiff function: https://docs.microsoft.com/en-us/dot...ramework-4.7.2

                  HS 3.0.0.548: 1970 Devices 1148 Events
                  Z-Wave 3.0.1.262: 123 Nodes on one Z-Net

                  Comment


                  • #24
                    Ok think I am slowly getting it. As (no matter how I convert the Laston/LastOff) the way the values are stored, I cannot just subtract. I think on Vera that was possible, thats why I was trying the same here. Could have been another system. Anyway, I get that, that is why I need to use the DateDiff function. And to be sure it will always work, it would be best to add DateTime.Parse when retrieving the variables to make sure they are getting converted to a proper date format.

                    The microsoft docs do not make 'light reading' ... quite hard to digest and usually triggers a barrage of swear words

                    Comment


                    • #25
                      This is one way to do it:

                      Code:
                      Sub Main(Byval Parm as String)
                      Dim LastOn as Date
                      Dim LastOff as Date
                      Dim TS As Timespan
                      Dim TimeCalc As Integer
                      LastOn = DateTime.Parse(hs.GetIniSetting("Device6","LastOn","","Jon00UptimeDataHS3.ini"))
                      LastOff = DateTime.Parse(hs.GetIniSetting("Device6","LastOff","","Jon00UptimeDataHS3.ini"))
                      TS = LastOff - LastOn
                      TimeCalc = TS.TotalMinutes
                      hs.writelog("TimeCalc",TimeCalc.ToString)
                      End Sub
                      Jon

                      Comment


                      • #26
                        Originally posted by mikee123 View Post
                        Ok think I am slowly getting it. As (no matter how I convert the Laston/LastOff) the way the values are stored, I cannot just subtract. I think on Vera that was possible, thats why I was trying the same here. Could have been another system. Anyway, I get that, that is why I need to use the DateDiff function. And to be sure it will always work, it would be best to add DateTime.Parse when retrieving the variables to make sure they are getting converted to a proper date format.

                        The microsoft docs do not make 'light reading' ... quite hard to digest and usually triggers a barrage of swear words
                        I hear you. You can't be a computer programmer unless you can do swear words in Hexadecimal.

                        Vera and the LUA language uses the Linux date field, which is a long integer microsecond count from 1/1/1970. VB.Net uses a floating point number, with the whole number part being a count of days from (I beleive) 1/1/0000, and the fraction part holding the time of day (with .5 meaning noon). This is why simple subtraction worked in LUA, but on in vb.net.

                        Comment

                        Working...
                        X