TIA Portal Instructions

Comparator Operations
Decision Logic & Ranges

Value comparison is the engine of industrial decision-making. Master standard comparators, range tests, and validity checks in TIA Portal.

What are Comparator Operations in TIA Portal?

Comparator operations allow you to compare two values of the same data type (Integers, Reals, Time, etc.) or check if a value lies within or outside a defined range. They are fundamental for creating alarm thresholds, transition conditions, and regulation loops.

TIA Portal offers standard comparators (==, <>, >=, etc.), range instructions (IN_Range, OUT_Range), validity tests for floating-point numbers, and advanced instructions for Variant data types and DB pointers.

Standard Comparators

Evaluating equality and inequalities between two operands

CMP == — Equal

==

What it does

Compares two operands (IN1 and IN2). If IN1 is equal to IN2, the instruction returns TRUE. It supports almost all simple data types.

When to use it

Check if a counter has reached its target, if an HMI setpoint matches the current value, or if a specific machine state is active.

Pro Tips

Be careful with Real types: strict equality may fail due to precision. Prefer a margin or range.

In SCL: IF value1 = value2 THEN ...

You can compare Strings to verify identification codes.

CMP <> — Not equal

<>

What it does

Returns TRUE if both operands IN1 and IN2 are different. It is the inverse of equality.

When to use it

Detect a change in value, ensure a sensor is not returning 0, or verify that an operating mode is not the forbidden one.

Pro Tips

Often used to trigger an action as soon as a value deviates from its setpoint.

In SCL: IF value1 <> value2 THEN ...

Can be used to compare timestamps (DTL) to see if a date has changed.

CMP >= — Greater than or equal

>=

What it does

Returns TRUE if the first value (IN1) is greater than or equal to the second (IN2).

When to use it

Trigger a high-level alarm, allow heating as long as a temperature is not reached.

Pro Tips

Ideal for safety thresholds where the limit value must be included in the condition.

In SCL: IF level >= setpoint THEN ...

Can be used with Time types to check if a minimum duration has elapsed.

CMP <= — Less than or equal

<=

What it does

Returns TRUE if IN1 is less than or equal to IN2.

When to use it

Low-level monitoring, stopping a pump, validating a minimum pressure.

Pro Tips

Often coupled with hysteresis to avoid output chattering.

Works natively with Date and Time types.

In SCL: IF pressure <= min_limit THEN ...

CMP > — Greater than

>

What it does

Returns TRUE if IN1 is strictly greater than IN2.

When to use it

Strict comparison that should not include the target value.

Pro Tips

Less used than >= in automation, but useful for mathematical algorithms.

In SCL: IF current > max THEN ...

Preferable for loop index comparisons (FOR).

CMP < — Less than

<

What it does

Returns TRUE if IN1 is strictly less than IN2.

When to use it

Strict lower bound tests.

Pro Tips

Useful for checking if a value is 'in the negative' (val < 0).

In SCL: IF val < 0 THEN ...

Works with characters (alphabetical sorting).

Range Operations

Checking if a value belongs to an interval [MIN..MAX]

IN_Range — Within range

IN_Range

What it does

Checks if a value (VAL) is between a lower bound (MIN) and an upper bound (MAX). The interval is closed: MIN <= VAL <= MAX.

When to use it

Operating window monitoring (e.g., pressure between 2 and 5 bars), machine speed validation.

Pro Tips

Replaces two serial comparators, making the network more readable.

If MIN > MAX, the instruction always returns FALSE.

Very useful for 'Ready to Run' interlocks requiring multiple analog conditions.

OUT_Range — Outside range

OUT_Range

What it does

Returns TRUE if the value (VAL) is outside the interval defined by MIN and MAX. Either VAL < MIN or VAL > MAX.

When to use it

Anomaly detection or exceeding extreme thresholds (range alarm).

Pro Tips

It is the exact inverse of IN_Range.

Use it to simplify process alarm logic.

In SCL: res := (val < min) OR (val > max);

Validity Tests

Checking the integrity of floating-point numbers

|OK| — Check validity

|OK|

What it does

Checks if a floating-point value (Real or LReal) is a valid number. Returns FALSE if the value is NaN (Not a Number) or Inf (Infinity).

When to use it

After complex calculations (divisions, square roots, logs) to avoid propagating mathematical errors that could stop the PLC.

Pro Tips

Essential before sending a Real value to an analog output or a drive.

Allows detecting indirect division by zero.

Handy for filtering feedback from failing sensors.

|NOT_OK| — Check invalidity

|NOT_OK|

What it does

Returns TRUE if the floating-point value is invalid (NaN or Inf).

When to use it

Trigger a calculation fault bit or force a fallback value.

Pro Tips

Use it to put your machine into safe state if a trajectory calculation fails.

Equivalent to IS_NAN in other languages.

In SCL: IF NOT OK(value) THEN ...

Advanced Comparisons (Variant)

Instructions for dynamic programming and complex types

EQ_Type / NE_Type

What it does

Compares the data type of a Variant variable with that of another variable or a defined type.

When to use it

In generic blocks that must process different data types (e.g., a logging block accepting Int or Real).

Pro Tips

Essential for object-oriented programming and reusable libraries.

Allows securing dynamic memory access.

Used with the VariantGet instruction.

IS_NULL / NOT_NULL

What it does

Checks if a pointer (Variant or DB_ANY) is null (points to nothing).

When to use it

Avoid access errors when using indirectly addressed DBs.

Pro Tips

Always test NOT_NULL before calling a method or reading data via a pointer.

Equivalent to 'Nothing' or 'null' object in C#/Java.

Indispensable for managing linked lists or dynamic structures in PLC.

IS_ARRAY

What it does

Checks if a Variant variable points to an Array.

When to use it

Programming batch processing functions or data buffers.

Pro Tips

Couple it with CountOfElements to know the array size.

Allows creating universal sorting functions.

Also works for arrays of structures (UDT).

Comparator Comparison — Which one to use?

Choose the right instruction for your PLC logic

You want to...Use thisExample
Test exact equalityCMP ==Product code correct
High safety thresholdCMP >=Pressure > 10 bar
Maintain in a zoneIN_RangeTemp between 18 and 24°C
Monitor Real errors|OK|Check division result
Check DB pointerNOT_NULLRecipe DB loaded
Check Variant typeEQ_TypeInput is indeed Int

Frequently Asked Questions

Why does my == comparison sometimes fail with Real numbers?

Real numbers (floating-point) have limited precision. A calculation might result in 10.000001 instead of 10.0. Always use IN_Range with a small tolerance (e.g., between 9.99 and 10.01) to compare floating-point values.

What is the difference between IN_Range and two comparators in series?

Functionally, it's the same. However, IN_Range is graphically more compact, easier to read, and more performant as the PLC processes the instruction in a single step.

What are |OK| and |NOT_OK| instructions for?

They are used to detect 'silent' errors in Real calculations. If you take the square root of a negative number, the result is NaN. Without the |OK| test, this NaN will pollute all your other calculations and could cause a CPU stop.

Can we compare complete structures (UDT)?

The CMP == instruction does not allow directly comparing two structures. You must compare each member individually or use a specific block that compares memory areas.

Generate your comparison logic with AI

Describe your thresholds and conditions in plain language, T-IA Connect will generate optimized comparison networks with error handling.