Formulas are a feature of data inputs that allow users to interpret and calculate the values that will be represented on dashboards and reports. This guide will show a few examples of common formulas and includes a table of acceptable operators.
Requirements:
 Data inputs referencing a gateway channel or mapped registers (PLC) or data input sources*
 Fulladmin or equivalent access on cloud.samsara.com
Note: Formulas are currently only accessible on the following sources: third party pin (PLC mapped registers), gateway channel, and data inputs. You can reference another data input as a source which will allow you to add formulas to Samsara sensor data or even multiple data inputs.
:
Symbol

Modification

Format

Example // x = 2

+ 

Addition

Left Side: Numerical Right Side: Numerical Returns: Numerical

x+1 // Returns 3



Subtraction

Left Side: Numerical Right Side: Numerical Returns: Numerical

x  1 // Returns 1

* / ** %

Multiplication

Left Side: Numerical
Right Side: Numerical
Returns: Numerical

x*3 // Returns 6

>> <<  &

Bitwise shifts, masks

Left Side: Numerical Right Side: Numerical Returns: Numerical

See Bit Masking example below

& , 

Boolean

Left Side: Numerical Right Side: Numerical Returns: Numerical

x & 1 = 0010 & 0001 // Returns 0

?, : 
Conditional (If/else) 
Left Side: Boolean Right Side: Numerical Returns: Numerical 
x > 1 ? x : 0 // Returns 2
Note: These operators must be used in conjunction with one another.
 ? checks if condition on left (x > 1) is true, if true it returns value on right. If false it returns null.
 : checks if condition on left (x>1 ? x) is null, if null it returns value on right.

Setting up Variables:
Create the variable you would like to your device to refer to. A common starting variable is “x”.
One of the easiest ways to read a raw value from your data inputs is to use your variable “x” as the formula. This will represent the original value that the data input is receiving. This will allow you to view the last raw datapoint on the data inputs page and view raw datapoints from any reports or dashboards referencing this data input.
Predefined Formulas:
Some predefined formulas are available to quickly set up formulas. Presets include but are not limited to Min, Max, Average, and more.
You also can select a Time Window with which you would like this preset formula to reference.
Multivariable  Multivariable Data Inputs if you want to calculate multiple data inputs at one time. Each variable must be an existing data input to select them from the dropdown.
Bitmasking values  Some Data Inputs will be reporting bit data from a Third Party Device. Bitmasking is a useful tool to extract the specific bit you would like your Data Input to refer to in order to accurately represent data being communicated from your Third Party Device.
The bitmasking formula is relatively simple and follows the following pattern (x&2^[bit#])/(2^[bit#]):
Formula

Example // x = 2

(x&1)

Zero Bit (x&2^0)/(2^0)

(x&2)/2

First Bit (x&2^1)/(2^1)

(x&4)/4

See Bit Masking example below

(x&8)/8

Second Bit (x&2^2)/(2^2)

(x&4096)/4096

Twelfth Bit (x&2^12)/(2^12)

Other Available Formulas:
Min with 4 values


Example Formula:

min(x,1,2,3)

Example Inputs:

{3, 2, 0, 1, 2, 3},

Expected Outputs:

{3, 2, 0, 1, 1, 1}



Max with 4 values


Example Formula:

max(x,1,2,3)

Example Inputs:

{0, 1, 2, 3, 4, 5}

Expected Outputs:

{3, 3, 3, 3, 4, 5}



Avg with 4 values


Example Formula:

avg(x,1,2,3)

Example Inputs:

{0, 1, 2, 3, 4, 5}

Expected Outputs:

{1.5, 1.75, 2, 2.25, 2.5, 2.75}



Median (even)


Example Formula:

median(x, 1, 2, 3)

Example Inputs:

{0, 1, 2, 3, 4, 5}

Expected Outputs:

{1.5, 1.5, 2, 2.5, 2.5, 2.5}



Median (odd)


Example Formula:

median(x, 1, 2, 3, 4)

Example Inputs:

{0, 1, 2, 3, 4, 5}

Expected Outputs:

{2, 2, 2, 3, 3, 3}



Mode


Example Formula:

mode(x, 1, 2, 2, 4)

Example Inputs:

{0, 1, 2, 3, 4, 5}

Expected Outputs:

{2, 1, 2, 2, 2, 2}



Mode (multiple)


Example Formula:

mode(x, 1, 2, 2, 1)

Example Inputs:

{0, 1, 2, 3, 4, 5}

Expected Outputs:

{1, 1, 2, 1, 1, 1}



Standard Deviation


Example Formula:

stddev(x, 1, 2, 3)

Example Inputs:

{2, 4, 9, 16, 25, 30}

Expected Outputs:

{0.7071067811865476, 1.118033988749895, 3.112474899497183, 6.103277807866851, 9.98436277385793, 12.144957801491119}



Square Root


Example Formula:

sqrt(x)

Example Inputs:

{2, 4, 9, 16, 25, 30}

Expected Outputs:

{1.4142135623730951, 2, 3, 4, 5, 5.477225575051661}



Floor


Example Formula:

floor(x)

Example Inputs:

{6, 4.1, 4.99, 4.999, 4.9999, 4.99999}

Expected Outputs:

{6, 4, 4, 4, 4, 4}



Ceiling


Example Formula:

ceil(x)

Example Inputs:

{6, 4.1, 4.99, 3.999, 3.0000000001, 4.4}

Expected Outputs:

{6, 5, 5, 4, 4, 5}



Absolute


Example Formula:

abs(x)

Example Inputs:

{3, 2, 0, 1, 2, 3}

Expected Outputs:

{3, 2, 0, 1, 2, 3}



Round


Example Formula:

round(5.854123,x)

Example Inputs:

{0, 1, 2, 3, 4, 5}

Expected Outputs:

{6, 5.9, 5.85, 5.854, 5.8541, 5.85412}

Comments
0 comments
Article is closed for comments.