Announcement

Collapse
No announcement yet.

Help with rounding in a script

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

    Help with rounding in a script

    I need help getting my Decimal points in order. But I want them atleast 1 place.

    I'm counting gallons of water flowing and the meter only has 1 decimal place increments.

    So I have my current meter reading, plus a starting point (min, hour, day, week) and I'm subtracting to find my total for that last period.

    &hs.SetDeviceValueByRef(284,hs.DeviceValueEX(293)-hs.DeviceValueEX(279),False)

    Something like this above but for some reason I end up with alot of decimal points even though I should not be. The Math is not right in a script. I start with only XXXX.X and subtract XXXX.X but I somehow end up with X.XXXXXXXXXXX for some reason.

    Anyone have a reason why the math is off and how to fix it.

    Also I know in the virtual device I can adjust it to round, but my problem is in my mobile app Imperihome it shows the whole value and also haven't figured out how to round.

    Thanks,

    #2
    Have you tried the Math.Round() function?


    Dim dblDif as Double

    dblDif = hs.DeviceValueEX(293)-hs.DeviceValueEX(279)

    hs.SetDeviceValueByRef(284, Math.Round(dblDif, 1), False)
    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
      Originally posted by Uncle Michael View Post
      Have you tried the Math.Round() function?


      Dim dblDif as Double

      dblDif = hs.DeviceValueEX(293)-hs.DeviceValueEX(279)

      hs.SetDeviceValueByRef(284, Math.Round(dblDif, 1), False)

      I have not yet. I also have never used multi line scripts and current only entering them as one liners in the events tab. I think there is a scripts folder that I can make a text file and point to it in a event right?

      Comment


        #4
        As you are using Windows, you can use this one liner:

        Code:
        &hs.SetDeviceValueByRef(284,Round(hs.DeviceValueEX(293) - hs.DeviceValueEX(279),1),False)
        Jon

        Comment


          #5
          Originally posted by Uncle Michael View Post
          Have you tried the Math.Round() function?


          Dim dblDif as Double

          dblDif = hs.DeviceValueEX(293)-hs.DeviceValueEX(279)

          hs.SetDeviceValueByRef(284, Math.Round(dblDif, 1), False)

          Thank You!

          Originally posted by jon00 View Post
          As you are using Windows, you can use this one liner:

          Code:
          &hs.SetDeviceValueByRef(284,Round(hs.DeviceValueEX(293) - hs.DeviceValueEX(279),1),False)
          And Thank You!

          This worked out perfect.

          Comment


            #6
            Thanks from year 2022!!!

            Code:
            public void Main(object Parms){
            double mean = Math.Round((hs.DeviceValueEx(705)+hs.DeviceValueEx(701)+hs.DeviceValueEx(709)+hs.DeviceValueEx(713)+hs.DeviceValueEx(717))/5, 1);
            hs4.UpdateFeatureValueByRef(901, mean);
            }
            ‚Äč

            Comment

            Working...
            X