Announcement

Collapse
No announcement yet.

HS3 and JSON

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

    HS3 and JSON

    Hello,

    I am looking for some help regarding the following task:
    I have a device that i can call wia JSON as follow:
    The following command
    http://192.168.X.Y/api/xdevices.json?cmd=10
    returns:
    {"product":"Eco-devices","T1_PTEC":"TH..","T1_PAPP":750,"T1_BASE":19202918," T2_PTEC":"----","T2_PAPP":0,"T2_BASE":0,"INDEX_C1":28015854,"INDEX_C2" :0}

    I would like to make a simple script that I could call every X minutes that would return the T1_PAPP value (ex:750)

    Could someone help me write this simple script (i am newbie...)
    Thanks a lot

    #2
    This is a script that will put that value into the log, you will need to download the Newtonsoft.Json.dll file from http://www.newtonsoft.com/json and add a line to your settings.ini file in the ScriptingReferences line to enable HS to use this DLL. Take a look here for the idea http://forums.homeseer.com/showthrea...highlight=json

    Code:
    Imports Newtonsoft.Json
    Imports Newtonsoft.Json.Linq
    
    Sub Main(ByVal Parms As Object)
    
        Try
            Dim DataStr As String = "{""product"":""Eco-devices"",""T1_PTEC"":""TH.."",""T1_PAPP"":750,""T1_BASE"":19202918,"" T2_PTEC"":""----"",""T2_PAPP"":0,""T2_BASE"":0,""INDEX_C1"":28015854,""INDEX_C2"" :0}"
            Dim obj As New Object
    
            obj = JsonConvert.DeserializeObject(DataStr)
    
            hs.writelog("JSON", obj.Item("T1_PAPP").ToString)
    
        Catch ex As Exception
            hs.writelog("JSON", "Exception: " & ex.message)
        End Try
    
    End Sub
    I think .net has a way of doing it itself now but the above option is fairly straightforward...you will need to change the top line to download that text, obviously I can't do it but it should be something like

    Dim DataStr As String = hs.GetURL("http://192.168.X.Y","api/xdevices.json?cmd=10",True,80)

    or similar...

    Comment


      #3
      HS3 and Json

      Hi MrHappy,
      I downloaded and installed Newtonsoft.json.dll file.
      Now can you confirm where I should add this file? I do not have a ScriptingReferences line in my settings.ini file. I have [settings],[Display],[Reminders] and [LogcolorPairs]...

      Then,should I restart HS3 ?
      Excuse my ignorance......

      Thanks

      Comment


        #4
        If you don't already have a ScriptingReferences= line somewhere (please check the file) under the [Settings] key then please add one that says;

        Code:
        ScriptingReferences=Newtonsoft.Json;Newtonsoft.Json.dll
        save the file and then place the Newtonsoft.Json.dll file anywhere in the main HS3 directory. Give the script a go and then see whether or not it works, I sometimes have needed to restart HS3 for DLL references to work correctly but it should not need to be restarted.

        Comment


          #5
          HS3 and Json

          Thanks MrHappy,
          I'll test and comment

          Comment


            #6
            HS3 and Json

            Hi MrHappy,
            It works !! I get the value in the log !

            What would be the modification of the script that would parse this T1_PAPP value every X min ?

            Many thanks in advance

            Comment


              #7
              Originally posted by johndoe2011 View Post
              Hi MrHappy,
              It works !! I get the value in the log !

              What would be the modification of the script that would parse this T1_PAPP value every X min ?

              Many thanks in advance
              Good to hear, you are better off just calling the script from a recurring event every X minutes...

              Comment


                #8
                HS3 and Json

                Yes but if I call the script every x minutes, the
                http://192.168.X.Y/api/xdevices.json?cmd=10 command will not be called and the T1_PAPP will be the same. I guess the script should include the :
                http://192.168.X.Y/api/xdevices.json?cmd=10 somewhere
                Right ?

                Comment


                  #9
                  This is something I cannot test as I put in the post, I think that it will be (but as I say because I don't have access to that URL it is a slight guess) you replace this line

                  Dim DataStr As String = "{""product"":""Eco-devices"",""T1_PTEC"":""TH.."",""T1_PAPP"":750,""T1_BASE"":1 9202918,"" T2_PTEC"":""----"",""T2_PAPP"":0,""T2_BASE"":0,""INDEX_C1"":28015854,""INDEX _C2"" :0}"

                  With

                  Dim DataStr As String = hs.GetURL("http://192.168.X.Y","/api/xdevices.json?cmd=10",True,80)

                  Try it and see what happens...

                  Comment


                    #10

                    Comment


                      #11
                      h

                      but :
                      hs.GetURL("http://192.168.x.y","/api/xdevices.json?cmd=10",True,80)

                      works perfectly !!

                      Thanks a lot

                      Comment


                        #12
                        Nested JSON

                        Hi,

                        How do I do this with nested JSON data, e.g.

                        {
                        "Head" : {
                        "RequestArguments" : {
                        "DataCollection" : "CommonInverterData",
                        "DeviceClass" : "Inverter",
                        "DeviceId" : "1",
                        "Scope" : "Device"
                        },
                        "Status" : {
                        "Code" : 12,
                        "Reason" : "No inverter with this index found.",
                        "UserMessage" : ""
                        },
                        "Timestamp" : "2016-10-26T22:10:12+11:00"
                        }
                        }

                        Tried all kinds of way to get a specific element (e.g. Head.RequestArguments.Scope) but have not been successful so far. All help welcome. THANK YOU

                        Jan

                        Comment


                          #13
                          Nested json script problem solved

                          Hi all,

                          In case anyone else has the same problem with nested json vb scripts, finally found a way to do it:

                          JSON file:

                          {
                          "Head" : {
                          "RequestArguments" : {
                          "DataCollection" : "CommonInverterData",
                          "DeviceClass" : "Inverter",
                          "DeviceId" : "1",
                          "Scope" : "Device"
                          },
                          "Status" : {
                          "Code" : 0,
                          "Reason" : "",
                          "UserMessage" : ""
                          },
                          "Timestamp" : "2014-11-06T11:40:32+00:00"
                          },
                          "Body" : {
                          "Data" : {
                          "DAY_ENERGY" : {
                          "Value" : 3000,
                          "Unit" : "Wh"
                          },
                          "FAC" : {
                          "Value" : 50,
                          "Unit" : "Hz"
                          },
                          "IAC" : {
                          "Value" : 1.36,
                          "Unit" : "A"
                          },
                          "IDC" : {
                          "Value" : 1.05,
                          "Unit" : "A"
                          },
                          "PAC" : {
                          "Value" : 334,
                          "Unit" : "W"
                          },


                          ===

                          Example vb.net script to read PAC value from json file:

                          Imports Newtonsoft.Json
                          Imports Newtonsoft.Json.Linq

                          Sub Main(params as object)

                          Dim jsonText As String 'this is the string of json data that needs decoding
                          Din PAC As String
                          Dim obj As New Object

                          jsonText = hs.GetURL("http:\\192.168.2.4", "/solar_api/v1/GetInverterRealtimeData.cgi?Scope=Device&DeviceID=1&DataColl ection=CommonInverterData",TRUE,80)
                          hs.WriteLog("Fronius json", jsonText)

                          Dim deserialized = JsonConvert.DeserializeObject(jsonText)
                          PAC = deserialized("Body")("Data")("PAC")("Value")

                          hs.WriteLog("Fronius", PAC)

                          end sub

                          Comment


                            #14

                            Comment

                            Working...
                            X