Announcement

Collapse
No announcement yet.

assign hs device values while looping through nested for statement

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

    assign hs device values while looping through nested for statement

    I am working through a script that grabs a json string and breaks it into rows and colums. Basicly I get 4 rows and 4 col in each row.

    What is the most efficient way to assign these values to a homeseer device?

    Code:
    I use this: For row As Integer = 0 To data.Count - 1
    rowdata = data(row)
    For col As Integer = 0 To rowdata.Count - 1
    item = rowdata(col)
    hs.WriteLog(logName, "row " & row + 1 & " column " & col + 1 & " entry=" & item)
    hs.SetDeviceValueByName("Stove ", item)
    hs.SetDeviceString(655, item, true)
    Next
    Next
    You can see I have one device named "Stove" created.

    I know this should be simple, but I am struggling as a newbie with .net.

    Do I use nested select case here, or is there an easier way?
    Kirk

    http://cleverhouseautomation.ca
    http://southcoastwebsitedesign.ca

    #2
    It depends on at least a couple of things

    (1) Is there a device naming convention of some type for the devices you want to populate?
    (2) Is the returned 'item' always numeric, or can it be a string? If numeric you can use SetDeviceValueByName; otherwise you'll want SetDeviceString. You probably don't need both.

    On way that pops into mind for the device naming part is to create an array of device names, referenced by row, column.

    Code:
        ' populate the device name cross reference table
        Dim dvNmTbl(4, 4)
        dvNmTbl(0, 0) = "Kitchen Stove"
        dvNmTbl(0, 1) = "Garage Water Heater"
        ' ... etc.
    
    
            ' then inside the loops, to do the assignment
            If DvNmTbl(row, col) <> "" Then
                hs.SetDeviceValueByName(DvNmTbl(row, col), item)
            End If
    If item can be a string there's an additional step required.

    Comment


      #3
      What do you mean by device naming convention? Basically the 4 items. Stove, Dryer, Dishwasher, yet to be determined. (These are the biggest power draws on my solar system) For each device I retrieve 4 variable (All Numbers)Power Factor, Volts, Amps and watts.

      This is what is being retrieved.


      {"data":[[0,12313,0,0],[0,12313,0,0],[520,12315,20,1],[0,12314,0,0]]}
      Kirk

      http://cleverhouseautomation.ca
      http://southcoastwebsitedesign.ca

      Comment


        #4
        Hmm. I am not getting errors, but I am also not getting output to my devices:

        Here is the script:

        Code:
        Imports Newtonsoft.Json
        
        Sub Main(ByVal parm as object)
        Dim logName As String = "JSON Parse"
        Dim host As String = "192.168.1.118"
        Dim page As String = "/getdata"
        Dim strip_tags As Boolean = False
        Dim port As Integer = 8080
        
        Try
        Dim resp As String = hs.GetURL(host, page, strip_tags, port)
        If Left(resp, 1) <> "{" Then
        ' non-JSON response
        hs.WriteLogEx(logName, resp, "#ff0000")
        return
        End If
        
        Dim obj As Object = JsonConvert.DeserializeObject(resp)
        Dim data As Object = obj.Item("data")
        Dim rowdata As Object
        Dim item As Integer
        
        ' populate the device name cross reference table
        Dim dvNmTbl(4, 4)
        dvNmTbl(0, 0) = "Stove Power Factor"
        dvNmTbl(0, 1) = "Stove Voltage"
        dvNmTbl(0, 2) = "Stove Current"
        dvNmTbl(0, 3) = "Stove Watts"
        
        dvNmTbl(1, 0) = "Dishwasher Power Factor"
        dvNmTbl(1, 1) = "Dishwasher Voltage"
        dvNmTbl(1, 2) = "Dishwasher Current"
        dvNmTbl(1, 3) = "Dishwasher Watts"
        
        For row As Integer = 0 To data.Count - 1
        rowdata = data(row)
        For col As Integer = 0 To rowdata.Count - 1
        item = rowdata(col)
        hs.WriteLog(logName, "row " & row + 1 & " column " & col + 1 & " entry=" & item)
        
        
        If DvNmTbl(row, col) <> "" Then
        hs.SetDeviceValueByName(DvNmTbl(row, col), item)
        hs.SetDeviceStringByName(DvNmTbl(row, col), item, true)
        End If
        Next
        Next
        
        Catch ex As Exception
        'Error trapping
        hs.WriteLogEx(logName & " Exception", ex.Message, "#ff0000")
        End Try
        
        End Sub
        Kirk

        http://cleverhouseautomation.ca
        http://southcoastwebsitedesign.ca

        Comment


          #5
          A device naming convention would be applicable if the names contained the row/column numbers. They aren't in this case so that doesn't apply.

          Change
          Code:
          Dim item As Integer
          to
          Code:
          Dim item As Object
          and
          Code:
          hs.SetDeviceValueByName(DvNmTbl(row, col), item)
          to
          Code:
          hs.SetDeviceValueByName(DvNmTbl(row, col), CDbl(item))
          Get rid of
          Code:
          hs.SetDeviceStringByName(DvNmTbl(row, col), item, true)
          The first parameter of that is the device refid (integer) not the name (string). As I said above you only need to set the device value OR string, and value is what you want in this case.

          You might want to put a WriteLog inside the conditional where you do the SetDeviceValue, so you know that you got a name match.

          Code:
          hs.WriteLog(logName, "setting " & DvNmTbl(row, col) & " to " & item.toString)

          Comment


            #6
            perfect. I will work through that and see how it does.
            Kirk

            http://cleverhouseautomation.ca
            http://southcoastwebsitedesign.ca

            Comment

            Working...
            X