> For the complete documentation index, see [llms.txt](https://easyedge.gitbook.io/easyedge/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://easyedge.gitbook.io/easyedge/reference/expressions-operations-engine/practical-use-cases.md).

# Practical Use Cases

This final section brings together all the concepts we have discussed by demonstrating how to solve real-world industrial scenarios with a single Computation String. These examples showcase the symbiotic relationship between the high-performance mathematical engine and the flexible dynamic functions, allowing for the creation of powerful, single-line automation logic.

***

## Generating a Conditional Alarm Message

This use case demonstrates the correct pattern for conditional logic that results in a string output. The mathematical if() function (or a direct relational operator) is used to produce a numeric result (1.0 for true, 0.0 for false), which then drives a $switch function to select the appropriate string.

**Scenario**: A pressure sensor provides its reading in a JSON payload. If the pressure exceeds a dynamic threshold, a formatted alarm message needs to be generated for a monitoring system.

**Goal**: Create a string like "ALARM: Pressure at 125.5 PSI exceeds threshold of 120.0 PSI", but only if the condition is met. Otherwise, the output should be "OK".

**Operands**:

* a: JSON payload, e.g., {"id":"P-101", "reading":125.5}
* b: Numeric threshold, e.g., 120.0

**Computation String**: $switch($parse('json', a, '$.reading') > b, 1, $format('ALARM: Pressure at %.1f PSI exceeds threshold of %.1f PSI', $parse('json', a, '$.reading'), b), 'OK')

**Breakdown**:

1. **Innermost (Function)**: $parse('json', a, '$.reading') extracts the numeric value 125.5.
2. **Middle (Math)**: The relational expression 125.5 > 120.0 is evaluated by the numeric engine, which returns 1.0 (true).
3. **Outermost (Function)**: The $switch function receives 1.0 as its input. It matches the case 1 and proceeds to evaluate the corresponding result argument.
4. **Nested Evaluation**: The $format function is executed, producing the final alarm string.
5. **Final Result**: The string 'ALARM: Pressure at 125.5 PSI exceeds threshold of 120.0 PSI'. If the pressure had been 110.0, the relational operator would have returned 0.0, and the $switch would have returned the default value, 'OK'.

***

## Creating a Dynamic MQTT Routing Topic

This example showcases how multiple $functions can be nested to build complex, data-driven strings, perfect for tasks like creating dynamic topics for MQTT messages or generating structured log entries.

Scenario: Data from various devices needs to be published to an MQTT broker. The topic structure should be \[base\_topic]/\[device\_type]/\[priority]/\[device\_id].

**Goal**: Dynamically construct the full MQTT topic from a single JSON payload.

**Operands**:

* a: JSON payload, e.g., {"id":"T-101", "priority": 1, "type":"sensor"}

**Computation String**: $concat('events/', $parse('json', a, '$.type'), '/', $switch($parse('json', a, '$.priority'), 1, 'high\_priority', 'low\_priority'), '/', $parse('json', a, '$.id'))

**Breakdown**:

1. The $concat function requires all its arguments to be resolved first.
2. **Arg 1**: 'events/' is a literal.
3. **Arg 2**: $parse('json', a, '$.type') is evaluated, returning 'sensor'.
4. **Arg 3**: '/' is a literal.
5. **Arg 4**: A nested $switch is evaluated.

* Its input, $parse('json', a, '$.priority'), returns the number 1.
* The $switch matches the case 1 and returns the string 'high\_priority'.

6. **Arg 5**: '/' is a literal.
7. **Arg 6**: $parse('json', a, '$.id') is evaluated, returning 'T-101'.
8. **Final Assembly**: $concat combines the resolved arguments.
9. **Final Result**: The string 'events/sensor/high\_priority/T-101'.

***

## Decoding and Checking a Device Status Register

This use case combines hexadecimal conversion with bitwise logic inside a conditional $switch to interpret low-level device data.

**Scenario**: A PLC provides a status register as a single hexadecimal string. You need to check if a specific error bit (e.g., the 4th bit, which has a value of 8 or 0x08) is active.

**Goal**: Return a human-readable status string based on the state of a single bit.

**Operands**:

* a: The status register as a hex string, e.g., 'A9' (which is 10101001 in binary).

**Computation String**: $switch(band($fromHex(a), 8) > 0, 1, 'Overload Error', 'OK')

**Breakdown**:

1. **Innermost ($function)**: $fromHex(a) converts the string 'A9' to the decimal number 169.
2. **Middle (Math)**: band(169, 8) is executed. The bitwise AND of 10101001 and 00001000 is 00001000, which is the decimal number 8.
3. **Next Level (Math)**: The relational expression 8 > 0 is evaluated, returning 1.0 (true).
4. **Outermost ($function)**: The $switch function receives 1.0 as its input, matches the case 1, and returns the corresponding result.
5. **Final Result**: The string 'Overload Error'.

***

## Extracting, Scaling, and Formatting a Value from a Composite String

This example demonstrates a complete data processing pipeline in one line: it finds a value in a complex string, extracts it, performs a unit conversion, and formats it for display.

**Scenario**: A legacy device sends a single string containing multiple key-value pairs, like "ID=MOT-03;SPEED=1825.5;TEMP=75.2". You need to extract the speed (in RPM), convert it to Hz, and create a formatted output string.

**Goal**: Extract "1825.5", calculate 1825.5 / 60, round it to two decimal places, and present it in a descriptive string.

**Operands**:

* a: The composite string from the device.

**Computation String**: $format('Motor speed is %.2f Hz', roundn($split($regex(a, 'SPEED=(\[0-9.]+)', 1), ';', 0) / 60, 2))

**Breakdown (Inside-Out)**:

1. $regex(a, 'SPEED=(\[0-9.]+)', 1) finds the pattern and extracts the first capture group, returning the string '1825.5'.
2. $split('1825.5', ';', 0) is technically redundant here but ensures that if there were trailing characters, they would be ignored. It returns '1825.5'.
3. The engine implicitly converts this string to a number and performs the division: 1825.5 / 60, resulting in 30.425.
4. roundn(30.425, 2) rounds the result to two decimal places, returning 30.43.
5. $format('Motor speed is %.2f Hz', 30.43) produces the final output.
6. Final Result: The string 'Motor speed is 30.43 Hz'.
