22 Replies Latest reply on Jun 22, 2018 2:18 AM by Dan Fishman

# Count a digital tag in AF Analysis - What is the best solution

I have a digital tag that I want to analyze each day: How many times has the tag been "1" last 24h?

I have searched different topics and tried both expressions and rollup in analysis, but haven't got a good solution yet.

I is also important for me to be able to backfill a tag with the results from this analysis.

Any good suggestions?

Thanks!

-A-

• ###### Re: Count a digital tag in AF Analysis - What is the best solution

You will need an analysis to translate the digital state into a number and then multiple the average by the event count to obtain the total numbers of events within the time period.  KIB01120 has this exact situation worked out in solution number 4!  Note, in step 2 of example 4, the output is a PI Point Data Reference and that actually is not required.  You could use an analysis that does not write history since outputting the state as a 0 or 1 probably isn't that useful.

2 of 2 people found this helpful
• ###### Re: Count a digital tag in AF Analysis - What is the best solution

Thanks for the answer.

What if you want to count a tag based on events? For example: If value 1 = 1, value 2 increases with +1.

Any good ideas on how to do this?

-A-

• ###### Re: Count a digital tag in AF Analysis - What is the best solution

You could use logic similar to the below to implement a counter each time the value is equal to 1.  Note,the below will only work for event triggered analysis.

if 'attribute' =1 and PrevVal('attribute','*') <> 1  then 'counter' +1 else nooutput()

• ###### Re: Count a digital tag in AF Analysis - What is the best solution

Hi,

I have tested with this but I don't get it to work. I have tried with a output attribute, but no change in value. It doesn't count.

Any tips?

-A-

• ###### Re: Count a digital tag in AF Analysis - What is the best solution

If it didn't count is probably is not picking up your trigger.

Try something simple and use preview results instead of actually running it.

For example try: if TagVal('Attribute1','*') = "1" then 1 else 0

It might be you need the string "1" instead of 1.

Dan

• ###### Re: Count a digital tag in AF Analysis - What is the best solution

I have tried that before, and it is not a string, it is a digital value.

I think the problem may be the fact that the counter 'tag'+1 uses the same 'tag' as output attribute of the analysis?

-A-

• ###### Re: Count a digital tag in AF Analysis - What is the best solution

When you say a value of 1, do you mean an actual value of 1 or a digital state offset of 1?

• ###### Re: Count a digital tag in AF Analysis - What is the best solution

I mean the actual value of 1:

The analysis looks like this:

Don't know if this helps?

• ###### Re: Count a digital tag in AF Analysis - What is the best solution

It looks like it should be working if your data transitions from a 0 to a 1!  Can you include a screenshot of from preforming previewing the results within PI System Explorer analysis tab?

• ###### Re: Count a digital tag in AF Analysis - What is the best solution

Is your scheduling set to "event triggered"?

• ###### Re: Count a digital tag in AF Analysis - What is the best solution

Try setting the Hylser_dogn_test to an initial value of zero.  I wonder if the value is point created and yo are trying to add 1 to a system digital tag.  Also, I would explicit set the event-triggered to PLC:Slave2..

2 of 2 people found this helpful
• ###### Re: Count a digital tag in AF Analysis - What is the best solution

Hi again!

Did just now log in to set the initial value to zero, and can now see that it has been counting since last I was online in AF!

Now idea why, but it it correct now!

Thanks for all help

-A-

• ###### Re: Count a digital tag in AF Analysis - What is the best solution

Great!!  I hope next time we can solve the problem faster!  I'm glad it is all working!

• ###### Re: Count a digital tag in AF Analysis - What is the best solution

Next issue (in same problem):

Now the counter works, and I would like to reset it every midnight to 0.

I cannot seem to use analysis to perform this because I cannot have two analysis writing to the same output attribute, this is not possible.

I have also tried to make a new analysis that becomes 1 every midnight, then to use this in my code. But i am not able to select it as an trigger, why? See picture.

• ###### Re: Count a digital tag in AF Analysis - What is the best solution

There are several ways to do this.  From what I gather, analysis is probably trying to prevent you from making a circular reference.

1) One way around having two analysis writing to the same output attribute is to have it write to a second attribute.  The service should not see any dependency between the analysis.  This second attribute will reference the same PI Point.  This issue is any sort of dependency relationship is broken.

2) A second way, is to introduce a tag that writes a 0 or a 1.  A second analysis could get the EventCount from just after midnight to the current time and have logic to reset at midnight.  Again, not a strong dependency relationship, although it might not matter.

3) A third method, is to just run one analysis and include logic to reset at midnight.  This would have to be periodic and have to sample at least as fast as the sample rate.  Although it runs often, it would be intensive since the previous value is held in memory by the analysis service.  You might have to schedule as often as every second. A lot of this depends on how accurate you need the timestamp of the change to be!  This also requires just one tag.

• ###### Re: Count a digital tag in AF Analysis - What is the best solution

The way I tried, is what you describe in number 2.

But I don't get to choose the 'Resethylser' attribute as an trigger on the original analysis. It is just an AF attribute, but maybe it will work if i make this a Pi point, but in that case: Why?

-A-

• ###### Re: Count a digital tag in AF Analysis - What is the best solution

If I have time I can try to work out an example.  I am guessing Resethylser is an attribute that does not have a data reference.  Not all attribute configurations support what is called a "data pipe".  The attribute must support a "data pipe" to allow it to be a trigger.

• ###### Re: Count a digital tag in AF Analysis - What is the best solution

Hi Arve - The solution that Dan Fishman suggested should work in your case. I should mention that with upcoming PI Server 2018 release, you should be able to leverage native support for mapping and filtering arrays. For example, the following example shows how to count the number of times `cdm158` tag has been equal to "Manual" or "Auto" in past 1 day.

Thanks,

Nitin

3 of 3 people found this helpful
• ###### Re: Count a digital tag in AF Analysis - What is the best solution

That would be great.  Would only have to detect the change in state though and not the number of values.  Are there array functions that can do that sort of compare?

• ###### Re: Count a digital tag in AF Analysis - What is the best solution

Dan - Somehow I missed replying to your comment. I believe you are asking for something like this: For e.g. How many times did the attribute 'cdm158' change from Auto to Cascade in the past day?

With the upcoming 2018 release, you can do this as follows:

So if you looked at `values` and `filteredValues`, this is what you'll see:

Basically the second argument of FilterData function allows for any PE expression that evaluates to true or false. You can use the "placeholder" \$val to access the current value in the sequence and apply any PE functions on that \$val.

Hope this helps.

-Nitin

1 of 1 people found this helpful
• ###### Re: Count a digital tag in AF Analysis - What is the best solution

Nitin,

Really great example!  That is really neat!  Thanks for providing that!