Announcement

Collapse
No announcement yet.

Convert HS3 DeviceString to DeviceValue

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

    Convert HS3 DeviceString to DeviceValue

    Hi I thought this would be easy and I can usually work my way through it by reading all the posts here, but this has me stumped for the past couple of days

    I am scraping websites for current exchange rates of crypto currencies. I am successful at extracting just the current amounts from the sites and they are assigned as devicestrings using the webscraper app I have had when it first come out.

    How do I convert this devicestring to a devicevalue in a script so I can perform some simple math functions and update virtual devices with my results?

    I do have what appears to be a true number as the device string doesn't appear to have any html tags embedded in it.

    Secondly, is it possible to convert a fraction of an integer to a device value to use in my equations? Some of the currencies are only .05 and .10 cents each.

    Thanks for your help

    Will

    #2
    this might help

    https://forums.homeseer.com/showthread.php?p=750338

    Stuart

    Comment


      #3
      Originally posted by will40 View Post
      Hi I thought this would be easy and I can usually work my way through it by reading all the posts here, but this has me stumped for the past couple of days

      I am scraping websites for current exchange rates of crypto currencies. I am successful at extracting just the current amounts from the sites and they are assigned as devicestrings using the webscraper app I have had when it first come out.

      How do I convert this devicestring to a devicevalue in a script so I can perform some simple math functions and update virtual devices with my results?

      I do have what appears to be a true number as the device string doesn't appear to have any html tags embedded in it.

      Secondly, is it possible to convert a fraction of an integer to a device value to use in my equations? Some of the currencies are only .05 and .10 cents each.

      Thanks for your help

      Will
      You have a couple of options, I would suggest trying either Convert.ToDecimal or Decimal.TryParse - the latter is a bit more forgiving in that it won't throw an exception if it can't convert it to a decimal however the former is just a slight bit more straightforward.

      So you might do something like this for example

      Code:
      Dim CurrentValue As Decimal
      
      CurrentValue = Convert.ToDecimal(hs.devicestring(1234))
      
      CurrentValue = CurrentValue + 0.5
      
      hs.writelog("", CurrentValue)
      
      hs.setdevicevaluebyref(1234, CurrentValue, True)
      If it throws an error then there may be hidden text/special characters in it that need to be removed. HS device values are doubles so you don't have to worry about whole numbers.

      Comment


        #4
        Very nice mrhappy. Your convert to Decimal works perfectly.

        Thank you!!

        Comment

        Working...
        X