The Lua AND function returns unexpected results


There are two variables:

- When using AND:

DEV700.BO1=(INDIT and koll1_p) --prim sziv1

The result is DEV700.BO1=1

- When using *:

DEV700.BO1=(INDIT * koll1_p) --prim sziv1

The result is correct DEV700.BO1=0

Product Line

SmartStruxure Lite


SmartStruxure Lite MPM


The simple issue here is that the and keyword is a logical operator, however the variables being compared are numeric.

Contrary to many other languages, a Lua variable is not typed, which means that the kind of value it stores are not constrained and can actually change during a program execution:

From the Lua Scripting Language overview

Boolean Designates a variable which can hold the true or false values. It is important to note that true or false are not numbers and do not equal 1 or 0.

That is why the mathematical multiplier * keyword works in this instance but the logical and does not.


The logical operators are and, or, and not. Like control structures, all logical operators consider false and nil as false and anything else as true. The operator and returns its first argument if it is false; otherwise, it returns its second argument. The operator or returns its first argument if it is not false; otherwise, it returns its second argument:

print(4 and 5)         --> 5
    print(nil and 13)      --> nil
    print(false and 13)    --> false
    print(4 or 5)          --> 4
    print(false or 5)      --> 5

Both and and or use short-cut evaluation, that is, they evaluate their second operand only when necessary.

Essentially to use the and operator with numeric values they will first need to be evaluated into true or false states. For example:

If INDIT ~=0 then x= true else x= false end
If koll1_p ~=0 then y= true else y= false end

DEV700.BO1=(x and y)