Skip to content

REPLACE Functional Keyword

Feature Since Version Notes
REPLACE >v1.5.6 String replacement

1. Overview

  • Description: Finds all occurrences of a specified substring within a source string and replaces them with another specified substring.

2. Signature

  • Syntax:
    REPLACE "<substringToFind>" WITH <replacementValue> IN <sourceString>
    

3. Parameters

  • "<substringToFind>": The exact substring to search for within the source string.
  • <replacementValue>: The value that will replace each occurrence of <substringToFind>. Can be a literal, an Entity (PAYLOAD, TIMESTAMP), GET TOPIC result, or a {variable}.
  • <sourceString>: The original string in which to perform the replacement. Can be a literal string, PAYLOAD, GET TOPIC result, or a {variable}.

4. Return Value

  • Description: Returns a new string with all occurrences of <substringToFind> replaced by <replacementValue>.

5. Usage Examples

Basic Example

Replace placeholders in a template message.

DEFINE ACTION SendWelcomeMessage
ON TOPIC "user/new" DO // Assume PAYLOAD is the username
    SET "welcomeMsg" = REPLACE "{user}" WITH PAYLOAD IN "Welcome, {user}! Please check your email."
    PUBLISH TOPIC "user/welcome_message" WITH {welcomeMsg}
* Scenario: Payload Alice arrives on user/new. Result: user/welcome_message gets Welcome, Alice! Please check your email.

Intermediate Example (Dynamic Topic Construction)

Use REPLACE within PUBLISH TOPIC or KEEP TOPIC to create dynamic topics.

DEFINE ACTION PublishToDynamicTopic
ON TOPIC "device/alert/source" DO // Assume PAYLOAD is device ID, e.g., "temp_sensor_01"
    // Creates topic like "alerts/processed/temp_sensor_01"
    PUBLISH TOPIC (REPLACE "{id}" WITH PAYLOAD IN "alerts/processed/{id}") WITH "ALERT_RECEIVED"

Advanced Example (Chaining Replacements - Requires SET)

Replace multiple placeholders sequentially.

DEFINE ACTION FormatReport
ON TOPIC "report/trigger" DO
    SET "template" = "Report for {device}: Status {status}, Timestamp {ts}"
    SET "deviceID" = GET TOPIC "report/device_id"
    SET "deviceStatus" = GET TOPIC "report/status"

    SET "step1" = REPLACE "{device}" WITH {deviceID} IN {template}
    SET "step2" = REPLACE "{status}" WITH {deviceStatus} IN {step1}
    SET "finalReport" = REPLACE "{ts}" WITH TIMESTAMP "ISO" IN {step2}

    PUBLISH TOPIC "report/formatted" WITH {finalReport}

6. Notes & Additional Information

  • Replacement is case-sensitive.
  • If <substringToFind> is not found, the original <sourceString> is returned.