Hi guys, is there any way to create a expression tag to count up with a rate of 1 second based on a boolean value or tag?

I want to record how long the tag was true, then keep that value and it will increment again when the tag is true again.


[~]booleanTag.value = path a boolean tag that determines when to count
[~]count.value = path to itself
And add in a fixed trigger rate (of how ever often you wish to count)

(the isGood parts are because the initial value would be null instead of 0)

1 Like

I am a little confused. So if your tag turns true, you want this other tag to start counting up 1, 2, 3, … seconds etc until it is false? And when it turns true again, you start counting up 1, 2, 3, … etc seconds again? Is that correct?

He said

So i assume it will continue counting, which my code does

(4s interval)
True: 1,2,3,4
False: 4,4,4,4
True: 5,6,7,8

1 Like

If that’s what he wants then yes I would go with your solution.

If he wants it to reset each time it goes to True, I would make an expression tag that checks if the boolean is true, and if so then does data arithmetic on the timestamp property of the boolean tag.

If this is something important and you want to keep track of for analysis reasons, you might also consider using historian. Then you can see exactly when things turn on/off and for how long, at what time of day, etc. You could use On Change sampling method since its a boolean.

Yeah, the way i made it it is impossible to reset by writing to it, as an expression is readonly…
I suppose if he wants that, he would need to add in an extra if that triggers it to turn 0 when an other tag is true… Im not sure this is great practice tbh

Altho maybe a refresh binding resets it, idk if you can trigger that on a tag?

1 Like

Agreed I probably would not do this outside of a development/a diagnostic.

If this is data the customer wants to know, I would most likely be using historian for this. Or at least a tag change event that records the boolean tag change events to a database that you can run analytical queries on after the fact. But probably historian.

1 Like

thanks brother appreciated.