As part of my blog series on APIs and Powershell, this first post will show you how you to post your weather data to Azure Log Analytics using Weather Underground. While I’m not the first to put weather data in Azure Log Analytics, it’s still a great example and I’m not using the same weather provider as them.
Why Weather Underground? I’m using a Acurite 5 in 1 sensor at my house. Acurite has the ability to pass your data to Weather Underground and Weather Underground offers free API keys. So I’m not putting just any weather up in the cloud, I’m putting my weather data from my house.
I’m not going to step by step how to connect your weather station to Weather Underground, because, its very easy and there other guides out there that help you do that.
You’ll need your API key from Weather Underground, which you can get here. https://www.wunderground.com/weather/api/d/pricing.html
You need need to signup with Weather Underground account to get add your Personal Weather Station and get its ID, it is used in the URL to get the data.
You’ll obviously need an Azure Log Analytics workspace. Signup here. https://azure.microsoft.com/en-us/services/log-analytics/
Once setup, you’ll need your Workspace and Primary keys.
Also need Powershell and you’ll need the Powershell Module OMSIngestionAPI, which I talked about in this post.
Determining the Best CMDLet to use
As of Powershell 3.0 and later we have two cmdlets that let us access web data. Invoke-WebRequest and Invoke-RestMethod. Determining which one to use requires either knowledge of the website you are going to be access, or two simply run each cmdlet against the URL and deciding which one is better. Here, I’ve run both against Weather Underground to determine which result I like better. Writing this post after having already looking at Weather Underground data I know that I am looking for a field called “current_observations”
$APIKey = "your weather underground key" $url = "http://api.wunderground.com/api/$apikey/conditions/q/tx/pws:KTXMANVE16.json" $result = invoke-webrequest -uri $url
If we use Invoke-webrequest we get this as the result. We would then have to do the following to get our current observations
$JSON = ConvertFrom-Json $Result.Content $weather = $JSON.current_observation
However, if we use Invoke-Restmethod we get a much cleaner result right off the bat.
It has already been converted from JSON to a Powershell object.
That’s a lot of data and the screen grab doesn’t even show it all.
Convert to JSON
Some of that data is useless to me, so I want to pare it down. I also want to trim it because Azure Log Analytics didn’t like some of the fields from Weather Underground.
$weather = $jsonresult.current_observation | Select-Object temp_f, relative_humidity, dewpoint_f, feelslike_f, feelslike_c, wind_dir, wind_mph, uv, weather
Now that we have the fields we want, we need to convert it back to JSON to post to Azure Log Analytics.
$weather = ConvertTo-Json $weather
# Replace with your Workspace ID $CustomerId = "OMS Workspace ID" # Replace with your Primary Key $SharedKey = "Primary Key" # Specify the name of the record type that you'll be creating $LogType = "Current_Conditions" # Specify a field with the created time for the records $TimeStampField = get-date $TimeStampField = $TimeStampField.GetDateTimeFormats(115) $APIKey = "api key here" # Gets Weather Data via Weather Underground from my Personal Weather Station $URL = "http://api.wunderground.com/api/$apikey/conditions/q/tx/pws:KTXMANVE16.json" #get weather from weather underground $JSONResult = Invoke-RestMethod -Uri $URL #select fields to upload $weather = $jsonresult.current_observation | Select-Object temp_f, relative_humidity, dewpoint_f, feelslike_f, feelslike_c, wind_dir, wind_mph, uv, weather #convert back to json $weather = ConvertTo-Json $weather # Submit the data to the API endpoint Send-OMSAPIIngestionFile -customerId $customerId -sharedKey $sharedKey -body $weather -logType $logType
Log Analytics Queries
Now that we have our data in Log Analytics, lets run some quick simple queries. One of the things I’m considering is wind turbines. On our property we get a lot of wind. Thats one of the things we loved about the property after we bought it, save for August, there’s almost always a breeze.
A lot of wind turbines have a start up speed of about 5mph, so lets find the wind for the last 7 days that was greater than 5mph and graph it visually.
Current_Conditions_CL | where wind_mph_d > 5 | summarize avg(wind_mph_d) by TimeGenerated | render timechart
Stepping through this query, Current_Conditions_CL is the custom log we’ve uploaded to Log Analytics. Where wind is greater than 5. Using summarize we take the average of the wind vield by time generated and then render a time chart.
As you can see we get some pretty good peak wind speed. But the really cool thing is the purple dots that Analytics has put on the graph, where it suspects some outliers. Clicking the last one auto produced this query and the graph, which is really cool.
Granted this information is pointless to me as its not really an out-liar its likely because it went from 6mph to over 10mph. I’m only sending my data up every 15 minutes, so its not going to report any ramp up or ramp down data points.
Next, lets get the average for the last 7 days, but only when its above 5mph.
Current_Conditions_CL | where wind_mph_d > 5 | project wind_mph_d | summarize avg(wind_mph_d)
The first two lines as the query above, so project pares the data down to only the field or fields we tell it to and again summarize avg() to get the result.
8mph is a pretty good constant speed for the types of turbines I’m looking at.
And, per Weather Undergrounds TOS, I have to display the WU badge whenever presenting data. So here is live data from my weather station outside my house. If you’re reading during the day, its probably hot, now that its summer.