Difference between revisions of "Functions"
From webCoRE Wiki - Web-enabled Community's own Rule Engine
(→String) |
|||
Line 11: | Line 11: | ||
==boolean== | ==boolean== | ||
:This is an alias of [[Functions#bool|bool]] | :This is an alias of [[Functions#bool|bool]] | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
==eq== | ==eq== | ||
Line 71: | Line 59: | ||
:Returns | :Returns | ||
::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>. | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
=[[Variable_data_types#Date|Date]] functions= | =[[Variable_data_types#Date|Date]] functions= | ||
Line 190: | Line 172: | ||
==concat== | ==concat== | ||
:Syntax | :Syntax | ||
− | ::<code>concat('' | + | ::<code>''string'' concat(''dynamic'' value1, ''dynamic'' value2[, .., ''dynamic'' valueN])</code> |
:Returns | :Returns | ||
::Returns a string that is the concatenation of all the supplied values. This function accepts two or more input values. | ::Returns a string that is the concatenation of all the supplied values. This function accepts two or more input values. | ||
+ | |||
+ | ==contains== | ||
+ | :Syntax | ||
+ | ::<code>''boolean'' contains(''string'' haystack, ''string'' needle)</code> | ||
+ | :Returns | ||
+ | ::Returns true if the <code>needle</code> is found anywhere in the <code>haystack</code> | ||
+ | |||
+ | ==endsWith== | ||
+ | :Syntax | ||
+ | ::<code>''boolean'' endsWith(''string'' haystack, ''string'' needle)</code> | ||
+ | :Returns | ||
+ | ::Returns true if <code>haystack</code> ends with the <code>needle</code> | ||
==format== | ==format== | ||
:Syntax | :Syntax | ||
− | ::<code>format(''string'' formatString[, ''dynamic'' value1[, .., ''dynamic'' valueN]])</code> | + | ::<code>''string'' format(''string'' formatString[, ''dynamic'' value1[, .., ''dynamic'' valueN]])</code> |
:Returns | :Returns | ||
::Returns a string that is built based on the <code>formatString</code> with arguments replaced by their corresponding values. Follows the java syntax for <code>printf()</code>. See a quick reference [https://www.cs.colostate.edu/~cs160/.Summer16/resources/Java_printf_method_quick_reference.pdf here], or a more detailed reference [https://sharkysoft.com/archive/printf/docs/javadocs/lava/clib/stdio/doc-files/specification.htm Reference here]. | ::Returns a string that is built based on the <code>formatString</code> with arguments replaced by their corresponding values. Follows the java syntax for <code>printf()</code>. See a quick reference [https://www.cs.colostate.edu/~cs160/.Summer16/resources/Java_printf_method_quick_reference.pdf here], or a more detailed reference [https://sharkysoft.com/archive/printf/docs/javadocs/lava/clib/stdio/doc-files/specification.htm Reference here]. | ||
Line 242: | Line 236: | ||
;substring(string, start, count) | ;substring(string, start, count) | ||
:returns a substring of a value | :returns a substring of a value | ||
+ | |||
+ | ==startsWith== | ||
+ | :Syntax | ||
+ | ::<code>''boolean'' startsWith(''string'' haystack, ''string'' needle)</code> | ||
+ | :Returns | ||
+ | ::Returns true if <code>haystack</code> starts with the <code>needle</code> | ||
+ | |||
;text() | ;text() | ||
://todo | ://todo |
Revision as of 03:56, 6 May 2017
Functions are extensions to expressions that allow data processing and conversion. Here is a list of all defined functions
Contents
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
- Returns the truth value of the input. Accepts anything as input and will return true if
boolean
- This is an alias of bool
eq
- Syntax
eq(dynamic value1, dynamic value2)
- Returns
- Returns true if
value1
is equivalent tovalue2
- Returns true if
ge
- Syntax
ge(dynamic value1, dynamic value2)
- Returns
- Returns true if
value1
is greater than or equal tovalue2
- Returns true if
gt
- Syntax
gt(dynamic value1, dynamic value2)
- Returns
- Returns true if
value1
is greater thanvalue2
- Returns true if
isBetween
- Syntax
isBetween(dynamic value, dynamic startValue, dynamic startValue)
- Returns
- Returns true if
value
is greater then or equal tostartValue
and less than or equal toendValue
- Returns true if
isEmpty
- Syntax
isEmpty(dynamic value)
- Returns
- Returns true if
value
is not set, an empty string, or zero
- Returns true if
le
- Syntax
le(dynamic value1, dynamic value2)
- Returns
- Returns true if
value1
is less than or equal tovalue2
- Returns true if
lt
- Syntax
lt(dynamic value1, dynamic value2)
- Returns
- Returns true if
value1
is less thanvalue2
- Returns true if
not
- Syntax
not(dynamic value)
- Returns
- Returns the negated truth state of
value
. This is the logical negation ofbool(dynamic value)
.
- Returns the negated truth state of
Date functions
date
- Syntax
date(datetime value)
- Returns
- Returns the date portion of
value
by stripping off time information.
- Returns the date portion of
Datetime functions
addDays
- Syntax
addDays(datetime value, integer days)
- Returns
- Returns a datetime that is
days
days after thevalue
. For negative values ofdays
, it returns a datetime that isdays
days beforevalue
.
- Returns a datetime that is
addHours
- Syntax
addHours(datetime value, integer hours)
- Returns
- Returns a datetime that is
hours
hours after thevalue
. For negative values ofhours
, it returns a datetime that ishours
hours beforevalue
.
- Returns a datetime that is
addMinutes
- Syntax
addMinutes(datetime value, integer minutes)
- Returns
- Returns a datetime that is
minutes
minutes after thevalue
. For negative values ofminutes
, it returns a datetime that isminutes
minutes beforevalue
.
- Returns a datetime that is
addSeconds
- Syntax
addSeconds(datetime value, integer seconds)
- Returns
- Returns a datetime that is
seconds
seconds after thevalue
. For negative values ofseconds
, it returns a datetime that isseconds
seconds beforevalue
.
- Returns a datetime that is
addWeeks
- Syntax
addWeeks(datetime value, integer weeks)
- Returns
- Returns a datetime that is
weeks
weeks after thevalue
. For negative values ofweeks
, it returns a datetime that isweeks
weeks beforevalue
.
- Returns a datetime that is
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 functions
concat
- Syntax
string concat(dynamic value1, dynamic value2[, .., dynamic valueN])
- Returns
- Returns a string that is the concatenation of all the supplied values. This function accepts two or more input values.
contains
- Syntax
boolean contains(string haystack, string needle)
- Returns
- Returns true if the
needle
is found anywhere in thehaystack
- Returns true if the
endsWith
- Syntax
boolean endsWith(string haystack, string needle)
- Returns
- Returns true if
haystack
ends with theneedle
- Returns true if
format
- Syntax
string format(string formatString[, dynamic value1[, .., dynamic valueN]])
- Returns
- Returns a string that is built based on the
formatString
with arguments replaced by their corresponding values. Follows the java syntax forprintf()
. See a quick reference here, or a more detailed reference Reference here. - Each % in the format string represents an argument and corresponds to a value (in order of appearance) in the value list. Each argument can have formatting settings, according to the reference provided.
- Returns a string that is built based on the
- Example
-
format('The temperature outside is %.2f degrees Fahrenheit', [Temp sensor:temperature])
outputsThe temperature outside is 71.26 degrees Fahrenheit
- The
%.2f
translates to "format asf
loat and use.2
decimal places.
-
- 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)
outputshel
- Example2:
left("hello world", 7)
outputshello w
- lower(string)
- returns a lower case value of a string
- Example:
lower("HELLO WORLD")
outputshello 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)
returnslo w
- Example2:
mid("hello world", 6, 3)
returnswor
- replace(string, search, replace[, [..], search, replace])
- replaces a search text inside of a value
- Example1:
replace("hello world", "world", "earth")
returnshello 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
startsWith
- Syntax
boolean startsWith(string haystack, string needle)
- Returns
- Returns true if
haystack
starts with theneedle
- Returns true if
- 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