# Creating a Counter Expression Tag

**URL:** <https://forum.inductiveautomation.com/t/creating-a-counter-expression-tag/98382>\
**Category:** Ignition\
**Created:** [November 8, 2024, 9:39am UTC](https://forum.inductiveautomation.com/t/creating-a-counter-expression-tag/98382 "2024-11-08T09:39:57Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ganesh\_Shinde2](https://avatars.discourse-cdn.com/v4/letter/g/dc4da7/32.png) [@Ganesh\_Shinde2](https://forum.inductiveautomation.com/u/Ganesh_Shinde2)\
**Post date:** [November 8, 2024, 9:39am UTC](https://forum.inductiveautomation.com/t/creating-a-counter-expression-tag/98382/1 "2024-11-08T09:39:57Z")

</div>

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

```python
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

---

<div class="post-metadata">

**Author:** ![Benjamin\_Furlani](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/benjamin_furlani/32/66084_2.png) [@Benjamin\_Furlani](https://forum.inductiveautomation.com/u/Benjamin_Furlani)\
**Post date:** [November 8, 2024, 2:40pm UTC](https://forum.inductiveautomation.com/t/creating-a-counter-expression-tag/98382/2 "2024-11-08T14:40:16Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [November 8, 2024, 3:12pm UTC](https://forum.inductiveautomation.com/t/creating-a-counter-expression-tag/98382/3 "2024-11-08T15:12:41Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Ganesh\_Shinde2](https://avatars.discourse-cdn.com/v4/letter/g/dc4da7/32.png) [@Ganesh\_Shinde2](https://forum.inductiveautomation.com/u/Ganesh_Shinde2)\
**Post date:** [November 13, 2024, 10:55am UTC](https://forum.inductiveautomation.com/t/creating-a-counter-expression-tag/98382/4 "2024-11-13T10:55:36Z")

</div>

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

 ![Screenshot 2024-11-13 161317](https://us1.discourse-cdn.com/inductiveautomation/original/3X/7/a/7a0a1d6a828ef61a53e8da520ef133be49b6d4d8.png)  
In the above image the tag is working according to the Expression, but quality is Error\_ExpressionEval  
 ![Screenshot 2024-11-13 161405](https://us1.discourse-cdn.com/inductiveautomation/original/3X/b/f/bf0aa34effb6466e48d89bedba2345a67850a7ae.png)  
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

---

<div class="post-metadata">

**Author:** ![JordanCClark](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jordancclark/32/83362_2.png) [@JordanCClark](https://forum.inductiveautomation.com/u/JordanCClark)\
**Post date:** [November 13, 2024, 12:48pm UTC](https://forum.inductiveautomation.com/t/creating-a-counter-expression-tag/98382/5 "2024-11-13T12:48:09Z")

</div>

Is this just incrementing once per second?

---

<div class="post-metadata">

**Author:** ![Ganesh\_Shinde2](https://avatars.discourse-cdn.com/v4/letter/g/dc4da7/32.png) [@Ganesh\_Shinde2](https://forum.inductiveautomation.com/u/Ganesh_Shinde2)\
**Post date:** [November 13, 2024, 12:51pm UTC](https://forum.inductiveautomation.com/t/creating-a-counter-expression-tag/98382/6 "2024-11-13T12:51:08Z")

</div>

Yes

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [November 13, 2024, 12:53pm UTC](https://forum.inductiveautomation.com/t/creating-a-counter-expression-tag/98382/7 "2024-11-13T12:53:40Z")

</div>

> [@Ganesh\_Shinde2](#):
>
> but it does not keep incrementing the value

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:

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

```

---

<div class="post-metadata">

**Author:** ![JordanCClark](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jordancclark/32/83362_2.png) [@JordanCClark](https://forum.inductiveautomation.com/u/JordanCClark)\
**Post date:** [November 13, 2024, 12:55pm UTC](https://forum.inductiveautomation.com/t/creating-a-counter-expression-tag/98382/8 "2024-11-13T12:55:07Z")

</div>

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.

> [@Timer ON and timer OFF](https://forum.inductiveautomation.com/t/timer-on-and-timer-off/55156/13):
>
> UDT attached. Monitor has two Diagnostic level alarms for TON and TOF operation. Expression tags bound to each alarm isActive flag. Integer tags for TON and TOF time values. limited to even seconds [timer\_udt.json](https://forum.inductiveautomation.com/uploads/short-url/iZa3PJfppuHE6IfYY2FYBxMIlKz.json) (1.2 KB)

---

<div class="post-metadata">

**Author:** ![Ganesh\_Shinde2](https://avatars.discourse-cdn.com/v4/letter/g/dc4da7/32.png) [@Ganesh\_Shinde2](https://forum.inductiveautomation.com/u/Ganesh_Shinde2)\
**Post date:** [November 27, 2024, 6:49am UTC](https://forum.inductiveautomation.com/t/creating-a-counter-expression-tag/98382/9 "2024-11-27T06:49:09Z")

</div>

@pturmel @JordanCClark Thanks for the inputs I have created a UDT having timestamps and using seconds between to get runtime
