Getting result of if statements in Expressions

I'm using ignition 8.1.43

I have an expression tag that I have several nested if statements working together to give me a numerical response. I've hit a point where I need to take the numerical response of those if statements and put it into another if statement. to either divide by 1,000 or 10,000

I certainly could put this at the top of my nest if statements, but then I would have all that logic repeated twice for each branch of the if statement. Any time I changed one line I would have to go to the opposing part of the if statement and made sure I changed it there as well.

Is there anyway to put an if statement AFTER a completed statement in an expression tag and have it reference the value of the first statement? Here's an example of what I mean:

If (1=1
	, 5000
	, 6000
	)
If (theSky=Blue
	, {returnedValue}/1000
	, {returnedValue}/10000
)

So that second if statement is using the output of the first one. Is that possible?

No, not directly.
The simplest solution is to use an intermediate expression tag/custom property/whatever, to hold the first expression.

1 Like

Thanks, I'll do that then!

Yes, with the Integration Toolkit module's transform() & value() functions.

Your scenario is precisely the kind of task those exist to solve.

Based on @PGriffith's solution to a similar problem this week (which he's forgotten already), https://forum.inductiveautomation.com/t/how-to-transform-some-tags-into-1-tag-easily/98195/8:

case (
    stringFormat("%d.%d", 1 = 1, theSky = "Blue"), 
    "0.0", 5000,
    "1.0", 6000,
    "0.1", {returnedValue}/1000,
    "1.1", {returnedValue}/10000,
    null
)

I haven't tested it.

1 Like

Might want to look closer.

1 Like

Eh. This does, technically, work, but I would argue obscures the logic and what your actual intent is much more than an intermediate tag/custom property/whatever with a useful name.

Phil's solution in the integration toolkit is solid too, but I'd (personally) favor a name based approach if we ever did anything like this first party, as in some kind of variable binding syntax, but the exact details of that are very fuzzy and demand is pretty low.

1 Like

I have a number of thoughts swirling.....

.....but they'll have to wait for after my v8.3 module updates.