Announcement

Collapse
No announcement yet.

Scipt to pull data from the new WU API

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

  • mikee123
    replied
    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)

    Leave a comment:


  • mikee123
    replied
    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.

    Leave a comment:


  • joegr
    replied
    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

    Leave a comment:


  • joegr
    replied
    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,

    Leave a comment:


  • sparkman
    replied
    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)

    Leave a comment:


  • mikee123
    replied
    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 !

    Leave a comment:


  • joegr
    replied
    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.

    Leave a comment:


  • mikee123
    replied
    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

    Leave a comment:


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

    Leave a comment:


  • mikee123
    replied
    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)

    Leave a comment:


  • joegr
    replied
    The URL for FireFox would be:
    Code:
    https://api.weather.com/v2/pws/observations/current?stationId=xxx&format=json&units=e&apiKey=xxxx
    just put your station and key in.

    Leave a comment:


  • joegr
    replied
    Originally posted by mikee123 View Post

    I tried that. Didnt work
    Sep-25 22:34:57 Error Compiling script C:\Program Files (x86)\HomeSeer HS3\scripts\WeatherObserved.vb: 'temp' is not a member of 'scriptcode31.VBWrapper.Observation'.
    Sep-25 22:34:57 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
    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.

    Leave a comment:


  • joegr
    replied
    Originally posted by mikee123 View Post
    Ok I just worked out how to display (set values etc...) I like your 'eyecandy' png's. Where did you get them from ?

    Where in the script would I add the calculation ? I tried adding

    temp = (temp-32) * 5/9

    under the
    UpdateNlog("O112",jcData.observations(0).imperial.temp,"",0) but it does not calculate. Its still displaying as F value
    See my post above for the temperature calculation. I added after posting, but I though that maybe you hadn't seen that post yet.

    I found weather icons in various places on the web and grabbed the ones I liked. Some may have also come from HS2. I'm been building the collection for a while.

    Leave a comment:


  • mikee123
    replied
    Originally posted by sparkman View Post
    The API allows pulling of non-imperial data. I believe you just need to change these from:

    Code:
    UpdateNlog("O112",jcData.observations(0).imperial.temp,"",0)
    to

    Code:
    UpdateNlog("O112",jcData.observations(0).temp,"",0)
    I tried that. Didnt work
    Sep-25 22:34:57 Error Compiling script C:\Program Files (x86)\HomeSeer HS3\scripts\WeatherObserved.vb: 'temp' is not a member of 'scriptcode31.VBWrapper.Observation'.
    Sep-25 22:34:57 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

    Leave a comment:


  • joegr
    replied
    Originally posted by sparkman View Post
    The API allows pulling of non-imperial data. I believe you just need to change these from:

    Code:
    UpdateNlog("O112",jcData.observations(0).imperial.temp,"",0)
    to

    Code:
    UpdateNlog("O112",jcData.observations(0).temp,"",0)
    Maybe for you, but there is no such field in the json I am receiving.
    observations
    0
    stationID "KMSGULFP21"
    obsTimeUtc "2019-09-25T21:29:25Z"
    obsTimeLocal "2019-09-25 16:29:25"
    neighborhood " Oakleigh Manor"
    softwareType "Custom version1.5"
    country "US"
    solarRadiation null
    lon -89.01924896
    realtimeFrequency null
    epoch 1569446965
    lat 30.44963074
    uv null
    winddir 180
    humidity 45
    qcStatus 1
    imperial
    temp 95
    heatIndex 101
    dewpt 74
    windChill 95
    windSpeed 2
    windGust 4
    pressure 29.84
    precipRate 0
    precipTotal 0
    elev 16

    Leave a comment:

Working...
X