Understanding How to Use IPICalculation

Discussion created by rdube02 Champion on Apr 2, 2013
Latest reply on Apr 3, 2013 by hanyong

Hi everyone.


So, I've been struggling for about a week now trying to really understand how to make IPICalculation do what I want it to do. I've downloaded all of the help files and studied them, but it's just not working.


Here's the case:


I have a tag called "System_2_Mode_Reason", which changes whenever the mode of a machine changes throughout the day. It could be "dumping", "contamination", "normal", etc...  There are maybe 20 different things that could be in there depending on what the operator chose from a dropdown list.


What I am trying to do in Processbook is actually collect how many times per day that tag had "contamination" as a value. Maybe monday the tag was equal to "contamination" 4 times. Tuesday 0. Wednesday 20. Etc.....  I want to be able, in code, to use a defined time range - say one week - have an array of counts for each day, even if there were zero for the day.


Here's the Problem:


I thought I could accomplish this with IPICalculation by using an approach like this:



PE_Expression = "IF 'SYSTEM_2_Mode_Reason' = ""Embedded"" THEN 1 ELSE 0"

strTag = "SYSTEM_2_Mode_Reason"

Set srv = Servers("k2ar1pixx")

Set ipiCalc = srv

Set pt = srv.PIPoints(strTag)

Set myStartTime = New PITimeServer.DynamicTime
Set myEndTime = New PITimeServer.DynamicTime

myStartTime.InputString = Format(DateAdd("d", -7, Now()), "mm/dd/yyyy HH:mm:ss")

myEndTime.InputString = Format(Now(), "mm/dd/yyyy HH:mm:ss")

duration = "7d" ' 7 dayu calculation period
' calculate hourly total for the entire time period (180 days)

Set nvsSum = ipiCalc.ExpressionSummaries(StartTime, EndTime, duration, PE_Expression, PISDK.ArchiveSummariesTypeConstants.asTotal, CalculationBasisConstants.cbEventWeighted)

' extract the pivalues for total from the result

Set valsum = nvsSum("Total").Value

 I have "valsum" decalred as PIValues - so I would think after the above use of ExpressionSummaries, valsum would contain all of those daily values for each 1d interval. I actually verified that valsum count is 7 - but when I loop through "For Each ptValue in valsum", and then pull the value with ptValue.Value, first I get a 0, and then I get a type mismatch error the second time through the loop.


If I use TimeWeighted, I get 7 good values, but they're all decimal values less than zero like 0.6434223, 0.3452232, etc --  not integer counts as you'd expect.


It seems like it should be simple enough to count the number of times a certain tag equals a certain value every day, but I just can't get this to work.


Anyone have any similar IPICalculation code you can share that does soemthing like this?