Difference between revisions of "Functions"

From webCoRE Wiki - Web-enabled Community's own Rule Engine
Jump to: navigation, search
(Boolean)
Line 1: Line 1:
 
Functions are extensions to expressions that allow data processing and conversion. Here is a list of all defined functions
 
Functions are extensions to expressions that allow data processing and conversion. Here is a list of all defined functions
  
==[[Variable_data_types#Boolean|Boolean]]==
+
==[[Variable_data_types#Boolean|Boolean]] functions==
  
===bool===
+
==bool==
 
:Syntax
 
:Syntax
 
::<code>bool(''dynamic'' value)</code>
 
::<code>bool(''dynamic'' value)</code>
Line 9: Line 9:
 
::Returns the truth value of the input. Accepts anything as input and will return true if <code>value</code> is either a non-zero number, a non-empty string (with some exceptions, see below), a non-empty device list, a non-empty date/time/datetime
 
::Returns the truth value of the input. Accepts anything as input and will return true if <code>value</code> is either a non-zero number, a non-empty string (with some exceptions, see below), a non-empty device list, a non-empty date/time/datetime
  
===boolean===
+
==boolean==
 
:This is an alias of [[Functions#bool|bool]]
 
:This is an alias of [[Functions#bool|bool]]
  
===contains===
+
==contains==
 
:Syntax
 
:Syntax
 
::<code>contains(''string'' haystack, ''string'' needle)</code>
 
::<code>contains(''string'' haystack, ''string'' needle)</code>
Line 18: Line 18:
 
::Returns true if the <code>needle</code> is found anywhere in the <code>haystack</code>
 
::Returns true if the <code>needle</code> is found anywhere in the <code>haystack</code>
  
===endsWith===
+
==endsWith==
 
:Syntax
 
:Syntax
 
::<code>endsWith(''string'' haystack, ''string'' needle)</code>
 
::<code>endsWith(''string'' haystack, ''string'' needle)</code>
Line 24: Line 24:
 
::Returns true if <code>haystack</code> ends with the <code>needle</code>
 
::Returns true if <code>haystack</code> ends with the <code>needle</code>
  
===eq===
+
==eq==
 
:Syntax
 
:Syntax
 
::<code>eq(''dynamic'' value1, ''dynamic'' value2)</code>
 
::<code>eq(''dynamic'' value1, ''dynamic'' value2)</code>
Line 30: Line 30:
 
::Returns true if <code>value1</code> is equivalent to <code>value2</code>
 
::Returns true if <code>value1</code> is equivalent to <code>value2</code>
  
===ge===
+
==ge==
 
:Syntax
 
:Syntax
 
::<code>ge(''dynamic'' value1, ''dynamic'' value2)</code>
 
::<code>ge(''dynamic'' value1, ''dynamic'' value2)</code>
Line 36: Line 36:
 
::Returns true if <code>value1</code> is greater than or equal to <code>value2</code>
 
::Returns true if <code>value1</code> is greater than or equal to <code>value2</code>
  
===gt===
+
==gt==
 
:Syntax
 
:Syntax
 
::<code>gt(''dynamic'' value1, ''dynamic'' value2)</code>
 
::<code>gt(''dynamic'' value1, ''dynamic'' value2)</code>
Line 42: Line 42:
 
::Returns true if <code>value1</code> is greater than <code>value2</code>
 
::Returns true if <code>value1</code> is greater than <code>value2</code>
  
===isBetween===
+
==isBetween==
 
:Syntax
 
:Syntax
 
::<code>isBetween(''dynamic'' value, ''dynamic'' startValue, ''dynamic'' startValue)</code>
 
::<code>isBetween(''dynamic'' value, ''dynamic'' startValue, ''dynamic'' startValue)</code>
Line 48: Line 48:
 
::Returns true if <code>value</code> is greater then or equal to <code>startValue</code> and less than or equal to <code>endValue</code>
 
::Returns true if <code>value</code> is greater then or equal to <code>startValue</code> and less than or equal to <code>endValue</code>
  
===isEmpty===
+
==isEmpty==
 
:Syntax
 
:Syntax
 
::<code>isEmpty(''dynamic'' value)</code>
 
::<code>isEmpty(''dynamic'' value)</code>
Line 54: Line 54:
 
::Returns true if <code>value</code> is not set, an empty string, or zero
 
::Returns true if <code>value</code> is not set, an empty string, or zero
  
===le===
+
==le==
 
:Syntax
 
:Syntax
 
::<code>le(''dynamic'' value1, ''dynamic'' value2)</code>
 
::<code>le(''dynamic'' value1, ''dynamic'' value2)</code>
Line 60: Line 60:
 
::Returns true if <code>value1</code> is less than or equal to <code>value2</code>
 
::Returns true if <code>value1</code> is less than or equal to <code>value2</code>
  
===lt===
+
==lt==
 
:Syntax
 
:Syntax
 
::<code>lt(''dynamic'' value1, ''dynamic'' value2)</code>
 
::<code>lt(''dynamic'' value1, ''dynamic'' value2)</code>
Line 66: Line 66:
 
::Returns true if <code>value1</code> is less than <code>value2</code>
 
::Returns true if <code>value1</code> is less than <code>value2</code>
  
===not===
+
==not==
 
:Syntax
 
:Syntax
 
::<code>not(''dynamic'' value)</code>
 
::<code>not(''dynamic'' value)</code>
Line 72: Line 72:
 
::Returns the negated truth state of <code>value</code>. This is the logical negation of <code>[[Functions#bool|bool]](''dynamic'' value)</code>.
 
::Returns the negated truth state of <code>value</code>. This is the logical negation of <code>[[Functions#bool|bool]](''dynamic'' value)</code>.
  
===startsWith===
+
==startsWith==
 
:Syntax
 
:Syntax
 
::<code>startsWith(''string'' haystack, ''string'' needle)</code>
 
::<code>startsWith(''string'' haystack, ''string'' needle)</code>

Revision as of 03:32, 6 May 2017

Functions are extensions to expressions that allow data processing and conversion. Here is a list of all defined functions

Boolean functions

bool

Syntax
bool(dynamic value)
Returns
Returns the truth value of the input. Accepts anything as input and will return true if value is either a non-zero number, a non-empty string (with some exceptions, see below), a non-empty device list, a non-empty date/time/datetime

boolean

This is an alias of bool

contains

Syntax
contains(string haystack, string needle)
Returns
Returns true if the needle is found anywhere in the haystack

endsWith

Syntax
endsWith(string haystack, string needle)
Returns
Returns true if haystack ends with the needle

eq

Syntax
eq(dynamic value1, dynamic value2)
Returns
Returns true if value1 is equivalent to value2

ge

Syntax
ge(dynamic value1, dynamic value2)
Returns
Returns true if value1 is greater than or equal to value2

gt

Syntax
gt(dynamic value1, dynamic value2)
Returns
Returns true if value1 is greater than value2

isBetween

Syntax
isBetween(dynamic value, dynamic startValue, dynamic startValue)
Returns
Returns true if value is greater then or equal to startValue and less than or equal to endValue

isEmpty

Syntax
isEmpty(dynamic value)
Returns
Returns true if value is not set, an empty string, or zero

le

Syntax
le(dynamic value1, dynamic value2)
Returns
Returns true if value1 is less than or equal to value2

lt

Syntax
lt(dynamic value1, dynamic value2)
Returns
Returns true if value1 is less than value2

not

Syntax
not(dynamic value)
Returns
Returns the negated truth state of value. This is the logical negation of bool(dynamic value).

startsWith

Syntax
startsWith(string haystack, string needle)
Returns
Returns true if haystack starts with the needle

Date

date(value)
returns the value as a date type

Datetime

addDays(dateTime, days)
returns the value as a Datetime type
addHours(dateTime, hours)
returns the value as a Datetime type
addMinutes(dateTime, minutes)
returns the value as a Datetime type
addSeconds(dateTime, seconds)
returns the value as a Datetime type
addWeeks(dateTime, weeks)
returns the value as a Datetime type

Decimal

avg(values)
calculates the average of a series of numeric values
ceil()
//todo
ceiling(decimal or string)
converts a decimal value to it's closest higher integer value
celsius(temperature)
converts temperature from Fahrenheit to Celsius
decimal(integer or string)
converts an integer value to it's decimal value
dewPoint(temperature, relativeHumidity[, scale])
returns the calculated dew point temperature
fahrenheit([Sensor;temperature])
converts temperature from Celsius to Fahrenheit
float()
//todo
floor(decimal or string)
converts a decimal value to it's closest lower integer value
max(values)
calculates the maximum of a series of numeric values
median(values)
returns the value in the middle of a sorted array
min(values)
calculates the minimum of a series of numeric values
number()
//todo
power(integer or decimal or string, power)
converts a decimal value to it's power decimal value
round(decimal or string, [precision])
converts a decimal value to it's rounded value
sqr(integer or decimal or string)
converts a decimal value to it's square decimal value
sqrt(integer or decimal or string)
converts a decimal value to it's square root decimal value
stdev(values)
calculates the standard deviation of a series of numeric values
sum(values)
calculates the sum of a series of numeric values
variance(values)
calculates the standard deviation of a series of numeric values

Dynamic

if(condition, valueIfTrue, valueIfFalse)
evaluates a boolean and returns value1 if true, or value2 otherwise
least(values)
returns the value that is least found a series of numeric values
most(values)
returns the value that is most found a series of numeric values
previousValue([device;attribute])
returns the previous value of the attribute

Integers

age([device;attribute])
returns the number of milliseconds an attribute had the current value
count(values)
calculates the number of true/non-zero/non-empty items in a series of numeric values
int()
//todo
integer(decimal or string)
converts a decimal value to it's integer value
newer([device;attribute],[...], [device;attribute], threshold)
returns the number of devices whose attribute had the current value for less than the specified number of milliseconds
older([device;attribute],[...], [device;attribute], threshold)
returns the number of devices whose attribute had the current value for more than the specified number of milliseconds
previousAge([device;attribute])
returns the number of milliseconds an attribute had the previous value

String

concat(string1, string2)
returns two strings appended together
Example: concat("hello ", "world") or concat("hello", " ", "world") outputs hello world
format(format string, values)
Usage:
formatString is a Java printf() type format string (see Reference)
values is a dynamic length list of values (one entry for each % in the format string)
returns a string with the placeholders replaced with the appropriately formatted values
Example: format('The temperature outside is %.2f degrees Fahrenheit', [Temp sensor:temperature]) outputs The temperature outside is 71.26 degrees Fahrenheit
formatDuration(value[, friendly = false[, granularity = 's'[, showAdverbs = false]]])
Usage:
value is a number of milliseconds
friendly = true/false (false >>> 0d 00:00:00.000, true >>> so many days, so many hours, etc)
granularity = one of ms, s, m, h, d (smallest part displayed)
showAdverbs = true/false, true adds the in .... or .... ago to friendly times
returns a nicely formatted string of time
eg: formatDuration(12029) - 00:00:12 | formatDuration(12029, true) - 12 seconds | formatDuration(12029, true, "m") - 0 minutes | formatDuration(12029, true, "ms", true) - in 12 seconds and 29 milliseconds
left(string, count)
returns a substring of a value
Example1: left("hello world", 3) outputs hel
Example2: left("hello world", 7) outputs hello w
lower(string)
returns a lower case value of a string
Example: lower("HELLO WORLD") outputs hello world
mid(string, start, count)
returns a substring of a value
returns characters (including spaces) specified by count after start position from string
Example1: mid("hello world", 3, 4) returns lo w
Example2: mid("hello world", 6, 3) returns wor
replace(string, search, replace[, [..], search, replace])
replaces a search text inside of a value
Example1: replace("hello world", "world", "earth") returns hello earth
Example2: replace(replace("[hello world]", "\[", "("),"\]",")") returns (hello world)
Note: There are special characters in regular expressions that needs to be escaped or the script will return an error, in the above example [ and ] are special characters.
right(string, count)
returns a substring of a value
sprintf(format, arguments)
formats a series of values into a string
string(anything)
converts a value to it's string value
substring(string, start, count)
returns a substring of a value
text()
//todo
title(string)
returns a title case value of a string
upper(string)
returns an upper case value of a string

Time

time(value)
returns the value as a time type
addSeconds(dateTime, seconds)
adds seconds to time, returns the value as a time type
addMinutes(dateTime, minutes)
adds minutes to time, returns the value as a time type
addHours(dateTime, hours)
adds hours to time, returns the value as a time type
addDays(dateTime, days)
adds days to time, returns the value as a time type
addWeeks(dateTime, weeks)
adds weeks to time, returns the value as a time type