Optional  not required.
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 and a list of other formulas available.
Requirements:
 Data Inputs referencing 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.
Operators and Modifiers:
Symbol 
Modification 
Example // x = 2 
+  
Addition 
x+1 // Returns 3 
* / ** % 
Multiplication 
x**3 // Returns 8 
>> <<  & 
Bitwise shifts, masks 
See Bit Masking example below 
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.
Some advanced Data Inputs can have multiple variables. An example of multivariable data points is included below.
Predefined Formulas:
Some predefined formulas are available to quickly set up formulas. Presets include but are not limited to Min, Max, Average, Totalize and more.
You also can select a Time Window with which you would like this preset formula to reference.
Custom Formulas:
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 
Bit reference 
(x&1) 
Zero Bit (x&2^0)/(2^0) 
(x&2)/2 
First Bit (x&2^1)/(2^1) 
(x&4)/4 
Second Bit (x&2^2)/(2^2) 
(x&8)/8 
Third Bit (x&2^3)/(2^3) 
(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
Please sign in to leave a comment.