Skip to content

AS Functional Keyword

Feature Since Version Notes
AS >v1.4.4 Data type conversion
AS STRING, AS INT, AS DOUBLE ??? Type casting

Overview

  • Description: Used to explicitly convert a value to a specific data type. This is often necessary when performing operations that require a certain type or when publishing data in a specific format.

Signature

  • Syntax:
    <value> AS <DataType>
    

Parameters

  • <value>: The value or expression to be converted.
  • <DataType>: The target data type. Common types include:
    • STRING
    • INT
    • DOUBLE
    • BOOL (Conversion rules apply, e.g., non-empty strings/non-zero numbers might be true)

Usage Examples

Basic Example: String to Integer

Convert a payload received as a string to an integer for calculation.

DEFINE ACTION CalculateWithConversion
ON TOPIC "input/numeric_string" DO
    // Payload might be "25"
    SET "intValue" = PAYLOAD AS INT
    PUBLISH TOPIC "output/calculated" WITH ({intValue} + 10)

Intermediate Example: Publishing as String

Ensure a calculated numeric value is published as a string.

DEFINE ACTION PublishStringValue
ON TOPIC "input/calculate" DO
    SET "result" = (PAYLOAD * 5)
    PUBLISH TOPIC "output/string_result" WITH ({result} AS STRING)

Advanced Example: Boolean Conversion in Condition

Explicitly convert a topic value (e.g., "1", "0", "true", "false") to boolean for an IF condition.

DEFINE ACTION CheckBooleanString
ON TOPIC "config/enable_feature" DO
    // Payload could be "true", "1", "false", "0"
    IF (PAYLOAD AS BOOL) THEN
        PUBLISH TOPIC "feature/status" WITH "Enabled"
    ELSE
        PUBLISH TOPIC "feature/status" WITH "Disabled"
    ENDIF

Notes & Additional Information

  • LOT performs some automatic type conversions, but AS provides explicit control when needed.
  • Conversion rules (e.g., how strings are converted to BOOL) depend on the broker implementation.
  • Invalid conversions might result in errors or default values (like 0 for failed string-to-int).