Use Expressions in Actions

In this document, we explain the syntax of the expressions you can use in Action Condition and some Action Config fields. These expressions are used to access and evaluate the Message and Context of event messages received from the channel. In action condition fields, you can use these expressions to conditionally execute actions based on data in event message. In some action config fields, you can also use these expressions to dynamically customize the input data for the action.

Action Condition Expressions

In action conditions, expressions will evaluate to a boolean value.

The action condition is evaluated each time the channel receives a new event. If the result of the evaluation is true, the action is executed.

For example, this expression is evaluated as true when the Message of an event sent to the channel is {"temperature": 42} and the current day of the week in UTC is Monday.

(payload.temperature - 2.12) >= 32.8 && getUTCDayOfWeek(now()) == 1

For another example, this expression is evaluated as true when the Message sent from the event source is {"clicktype": "SINGLE"} or when the Context sent to the channel contains {"protocol": "http"}.

startsWith(toUpperCase(event.payload.clicktype), "SINGLE") || context.protocol == "http"

Action Config Expressions

In supported action config input fields, expressions will evaluate to strings.

The action config expression is evaluated when the action is executed. The result of the evaluation is used in the input of the action.

For example, if the event is {"presignedUrls": {"get": "https://example.com"}}, the output of the following expression will be <https://example.com|Image>.

<${event.payload.presignedUrls.get}|Image>

For another example, if the event is {"output": {"people": 3, "people_with_blue_shirts": 0}}, the output of the following expression will be There are 3 people. Among them, 0 are wearing blue shirts!.

There are ${payload.output.people} people. Among them, ${payload.output.people_with_blue_shirts} are wearing blue shirts!

Expression Syntax Reference

Expressions in Action Condition and Action Config fields can include variables, operators, and functions. The following is a reference of the syntax you can use in these expressions.

Variables

Variable Description
payload The Message sent to the channel. If it's a JSON object, you can retrieve values from the JSON object using syntax like payload.temperature or payload.values[1].foo["click type"]. If it's not a JSON object, payload is treated as a string.
context A JSON object corresponding to the Context sent to the channel. The schema of the object varies by event type. Refer to each Event Source reference for more details. For example: IoT Device Event Source
event.payload The original Message sent from the event source to the channel.
event.context A JSON object corresponding to the Context sent from the event source to the channel.

payload and event.payload have the same value in the channel that first received the event message from an event source. Similarly, context and event.context also have the same value.

Operators

Operator Description
|| Logical OR: Returns true if either condition is true.
&& Logical AND: Returns true if both conditions are true.
? : Ternary operator: Used as a ? b : c, it returns b if the condition a is evaluated as true, and c if evaluated as false. For example: event.payload.click_type == 1 ? "Single click" : "Double click"
== Equal to: Returns true if the values are equal.
!= Not equal to: Returns true if the values are not equal.
>= Greater than or equal to: Returns true if the left value is greater than or equal to the right value.
> Greater than: Returns true if the left value is greater than the right value.
<= Less than or equal to: Returns true if the left value is less than or equal to the right value.
< Less than: Returns true if the left value is less than the right value.
+ Addition: Returns the sum of two values.
- Subtraction: Returns the difference of two values.
* Multiplication: Returns the product of two values.
/ Division: Returns the quotient of two values.
% Modulus: Returns the remainder when the left value is divided by the right value.
** Exponentiation: Returns the result of raising the left value to the power of the right value.

Functions

Function Description
includes(string, value) Checks if string contains value, returning true or false.
startsWith(string, prefix) Checks if string begins with prefix, returning true or false.
endsWith(string, suffix) Checks if string ends with suffix, returning true or false.
toLowerCase(string) Converts string to lowercase.
toUpperCase(string) Converts string to uppercase.
now() Returns the current time in Unix milliseconds.
datetime(iso_string) Converts an ISO 8601 string to Unix milliseconds.
getUTCYear(unix_time | iso_string) Retrieves the UTC year from a UNIX time or ISO 8601 string.
getUTCMonth(unix_time | iso_string) Retrieves the UTC month from a UNIX time or ISO 8601 string.
getUTCDate(unix_time | iso_string) Retrieves the UTC day from a UNIX time or ISO 8601 string.
getUTCHours(unix_time | iso_string) Retrieves the UTC hour from a UNIX time or ISO 8601 string.
getUTCMinutes(unix_time | iso_string) Retrieves the UTC minute from a UNIX time or ISO 8601 string.
getUTCSeconds(unix_time | iso_string) Retrieves the UTC second from a UNIX time or ISO 8601 string.
getUTCMilliseconds(unix_time | iso_string) Retrieves the UTC milliseconds from a UNIX time or ISO 8601 string.
getUTCDayOfWeek(unix_time | iso_string) Returns the UTC day of the week from a UNIX time or ISO 8601 string, as a number (0 = Sunday).
int(unix_time | iso_string) Retrieves the UTC second from a UNIX time or ISO 8601 string.