How To: Use a Custom Inline Insight to Remap Numbers to Strings

Inline Insights

At the Professional tier, ExoSense allows users to create custom transforms. An overview of the feature is available here: Custom Inline Insights - Exosite Documentation. Custom Insights are considered a Developer Topic. This post will assume that the reader is comfortable using a low-code solution and has read the documentation for Inline Insight Functions.

A common use case for inline insights is remapping a signal value. Say a device reports a status code as an integer. Rather than having your user look up the meaning of the code, you could use an insight to turn that code into text. Consider the following table:

image

The device reports the code, but we want the end user to see the status. We’ll create some custom Inline Insight Functions to accomplish this task. We’ll start by creating a function specific to the case described above, then look at how we can create a more general function that can be used in a variety of cases. Your specific use-case will determine which one you choose to implement.

The Case-Specific Solution

Start by navigating to the Insight Management tab in the sidenav.

If you do not already have an “Insight Module” you would like to place the function in, click the + button at the top right and create a new module.

Click the + again and create a new Custom Insight Function.

This Transform will have one input, the code, and one output, the status. Because we’ll be coding this to be specific to this device and its status codes, we won’t need any constants.

For the Transform’s logic, we’ll use MathJS. The MathJS interface doesn’t include if...else... statements, but it does include ternary expressions. We can chain ternary expressions to check the value of the input A against the values we expect the device to send. When we get a match, we output the appropriate string. If the input fails to match any of our cases, we’ll output undefined to prevent the Transform from emitting a value.

The General Solution

Our previous solution was case-specific: our logic expected an integer value from 0 to 3 and output a predefined string. If don’t know what values our device is going to send and what kind of output string we want, we can create a general-purpose Transform using constants that will be defined when the Transform is added to a signal.

Start by creating a new Transform. We will again have only one input, the code, and one output, the status. But this time, we’ll include a number of constants. We’ll need a constant for each possible input and a corresponding constant for the output. To account for 3 possible input codes, we’ll need 6 total constants. To account for 5 possible input codes, we need 10 total constants. We’ll create an example that covers up to three input values.

Aside: You can create a Transform without a predefined number of constants by using the Multiple option. Doing so requires different logic, which we will not discuss in this post.

Add constants to the transform by clicking the + button to the left of the constant entry form. We’ll name our constants code_1, value_1, code_2, value_2, etc. Constants are referenced in the logic using the name given to them here.

For the logic, we will again use MathJS and ternary expressions. This time, instead of checking the input against a hardcoded value, we’ll check it against the constants. If we get a match, we’ll output the corresponding constant value.

Recommendations

When possible, we recommend using purpose-built Transforms with descriptive names over general-purpose Transforms. Purpose-built Transforms save the end user time, since they don’t need to be configured when added to a Signal, and cut down on errors, since the user can’t make a mistake when setting up the constants. Naming Transforms descriptively also makes them easier to identify in the Signal list.

For help creating custom Inline Insight Functions, please contact support@exosite.com.