Functions CASE() or SWITCH() Not Working

When I try to run in script console the sample that is showed in the web site https://docs.inductiveautomation.com/display/DOC80/case

case(
1, #// value
0, #// case 1
“Off”, #// return 1
1, #// case 2
“Running”, #// return 2
2, #// case 3
“Fault”, #// return 3
forceQuality("!BAD STATE!",0)) #// default

I got this error
Traceback (most recent call last):
File “”, line 1, in
NameError: name ‘case’ is not defined

These are expression language functions, you can’t use them in scripting.

There is another function like case that I can use in scripting?

The “pythonic” way to do that is define a dictionary as if a constant (outside your script function if in a script module), with the lookup values as keys and the results as the corresponding content. Then you just return myCasesDictionary[myKeyToLookUp].

If the cases are more dynamic than that, use python’s if...elif...elif...else construct.

1 Like

Cay you elaborate ‘expression language functions’ ?

See Expression Functions in manual–there’s also a link near the top of that page to read more about expressions–vs. Scripting.