Conditional statements inside an expression switch statement

Hi. I’m having some issues with an expression that I am writing for an expression tag within a UDT. I want the expression to run differently depending upon a property (deviceType) that is part of the UDT. I can’t seem to find why it won’t work. I keep getting an evaluation error on the tag. Any help is appreciated.
Here is the code:

{deviceType}, // value (string)
'HMI', //case 1
'PLC', //case 2
'Switch', //case 3
'SwitchCisco', //case 4
({[.]Ping_Status} = '1'), //return for HMI
({[.]OPC_Status} & ( {[.]Ping_Status} = '1')), //return for PLC
(({[.]OPC_Status} != 1) & ( {[.]Ping_Status} = '1')), //return for Switch
({[.]Ping_Status} = '1'), //return for SwitchCisco
False) //default

The Ping Status tag is a query tag within the UDT and the OPC Status tag is an OPC tag within the UDT

I think you’ll need {deviceType} in quotes, as it is a string that is evaluated at tag creation, not when the expression is run. You should know that this approach will evaluate all four subexpressions regardless of device type, then discard three of them. If any non-applicable expression fails, the whole expression will fail.
Consider creating a “base” UDT and inherit it in four dependent UDTs, one for each type. The base UDT would leave this item out, so the dependent UDTs can define it differently.

1 Like

Can you provide an example?