I have 160 tags I need to add up to get a total sum. I only want the sum of the individual tags if its "true" though. So any tag that is not true I would not want its value added into the total sum.
I'm new with expressions and could use some help if anyone knows how to write this out.
Are all these tags boolean and you want to count the number of true
values?
Yes they are boolean. Sorry I forgot to say if the boolean is true then I need to add the value of another tag. And so on 160 times.
If boolean is not true then dont add that sum into the final number.
This is hard for me to explain. Sorry
Try this:
{[default]Bool0} * {[default]Other0} +
{[default]Bool1} * {[default]Other1} +
{[default]Bool2} * {[default]Other2} +
... +
{[default]Bool159} * {[default]Other159}
This is a clever solution if this must be an expression... not great performance implications though.
@thomas_wallace are you sure this has to be an expression and not a script?
It could be script. I know little about expressions and even less about scripts.
The gateway events section of your project is where to start if you want to run scripts.
Let's say for instance you wanted this to update every minute. You could make a scheduled script with these settings:
On the script side, the pseudo-code steps would be:
# Read all the booleans and value tags
# Iterate through each pair of tags
# If the boolean of the pair is true, add the value
# If not true, ignore / don't even write a clause for this
# write the value of the result to a memory tag to hold your results.
If you need the actual lines of python... matt.eaton@alphastationautomation.com lol
Line 1 by itself works. When I added line 2 it doesnt work. Line 1 boolean is true. Line 2 boolean is false. Was using line 2 as a test.
Here's my pseudocode dump:
tagPaths = [] # build this somehow, first all the boolean tag paths, then all the value tag paths
values = [qv.value for qv in system.tag.readBlocking(tagsPaths)]
z = zip(values[0:159], values[160:319])
total = sum(map(lambda t: t[1], filter(z, lambda t: t[0])))
zip with slicing, you rockstar! I'm stealing that.
Thomas, hover over the Error_ExpressionEval and report what it says.
In general you should be posting code, not pictures of code (unless the context is relevant). See Wiki - how to post code on this forum.
Ok thanks. Was wondering how people posted code.
Error says "Expression right operand is null"
Right, so use the tag browser to examine the tags and figure out why you're getting a null.
@Kevin.Herron So add all my tags to the top line of your script then the rest is exactly how you have it?
More or less... I typed this out in a text editor and didn't actually try to run it.
If you type it out by hand the tag paths should look something like:
tagPaths = ["[default]Foo", "[default]Bar", ...]
but you should be able to build it programmatically if all your tag paths are predictable.
You also would need to write this script and run it somewhere else, not where you're currently defining the expression. Something like @matt.eaton suggested. It depends on how often you need this summation to happen.
Can you share your tag paths?
@pturmel
{[default]779/CabTrimAGVs/AGVProgram/AGV01/B2} * {[default]779/CabTrimAGVs/AGVStatus/AGV01/R5}
The AGV01 changes by one until it reaches 159. So AGV02, AGV03,.....so on for each tag
No AGV00?
no, AGV01-AGV159
AGV159 or AGV0159? Two digits until 99, then three? Or always a leading zero?