Let's build a weather display that can predict the future! Well, forecast it at least . We will be building the temperature line in RED:

 

Capture.PNG

 

Currently we have a weather station running that pulls in our weather data locally, which is giving us the blue temperature line. You can either hook up your PI System to pull in other weather data (via HTML interface, UFL, or other source) or pull in current local weather via a script as well. The red future data predictions are what we'll be building with this tutorial.

 

So to get started I'll list the versions I have in place (basically, your PI Server has to include Future Data Capability):

1. PI Data Archive (3.4.395.64+)

2. PI Coresight 2015 (2.1.0.0+)

3. Tested against PS Version 4.0+

 

I'm pulling my future weather predictions from the Weather Underground API (API | Weather Underground ), so I signed up for an account and have access to up to 10 days of hourly future weather data via a key.

 

Once that is all in place, let's start going through the PowerShell code (note that I'm syntax highlighting in Python...as there is no native PowerShell highlighting on PI Square...this seems to be the closest color match).

 

First, versions and disclaimers:

# ***********************************************************************
# * DISCLAIMER:
# *
# * All sample code is provided by OSIsoft for illustrative purposes only.
# * These examples have not been thoroughly tested under all conditions.
# * OSIsoft provides no guarantee nor implies any reliability,
# * serviceability, or function of these programs.
# * ALL PROGRAMS CONTAINED HEREIN ARE PROVIDED TO YOU "AS IS"
# * WITHOUT ANY WARRANTIES OF ANY KIND. ALL WARRANTIES INCLUDING
# * THE IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY
# * AND FITNESS FOR A PARTICULAR PURPOSE ARE EXPRESSLY DISCLAIMED.
# ************************************************************************


#$PSVersionTable
#Name                           Value                                               
#----                           -----                                               
#PSVersion                      4.0                                                 
#WSManStackVersion              3.0                                                 
#SerializationVersion           1.1.0.1                                             
#CLRVersion                     4.0.30319.34209                                     
#BuildVersion                   6.3.9600.16406                                      
#PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0}                                
#PSRemotingProtocolVersion      2.2    

 

Then lets do the actual pulling from the weather underground API. It does a web request which returns a JSON obect, which is awesome for PowerShell as we can work with the objects returned!

 

#Weather Underground Key:
#XXXXXXXXXXXXXX
#GET http://api.wunderground.com/api/XXXXXXXXXXXXXX/features/settings/q/query.format
#http://api.wunderground.com/api/XXXXXXXXXXXXXX/hourly10day/q/CA/San_Francisco.json
$currenturl = "http://api.wunderground.com/api/XXXXXXXXXXXXXX/hourly10day/q/CA/San_Leandro.json"
$response = Invoke-WebRequest -Method GET -Uri $currenturl | ConvertFrom-Json
$temperaturesSet = $response.hourly_forecast.temp.english
$timeSet = $response.hourly_forecast.fcttime.pretty

 

Now that we have the response in the $timeset variable and a $temperatureSet variable we have both a time and a value. All we need now is the tag name. So, we can start to build out our PowerShell Tools for the PI System logic. I've used the default PI Data Archive on my local machine.

 

#Connection to your PI Data Archive
$connection = Connect-PIDataArchive -PIDataArchiveMachineName "localhost"

 

Then let's auto create the future data PI Point (your name may vary):

 

# Assign Tag Name to the PI Point
$tagName = 'MyForecastTemperatureName' 
                  
#Create the PI Point associated with that attribute
if((Get-PIPoint -Name $tagName -Connection $connection) -eq $null)
{ 
    #Specify that it will be a future data point
    $futureAttribute =  New-Object 'system.collections.generic.dictionary[String,System.Object]'
    $futureAttribute.Add('Future','1')
    #Create the PI Point
    Add-PiPoint -Name $tagName -Attributes $futureAttribute -Connection $connection
}

 

Then we'll create a collection of the recorded values returned from the JSON request above (the time is a little weird, so I took a work around of adding 7 hours, but it should work out, your local timezone may vary, so be sure to check on that:

 

#Manipulate TimeStamp String to output something friendly for AF to input
#example $timestamp = "11:00 AM PDT on May 04, 2015"
$timestampu = @(); foreach($tsz in $timeSet) { $timestampu += ([Datetime]::Parse(($tsz -replace "PDT on ",""))) }
             
for($i=0; $i -lt $timestampu.Count; $i++)
{
  Add-PIValue -PointName $tagName -Time $timestampu[$i].AddHours(7) -Value $temperaturesSet[$i] -Connection $connection
}

 

Note that this is a small amount of data so I didn't bundle it into a single request to do just one call back to the PI Data Archive. If you have a lot of information passing through, or are doing this for multiple queries make sure to optimize your PI Data Archive call backs to just one rather than looping through like I've done.

 

That's it! One thing I've been doing is setting it to run as a scheduled task every week to pull in the hourly data for the week ahead. Also, a ton of other data is available via the weather underground API, so don't feel limited to temperature!

 

Thanks,

Eric