> For the complete documentation index, see [llms.txt](https://docs.aitheon.com/creators-studio/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.aitheon.com/creators-studio/standard-components/function.md).

# function

## function

You need this component when you can\`t find another one with the needed function. So, you can write this function to the **function** component. You need to know JavaScript to do this. &#x20;

![](https://3983587133-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LvAp4NFmv7Cj5kJvKjw%2F-MM1HZeii7N6PDdI508p%2F-MM1Kh-xoboGg5NkH2A9%2Ffunction.gif?alt=media\&token=5c29058b-283c-4c5e-ab58-c07a792e22c9)

Explore [**Writing Functions Guide**](/creators-studio/writing-functions-guide.md).

## change

The **change** component provides changing a message.&#x20;

This is a simple flow where a message payload is '1' and the topic is 'Start'. &#x20;

![](https://3983587133-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LvAp4NFmv7Cj5kJvKjw%2F-MMFBsaX3x-ZjzUNXVy4%2F-MMFFBTK9GL2hyG46fFa%2Fswitch1.gif?alt=media\&token=f9554b56-01ff-4e4a-a42c-21020185e332)

#### Properties

You can change this payload value or topic in four ways:&#x20;

![](https://3983587133-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LvAp4NFmv7Cj5kJvKjw%2F-MMFBsaX3x-ZjzUNXVy4%2F-MMFHKuHQIFoGy30VFcg%2Fchange%20prop.png?alt=media\&token=c6d42171-7bc7-427d-a93b-25a45b5e0539)

**1. Set** - set a value to a message payload or topic just by setting a new one. &#x20;

![](https://3983587133-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LvAp4NFmv7Cj5kJvKjw%2F-MMFBsaX3x-ZjzUNXVy4%2F-MMFGRn5Bsnn3D6V2IjP%2Fchange2.gif?alt=media\&token=50713c84-7594-4712-8f88-0904b23b8e0c)

You can see that the first debug component shows the payload value '1', when the second one that goes after the change component shows '2'.

**2. Change** - change a message due to a specific condition.  &#x20;

![](https://3983587133-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LvAp4NFmv7Cj5kJvKjw%2F-MMFBsaX3x-ZjzUNXVy4%2F-MMFIlQ-d_L8bW_4tDyP%2Fchange3.gif?alt=media\&token=11cf15e3-a315-4012-8bf2-39f97995d165)

Here we set the conditions: if msg.payload = 1 (number type), then change it to '2' (number type); if msg.topic = 'Start' (string tipe), then change it to 'Launch' (string type).&#x20;

**3.** **Delete** - delete a message or a part (a parameter) of the message object. &#x20;

![](https://3983587133-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LvAp4NFmv7Cj5kJvKjw%2F-MMFBsaX3x-ZjzUNXVy4%2F-MMFJooVD-2bMfdDHuoF%2Fchange4.gif?alt=media\&token=1468ea78-d961-498a-9ad6-595702ade5be)

**4.** **Move** - move a value to a new message property, removing the previous one at the same time. (Note: to show explicitly how we moved the value '**Start**' from '**topic**' to a new property '**name**', we changed the output method in the debug components). &#x20;

![](https://3983587133-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LvAp4NFmv7Cj5kJvKjw%2F-MMFBsaX3x-ZjzUNXVy4%2F-MMFKy-kHXjlTSv7kMW0%2Fchange5.gif?alt=media\&token=e53f1f20-fcfd-4c8c-a387-e4f7ed8527b3)

## switch

The **switch** component builds different paths due to conditions.

In this example, we imitate two kinds of messages from a sensor - '**1**' and '**23**'. Using the **change** components, we set '**Ok**' to the payload after the message '**1**', and '**Alarm!**' after the message '**23**'. &#x20;

![](https://3983587133-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LvAp4NFmv7Cj5kJvKjw%2F-MMFLL9ZFeOjYfo2jxHV%2F-MMFR6PO5_yKv9Kj9-T2%2Fswitch1.gif?alt=media\&token=0a053e36-2af3-4763-bd4c-ca16c78ec053)

Imagine that it\`s Ok when the sensor sends '1'. For any other number, we have to send an '**Alarm!**' message. The switch component may take a message and send it to a route depending on different conditions. In our example - if '**1**' then to the '**Ok**' route, if '**not 1**' - to the '**Alarm!**' one.&#x20;

![](https://3983587133-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LvAp4NFmv7Cj5kJvKjw%2F-MMFT3DVZeqAhs7cwjJ9%2F-MMFTBHdVsF6QOtsjnkr%2Fswitch2.gif?alt=media\&token=0ab71ebb-eb53-4aae-acf3-ebd7273d265c)

#### Properties

Many condition options in the component properties allow specifying conditions for a message\`s further path. Each condition will make a new connection port on the component in turn.

![](https://3983587133-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LvAp4NFmv7Cj5kJvKjw%2F-MMFTTfglWrNjvRObATM%2F-MMFUVB8pBKCthhrC8rN%2Fswitch%20prop.png?alt=media\&token=37e3b1ef-2b46-49d9-8644-f4fae36ffd0c)

## range

Mapps the payload number due to properties set up. If the payload is not numeric tries to convert it - for example, string type '1' to numeric type '1'.

#### Properties&#x20;

![](https://3983587133-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LvAp4NFmv7Cj5kJvKjw%2F-MPccL5ACRgaBuetpFTM%2F-MPcfKHcP1zfxmC_SVca%2Fimage.png?alt=media\&token=f341a2ea-5ad5-4908-a5c7-9f4e57550e53)

Choose the msg **Property** to scale **(1)**. By default, it\`s msg.payload.

Choose **Action** - the type of mapping **(2)**.&#x20;

* **Scale the message property** - the number will be scaled due to input and target range ratio. For example, if the target range is x10 to the input range so the payload number will change to x10. '5' becomes '50' etc.
* **Scale and limit to target range** - the number scales the same way, but the result will be in the target range. E. g. '12' becomes '120', but the result will be '100' due to the range maximum.
* **Scale and wrap within the target range** - the result will be wrapped within the target range. So '12' becomes '120', but only '20' is wrapped within the target range.

Specify **the input range** **(3)**.

Specify **the target range** **(4)**.

You can round the result to the nearest integer **(5)**.

In the example, we will examine the three action types for the input scale **0 to 10** and the target scale **0 to 100**. &#x20;

![](https://3983587133-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LvAp4NFmv7Cj5kJvKjw%2F-MPccL5ACRgaBuetpFTM%2F-MPcfT6Soy4g4GNsHo_o%2F7.gif?alt=media\&token=07661f5e-4928-44cc-8a8f-4743ca51cdf5)

It can be used for percent converting, for example. Just choose **the target range** 0 to 100 and **Scale the message property** action type.

## template

Sets the payload by embedding input values to a template. Useful for composing messages, emails, HTML pages, etc.&#x20;

#### Properties&#x20;

![](https://3983587133-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LvAp4NFmv7Cj5kJvKjw%2F-MPccL5ACRgaBuetpFTM%2F-MPcf_JL-V2ravdCOXQE%2Fimage.png?alt=media\&token=41401d6d-801d-42a0-9389-5261ad57a4e7)

Choose the message context (from the component, the flow, or the global context variable) and the message object **Property (1)** to extract the data from.

In the **Template** field **(2),** specify the template for the output message. By default, it proposes you to use a simple expression with dynamic adding of the payload value. E.g., if you write **Hello, {{payload}}!** and send **Bob** payload to the component, the result will be **Hello, Bob!**

Double curly braces are the [Mustache](https://mustache.github.io/mustache.5.html) syntax for taking a corresponding variable. The template field also takes other valid syntax and allows you to **Highlight** the **Syntax**.

Choose **Format (3)** - if you choose Plain text, the output message will ignore template syntax.

Choose the **Output** format **(4)** - you may use the template for generating JSON or YAML content. &#x20;

![](https://3983587133-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LvAp4NFmv7Cj5kJvKjw%2F-MPccL5ACRgaBuetpFTM%2F-MPcfjnnNYsurGqfmUs5%2F8.gif?alt=media\&token=706cdb18-6052-446f-ad78-fd769e2a467a)

## delay

Delays each message passing through the node or limits the rate at which they can pass.

#### Properties

There are two modes (Action types) for the component\`s properties.&#x20;

![](https://3983587133-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LvAp4NFmv7Cj5kJvKjw%2F-MPccL5ACRgaBuetpFTM%2F-MPcft_htN8uUGT04ycU%2Fimage.png?alt=media\&token=36aedcdd-be1e-4d14-8258-d0133c7174c6)

The first mode is **Delay each message (1)**. Allows to set up a delay span for each message that comes through. For example, to avoid flooding your email or a dashboard with messages.

You can set a **Fixed** delay interval, a **Random** one between some numbers, or use the **msg.delay** of each message **(2)**.

Set up the delay interval for each message **(3)**.&#x20;

![](https://3983587133-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LvAp4NFmv7Cj5kJvKjw%2F-MPccL5ACRgaBuetpFTM%2F-MPcfyvS9GgUkwdpNpjh%2Fimage.png?alt=media\&token=a4d8a011-07b8-48ed-99c9-570910012af0)

In the **Rate Limit** mode **(4),** the component limits the number of messages that come through at an interval.

Choose to rate the limit for **All messages** or **For each msg.topic (5)**. For this second option, you can choose to release the most recent message for all topics or release the most recent message for the next topic.

Set up the **Rate** **(6)**.&#x20;

You can optionally discard intermediate messages as they arrive **(7)**.

In the next example, we will inject a message with the array of numbers from 0 to 5, then split it to separate messages for each number and limit their arrival to 1 for a second. &#x20;

![](https://3983587133-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LvAp4NFmv7Cj5kJvKjw%2F-MPccL5ACRgaBuetpFTM%2F-MPcg6IAR_1THOK-GYI9%2F9.gif?alt=media\&token=fdef9b62-8f9c-40eb-a058-22d2315681af)

## trigger

The component sends a message when triggered and then sends the second one on some conditions.&#x20;

#### Properties&#x20;

![](https://3983587133-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LvAp4NFmv7Cj5kJvKjw%2F-MPccL5ACRgaBuetpFTM%2F-MPcgERsF-LSBPEAV3eP%2Fimage.png?alt=media\&token=22f7483b-4fdf-4bcf-9dc5-e719050e42a8)

Choose a message value to **Send (1)**. By default, it\`s a string type '1'. If you want it to send a message that arrived as a trigger, choose **the existing msg object**.

There are three modes for the component behavior after the first message sent **(2)**:

* **wait for** - the mode when you specify the time span **(3)** for the next message **(5)** release;&#x20;
* **wait to be reset** - when triggered, the component sends a message and blocks all subsequent ones before receives a **reset** **(7)** command. Then sends the message again and so on;
* **resend it every** - when triggered, the component resends the same message in specified intervals **(3)**.

In the **wait for** mode, you can choose to extend the delay if a new message arrives **(4)**. E.g., the trigger node will 'stay calm' until it receives signals, and it sends an 'alarm' when signals vanish - as it works in watchdog devices.

The interval may be set up by an incoming msg.delay **(4)**.

Specify a second message **(5)**. You may choose to send the second message to a separate output **(6)**.

There are two types of the reset command **(7)**: incoming **msg.reset** with any value, or you can define the **msg.payload** value that resets the trigger component.

Choose whether it handles **all messages** or **each** one **(8)**.

In the example, we send a message every 1 second, but it blocks all the following messages until we reset the condition by sending **msg.reset**.  &#x20;

![](https://3983587133-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LvAp4NFmv7Cj5kJvKjw%2F-MPccL5ACRgaBuetpFTM%2F-MPcgMzjrwb67pvWE9z4%2F10.gif?alt=media\&token=fb4d46f8-1ef4-4038-bd8c-ad678de36be6)

{% hint style="info" %}
Notice that the blue dot below the trigger component indicates the ongoing condition - in this example, it\`s waiting for a reset message.&#x20;
{% endhint %}

## exec

The component allows you to execute system commands or scripts and take their outputs. For example, you can run a **copy** command (for Windows) to copy a file to another directory.&#x20;

#### Properties  &#x20;

![](https://3983587133-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LvAp4NFmv7Cj5kJvKjw%2F-MPccL5ACRgaBuetpFTM%2F-MPcgU4i-Jbplr45XYDB%2Fimage.png?alt=media\&token=d486c75c-9e9f-42df-a9d3-cac55b68c8ae)

Enter a system Command **(1)**.&#x20;

You may use the **msg.payload** as the command parameters **(2);** otherwise, you can write them in **extra input parameters (3)**.

You can also use **extra input parameters** **(3)** to add some flags (extra parameters) to the command.

Choose the **Output** mode **(4)**. In the **exec mode,** you can see the output after the command is completed; in the **spawn mode,** you will see the results line by line as the command runs.&#x20;

Set up **Timeutout** to limit a command execution time **(5)**.

Here we execute the **echo** command using the **Command** field, then **extra input parameters** field, and the **msg.payload** as the command parameter:  &#x20;

![](https://3983587133-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LvAp4NFmv7Cj5kJvKjw%2F-MPccL5ACRgaBuetpFTM%2F-MPcgb2WqIVIZk3CAk8i%2F11.gif?alt=media\&token=1f7e0af5-3179-429b-b7e0-b5977a451f01)

The exec component has 3 outputs: for a payload (the result of a command execution), for en error information if any, and for execution code (0 for success and any other for failure).&#x20;

## rbe

Report by exception (rbe) component passes on data only if the payload changes. For example, you send to a motor the command "on", and the rbe component will block all following "on\`s" but will pass the "off" command.

#### Properties&#x20;

![](https://3983587133-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LvAp4NFmv7Cj5kJvKjw%2F-MPccL5ACRgaBuetpFTM%2F-MPcghgvFVLpqGturRA_%2Fimage.png?alt=media\&token=13c6e38d-49f2-4543-bfd3-7ab8532c1d8e)

Choose the blocking **Mode (1)** - there are several of them. The additional blocking properties will appear if you choose other modes.

Choose the **Property (2)** of the message object that the component passes (and blocks). By default, it\`s **payload**.

In the following example, we send through the rbe component a payload value '**1**' multiply, but the node passes it only once and then blocks until we send '**2**'. And vice versa.  &#x20;

![](https://3983587133-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LvAp4NFmv7Cj5kJvKjw%2F-MPccL5ACRgaBuetpFTM%2F-MPcgq-R57h26bZ4py0d%2F12.gif?alt=media\&token=ae1dc044-10ba-4e6b-ad4b-36a2690ad7e4)

##
