Announcement

Collapse
No announcement yet.

Script Newbie

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

    Script Newbie

    I have been using HS for many years. I used to script allot way back before it was .net. I have recently built our new house and am slowly getting HS integration work done. I am not even sure where to start as far as catching up on scripting again.

    What I trying to do is write a script that can grab a datastream from an internal IP address http://192.168.1.110:8080/getdata and explode the return value and assign to homeseer devices. The script will run every couple of minutes. The returned string from the IP address looks like this:


    {"data":[[-842,12176,6880,-704],[818,12176,1500,148],[-828,12178,7200,-725],[805,12177,1920,187]]}

    This is part of my off grid solar energy monitor.

    Does anyone have a very basic script that does something similar that they can share? I can then dig through that and start to see how to expand and build on it.

    Any help would be appreciated.
    Kirk

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

    #2
    One off the shelf solution is to use my datascraper script: https://forums.homeseer.com/forum/3r...and-homeseer-4

    Click image for larger version  Name:	Capture.PNG Views:	0 Size:	94.3 KB ID:	1392500

    The following settings create the these devices:


    Code:
    [Grab1]
    path=http://192.168.1.110:8080/getdata
    TextFile=1
    Encoding=
    Username=
    Password=
    Options=
    UserAgent=
    Devicemode=0
    StripHTML=0
    UseIE=0
    SSLMode=0
    Pattern1=(?s)\[\[(.*?),(.*?),(.*?),(.*?)],\[(.*?),(.*?),(.*?),(.*?)],\[(.*?),(.*?),(.*?),(.*?)],\[(.*?),(.*?),(.*?),(.*?)]]
    
    DeviceName1=Value 1
    DeviceText1=[0]
    DeviceValue1=[0]
    
    DeviceName2=Value 2
    DeviceText2=[1]
    DeviceValue2=[1]
    
    DeviceName3=Value 3
    DeviceText3=[2]
    DeviceValue3=[2]
    
    DeviceName4=Value 4
    DeviceText4=[3]
    DeviceValue4=[3]
    
    DeviceName5=Value 5
    DeviceText5=[4]
    DeviceValue5=[4]
    
    DeviceName6=Value 6
    DeviceText6=[5]
    DeviceValue6=[5]
    
    DeviceName7=Value 7
    DeviceText7=[6]
    DeviceValue7=[6]
    
    DeviceName8=Value 8
    DeviceText8=[7]
    DeviceValue8=[7]
    
    DeviceName9=Value 9
    DeviceText9=[8]
    DeviceValue9=[8]
    
    DeviceName10=Value 10
    DeviceText10=[9]
    DeviceValue10=[9]
    
    DeviceName11=Value 11
    DeviceText11=[10]
    DeviceValue11=[10]
    
    DeviceName12=Value 12
    DeviceText12=[11]
    DeviceValue12=[11]
    You can add more or select the metrics you want as necessary.
    Jon

    Comment


      #3
      Your data is JSON format, which can be parsed using the NewtonSoft package installed with HomeSeer. Here is a script demonstrating this.

      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
      
              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)
                  Next
              Next
      
          Catch ex As Exception
              'Error trapping
              hs.WriteLogEx(logName & " Exception", ex.Message, "#ff0000")
          End Try
      
      End Sub

      Results
      Code:
      Jun-09 22:03:21	 	JSON Parse	row 4 column 4 entry=187
      Jun-09 22:03:21	 	JSON Parse	row 4 column 3 entry=1920
      Jun-09 22:03:21	 	JSON Parse	row 4 column 2 entry=12177
      Jun-09 22:03:21	 	JSON Parse	row 4 column 1 entry=805
      Jun-09 22:03:21	 	JSON Parse	row 3 column 4 entry=-725
      Jun-09 22:03:21	 	JSON Parse	row 3 column 3 entry=7200
      Jun-09 22:03:21	 	JSON Parse	row 3 column 2 entry=12178
      Jun-09 22:03:21	 	JSON Parse	row 3 column 1 entry=-828
      Jun-09 22:03:21	 	JSON Parse	row 2 column 4 entry=148
      Jun-09 22:03:21	 	JSON Parse	row 2 column 3 entry=1500
      Jun-09 22:03:21	 	JSON Parse	row 2 column 2 entry=12176
      Jun-09 22:03:21	 	JSON Parse	row 2 column 1 entry=818
      Jun-09 22:03:21	 	JSON Parse	row 1 column 4 entry=-704
      Jun-09 22:03:21	 	JSON Parse	row 1 column 3 entry=6880
      Jun-09 22:03:21	 	JSON Parse	row 1 column 2 entry=12176
      Jun-09 22:03:21	 	JSON Parse	row 1 column 1 entry=-842
      Jun-09 22:03:21	 	Event	Running script in background: C:/Program Files/HomeSeer HS3/scripts/json_parse1.vb

      Comment


        #4
        That is awesome guys. So zwolfpack, if I then wanted to take those results that you have and assign to homseseer virtual devices, what would I script?
        Kirk

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

        Comment


          #5
          Hmm. I am missing something. If I copy this into a txt file and put in my scripts folder, when I try and run it I am getting:
          un-10 4:47:06 PM Error Running script, script run or compile error in file: F:/Program Files/HomeSeer HS3/scripts/eydro.txt1006:Expected ')' in line 3 More info: Expected ')'
          Kirk

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

          Comment


            #6
            In this case the best function to set the device values is probably
            Code:
            hs.SetDeviceValueByName(device_name, value)
            Coding for this would depend on how you want to name the devices. Naming can include the 'room' and 'floor' if desired.

            Comment


              #7
              Originally posted by ldragon99 View Post
              Hmm. I am missing something. If I copy this into a txt file and put in my scripts folder, when I try and run it I am getting:
              File needs a .vb extension.

              Comment


                #8
                Tried that and it changes the error:
                Jun-10 5:26:28 PM Error Compiling script F:\Program Files\HomeSeer HS3\scripts\eydro.vb: 'JsonConvert' is not declared. It may be inaccessible due to its protection level.
                Jun-10 5:26:28 PM Error Compiling script F:\Program Files\HomeSeer HS3\scripts\eydro.vb: Namespace or type specified in the Imports 'System.Core' doesn't contain any public member or cannot be found. Make sure the namespace or the type is defined and contains at least one public member. Make sure the imported element name doesn't use any aliases.
                Kirk

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

                Comment


                  #9
                  Did it work for you as you have it written? I copied and pasted into a file. What am I missing? Like I said pretty new when it comes to .vb
                  Kirk

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

                  Comment


                    #10
                    Oh yeah, I forgot about this - You need to modify the ScriptingReferences setting.

                    Stop HS, then locate file settings.ini in the Config subfolder below the HS root folder. Look for a line starting with 'ScriptingReferences', and if found add this entry to whats already there. If not found, add the entire line:
                    Code:
                    ScriptingReferences=Newtonsoft.Json;bin\homeseer\Newtonsoft.Json.dll
                    What I posted does work, it generates the output shown.

                    Comment


                      #11
                      Strange. I added that line to the settings ini and still get the errors.
                      Jun-10 6:41:42 PM Error Compiling script F:\Program Files\HomeSeer HS3\scripts\eydro.vb: 'JsonConvert' is not declared. It may be inaccessible due to its protection level.
                      Jun-10 6:41:42 PM Error Compiling script F:\Program Files\HomeSeer HS3\scripts\eydro.vb: Namespace or type specified in the Imports 'Newtonsoft.Json' doesn't contain any public member or cannot be found. Make sure the namespace or the type is defined and contains at least one public member. Make sure the imported element name doesn't use any aliases.
                      Jun-10 6:41:42 PM Error Compiling script F:\Program Files\HomeSeer HS3\scripts\eydro.vb: Namespace or type specified in the Imports 'System.Core' doesn't contain any public member or cannot be found. Make sure the namespace or the type is defined and contains at least one public member. Make sure the imported element name doesn't use any aliases

                      Restarted as well
                      Kirk

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

                      Comment


                        #12
                        So I saw that the path was wrong. mine is Bin\ not bin.

                        I changed it. Think I may have it now.
                        Kirk

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

                        Comment

                        Working...
                        X