Announcement

Collapse
No announcement yet.

Scipt to pull data from the new WU API

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

    #31
    I didnt see your post, I just tried, but...

    this

    Code:
           UpdateNlog("O112",jcData.observations(0).imperial.temp - 32)/1.8,"",0)
    results in these errors:
    Sep-25 22:43:46 Error Compiling script C:\Program Files (x86)\HomeSeer HS3\scripts\WeatherObserved.vb: End of statement expected.
    Sep-25 22:43:46 Error Compiling script C:\Program Files (x86)\HomeSeer HS3\scripts\WeatherObserved.vb: Argument not specified for parameter 'Dstr' of 'Public Sub UpdateNlog(HDcode As String, Dvalue As Double, Dstr As String, Always As Integer)'.
    Sep-25 22:43:46 Error Compiling script C:\Program Files (x86)\HomeSeer HS3\scripts\WeatherObserved.vb: Argument not specified for parameter 'Always' of 'Public Sub UpdateNlog(HDcode As String, Dvalue As Double, Dstr As String, Always As Integer)'.
    Sep-25 22:43:46 Error Compiling script C:\Program Files (x86)\HomeSeer HS3\scripts\WeatherObserved.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.
    but this conversion for pressure works

    Code:
    UpdateNlog("O118",jcData.observations(0).imperial.pressure*33.86,"",0)

    Comment


      #32
      You are missing the first "(" that I added,
      UpdateNlog("O112",(jcData.observations(0).imperial.temp - 32)/1.8,"",0)

      Comment


        #33
        Originally posted by joegr View Post

        You would have to add the new "temp" field in the Public Class Observation section. I didn't because at least for me, the api does not return that field. You can put your api URL in the address field of FireFox and it will show you the decoded json to see if you have that field.
        This is what the json decodes:
        stationID "IWESTC4"
        obsTimeUtc "2019-09-25T21:57:19Z"
        obsTimeLocal "2019-09-25 22:57:19"
        neighborhood "Southend-on-Sea"
        softwareType null
        country "GB"
        solarRadiation null
        lon 0.7
        realtimeFrequency null
        epoch 1569448639
        lat 51.5
        uv 0
        winddir 166
        humidity 91
        qcStatus 1
        imperial
        temp 60
        heatIndex 60
        dewpt 58
        windChill 60
        windSpeed 0
        windGust 0
        pressure 30
        precipRate 0
        precipTotal 0
        elev 23

        not sure I understand how to add the new "temp" field in the Public Class Observation section

        Comment


          #34
          Originally posted by mikee123 View Post
          ...not sure I understand how to add the new "temp" field in the Public Class Observation section
          Try the URL in FireFox first to see if you even have that field. I don't, but it may depend on what your weather station sends. Otherwise, the line I posted should convert it for you.

          Comment


            #35
            Originally posted by joegr View Post
            You are missing the first "(" that I added,
            UpdateNlog("O112",(jcData.observations(0).imperial.temp - 32)/1.8,"",0)
            That did it. I tried a few things as I thought it was just a little syntax mistake but couldnt find it. Getting to late, should hit the pillow but I think I'm 99% there. Thanks for the help !

            Comment


              #36
              Originally posted by joegr View Post

              Maybe for you, but there is no such field in the json I am receiving.
              Looking at my script, I have units=m in the url, rather than units=e

              The API docs state the following:

              Unit of Measure Requirement

              The unit of measure for the response. The following values are supported:
              • e = English units
              • m = Metric units
              • h = Hybrid units (UK)
              HS 3.0.0.548: 1990 Devices 1172 Events
              Z-Wave 3.0.1.262: 126 Nodes on one Z-Net

              Comment


                #37
                Originally posted by sparkman View Post

                Looking at my script, I have units=m in the url, rather than units=e

                The API docs state the following:

                Unit of Measure Requirement

                The unit of measure for the response. The following values are supported:
                • e = English units
                • m = Metric units
                • h = Hybrid units (UK)
                Okay, that would explain it then.
                Thanks,

                Comment


                  #38
                  Okay, give this a try (uses UK units):

                  Code:
                  Imports System.Web.Script.Serialization
                  
                  Const Fname As String  = "WeatherObserved.vb"          ' this file name
                  Const Lcolor As String = "#ffaa56"                     ' log color for normal
                  
                  Dim json As New JavaScriptSerializer
                  
                  Sub Main(ByVal Parms As Object)
                  
                      Try
                          Dim DataRaw As String = hs.geturl ("https://api.weather.com","/v2/pws/observations/current?stationId=xxx&format=json&units=h&apiKey=xxx",FALSE ,"80")
                  
                       if left(DataRaw,5) = "ERROR" then
                          hs.WriteLogDetail(Fname,DataRaw, "#FF0000", 2,Fname & ":Main",1)
                       else
                  
                          Dim jcData As WUC = json.Deserialize(Of WUC)(DataRaw)
                  
                         UpdateNlog("O112",jcData.observations(0).uk_hybrid.temp,"",0)
                         UpdateNlog("O113",jcData.observations(0).uk_hybrid.heatIndex,"",0)
                         UpdateNlog("O114",jcData.observations(0).uk_hybrid.dewpt,"",0)
                         UpdateNlog("O115",jcData.observations(0).uk_hybrid.windChill,"",0)
                         UpdateNlog("O116",jcData.observations(0).uk_hybrid.windSpeed,"",0)
                         UpdateNlog("O117",jcData.observations(0).uk_hybrid.windGust,"",0)
                         UpdateNlog("O123",jcData.observations(0).winddir,"",0)
                         UpdateNlog("O118",jcData.observations(0).uk_hybrid.pressure,"",0)
                         UpdateNlog("O119",jcData.observations(0).uk_hybrid.precipRate,"",0)
                         UpdateNlog("O120",jcData.observations(0).uk_hybrid.precipTotal,"",0)
                         UpdateNlog("O121",jcData.observations(0).uk_hybrid.elev,"",0)
                         UpdateNlog("O122",jcData.observations(0).humidity,"",0)
                         UpdateNlog("O124",jcData.observations(0).uv,"",0)
                  
                  
                  
                       end if
                  
                  Catch Ex As Exception
                      hs.WriteLogDetail(Fname,Ex.Message, "#FF0000", 2,Fname & ":Main",1)
                      End Try
                  
                  End Sub
                  
                      Public Class UkHybrid
                          Public Property temp As Integer
                          Public Property heatIndex As Integer
                          Public Property dewpt As Integer
                          Public Property windChill As Integer
                          Public Property windSpeed As Integer
                          Public Property windGust As Integer
                          Public Property pressure As Double
                          Public Property precipRate As Double
                          Public Property precipTotal As Double
                          Public Property elev As Integer
                      End Class
                  
                  Public Class Imperial
                          Public Property temp As Integer
                          Public Property heatIndex As Integer
                          Public Property dewpt As Integer
                          Public Property windChill As Integer
                          Public Property windSpeed As Integer
                          Public Property windGust As Integer
                          Public Property pressure As Double
                          Public Property precipRate As Double
                          Public Property precipTotal As Double
                          Public Property elev As Integer
                      End Class
                  
                      Public Class Observation
                          Public Property stationID As String
                          Public Property obsTimeUtc As DateTime
                          Public Property obsTimeLocal As String
                          Public Property neighborhood As String
                          Public Property softwareType As String
                          Public Property country As String
                          Public Property solarRadiation As Object
                          Public Property lon As Double
                          Public Property realtimeFrequency As Object
                          Public Property epoch As Integer
                          Public Property lat As Double
                          Public Property uv As Object
                          Public Property winddir As Integer
                          Public Property humidity As Integer
                          Public Property qcStatus As Integer
                          Public Property imperial As Imperial
                          Public Property uk_hybrid As UkHybrid
                      End Class
                  
                      Public Class WUC
                          Public Property observations As Observation()
                      End Class  
                  
                  Sub UpdateNlog (HDcode As String, Dvalue As Double, Dstr as String, Always as Integer) ' this routine updates a device and logs it if
                                                                                         ' it changed and if logging is turned on for the device or if always is set
                    Try
                      dim Dref    = hs.GetDeviceRef(HDcode)                                   ' get the reference to the device based on the house/device code
                      dim dv      = hs.GetDeviceByRef(Dref)                                   ' get device settings
                      dim Du      = hs.GetDeviceByRef(Dref)                                   ' get device data
                      dim Dstring = hs.DeviceVSP_GetStatus(Dref, Dvalue, 1)                   ' get the string
                  
                      hs.SetDeviceValueByRef (Dref,Dvalue,TRUE)                               ' update the device value
                      if Dstr > "" then
                         hs.SetDeviceString (Dref,Dstr,true)                                  ' update the device string if provided
                         Dstring = Dstr                                                       ' for the log
                      end if
                  
                      if (dv.MISC_Check(hs, Enums.dvMISC.NO_LOG) = 0) or (Always = 1) then    ' should it be logged?
                         hs.WriteLogDetail(Fname,Du.Location(hs) & " " & Du.name(hs) & " set to: " & Dstring, Lcolor, 9,Fname,1)
                      end if
                  
                  
                  Catch Ex As Exception
                        hs.WriteLogDetail(Fname,Ex.Message, "#FF0000", 1,Fname & ":UpdateNlog",9)
                  End Try
                  
                  end sub

                  Comment


                    #39
                    Originally posted by joegr View Post
                    Okay, give this a try (uses UK units):
                    Yes that works too. I still use a couple of conversions to display the units I want, but I have got that all working. Next project is trying to get the script for the weatherforecast working (from the beginning of this thread)

                    Might have to wait for the weekend though.

                    Comment


                      #40
                      I am still using this script to pull data into HS via the api. I have seen on the website that you can specify decimal in the geturl, but not sure where and how, I tried a few things but only get errors.

                      Request by PWS Station ID: Required Parameters: format, units, apiKey | Optional Parameter: numericPrecision
                      https://api.weather.com/v2/pws/obser...=e&apiKey=your ApiKey

                      Parameter Name - numericPrecision
                      Valid Parameter Value - decimal
                      Description - Optional parameter. Set to ‘decimal’ to ensure data is returned in decimal format when needed. Will return integers if this value is not used.

                      Code:
                      Try
                      Dim DataRaw As String = hs.geturl ("https://api.weather.com","/v2/pws/observations/current?stationId=IWESTC4&format=json&units=h&apiKey=xxxxxxx x,FALSE ,"80")
                      
                      if left(DataRaw,5) = "ERROR" then
                      hs.WriteLogDetail(Fname,DataRaw, "#FF0000", 2,Fname & ":Main",1)
                      else
                      
                      Dim jcData As WUC = json.Deserialize(Of WUC)(DataRaw)
                      
                      UpdateNlog("O112",jcData.observations(0).uk_hybrid.temp,"",0 )
                      UpdateNlog("O113",jcData.observations(0).uk_hybrid.heatIndex ,"",0)

                      Comment

                      Working...
                      X