Creating a Counter Expression Tag

Hi,
I Have an Expression Tag which is an IF condition

IF(
    {[.]B01} >= 0.1 || {[.]B02} >= 0.1 || {[.]B03} >= 0.1 || {[.]B04} >= 0.1 || {[.]S1B05} >= 0.1,
    IF(COALESCE({[.]RNT}, 0) < 1170, 1170, COALESCE({[.]RNT}, 0) + 1),
    COALESCE({[.]RNT}, 0)
) + (0 * NOW(1000))

In this Expression I am Checking the Condition if it is true and the tag value is less than 1170 which is an offset value the counter will start from 1170 then 1171 and so on which is working but the quality of the tag is showing Error Expression Eval but different instance of these tag with same expression it's not showing the quality error so not sure what I am doing wrong.
Basically, I am trying to create a counter tag which will keep incrementing by 1 while condition is true and while false the counter will stop counting and resume counting again when the condition becomes true from where it is stopped.
appreciate any help on this

In the instance that has the expression eval error, what is the error the eval is actually getting? You may be able to get this by going to the Tag Diagnostics.

You might also provide at least a screenshot of thee UDT you are using with it's tags for the bad instance.

Trying to count within a single expression tag is doomed. There must be a circular reference to the tag's value, and that has race conditions even when using coalesce(). Use a separate expression tag to yield the count condition, and have script on that increment a memory tag.

Hi @Benjamin_Furlani , @pturmel
Attaching the screen shot of the error I am getting


In the above image the tag is working according to the Expression, but quality is Error_ExpressionEval

Same expression I have added in another tag it is giving me Good Quality
@pturmel
I have written condition in another tag that is S1STAT giving Boolean value on which I am running the expression in another tag, I have tried writing tag value change script on that count condition tag to increment the value, but it only increments once on value change, but it does not keep incrementing the value, if possible, can you share some examples

Is this just incrementing once per second?

Yes

So, not a counter, but a timer?

If so, use a third tag, memory tag of type datetime, to hold a timestamp when the boolean condition goes true. (Written from the condition tag's valueChange event.)

Then the expression tag becomes:

if(
	{[.]ConditionTag},
	secondsBetween(
		{[.]TimestampTag},
		now(1000)
	),
	0
)

The main thing I see here is that you are trying to write a value back to itself. Expressions don't really work that way.

Here is a timer udt that you can use / whack / hack to your heart's content that may help you out.

2 Likes