Skip to content

Expressions

Overview

Reactor has two types of variables: Rule-based and Global. Each variable is given a name, which must be unique within its context (that is, a rule-based variable cannot have the same name as another variable in the same rule).

Rule-based variables are those that are defined on a rule. These variables have evaluation scope limited to the rule; that is, their container variable can be referenced in other variables' expressions within the rule, and they can reference Global Variables, but cannot refer to or modify variables in other rules (i.e. they are local within the scope of the rule that defines them). Rule-based variables are evaluated whenever a dependency of the rule triggers an evaluation of its conditions, and the variables are evaluated prior to the rule's trigger and constraint conditions being evaluated. They are evaluated in the order in which they are shown in the Expressions section of the rule.

Global variables are visible to all rules in the system, and can be referred to by any rule-based variable or any other global variable. Global variables are evaluated a bit differently from local variables. A global variable is evaluated when a rule that refers to the global variable in its own (rule-based) expressions or conditions is evaluated, or when a reaction is run that contains actions with substitutions referring to the global variable. In addition, the normal configuration of global variables is "auto-evaluation," where a global variable is recalculated if it contains references to another global variable or entity that has changed. Because it can be desirable to have some variables only update on demand, auto-evaluation can be turned off for any global variable. See Consequences of Disabling Auto-Evaluation, below.

It is possible for a rule-based variable to have the same name as a global variable. If another expression in the rule refers to that name, it will access the rule-based variable, being local in scope, in preference to the global variable (as it would be in most computer languages). Some users adopt naming conventions like prefixing the names of their global variables to avoid confusion/conflict (e.g. start them with g_ or simply g).

If you delete a variable, any rule, reaction or other expression that refers to it will not be able to resolve it. You can re-create the variable using the same name after having deleted it, but if that name is used in a rule (directly or by reference through expressions), you will need to disable and re-enable the rule (or reset it) to get the rule to watch the new replacement variable.

Expression Language Syntax

What it's not...

The expression language is not JavaScript, and definitely not an eval() running inside the code. The entire reason for a separate expression language is specifically to avoid this potential security tragedy, and to provide convenience in both syntax and environment that are tuned for IoT applications. Likewise, it is not C, C++, Java, Python, or Lua. And for those of you that may be expecting, or hoping, that is entirely the same as the expression language used in the Reactor plugin for Vera, well... it's not that either.

Reactor's expression syntax is similar to the "infix" expression syntax used by most common languages (C, Java, JavaScript, etc.). The simplest expression is simply a numeric constant, such as 1234. This is a complete expression, the result value of which is 1234. Negative numbers begin with a - sign, such as -1234. Numbers may have decimal points and decimal digits: -12.34. Numbers may also be given in scientific format: 1.234e3 is equal to 1234 (1.234 x 103). Hexadecimal integers may be entered by prefixing with 0x; for example, 0x20 is decimal 32. Likewise binary integers can be prefixed with 0b, and octal with 0o.

Strings are represented as characters surrounded by matching double-quotes ("), single quotes ('), or back-ticks (`). The quote character used to open a string must be used to close the string, and the other quote characters may be used within the string. For example, "This is a 'valid' example" and '"this" is too'.

Boolean values true and false are represented by the reserved words true and false, respectively.

The reserved word null evaluates to the null value (basically means "no value").

Identifiers are names that represent values (variables). An identifier must begin with an upper- or lowercase alphabetic character, and may follow with any combination of alphanumeric characters and underscore. Thus myLastSignal is a valid identifier, but 023lastSignal is not, and nor is just another name!.

Functions are identifiers followed by a paren-enclosed list of expressions as its arguments (or empty for no arguments). The maximum value of a series of numbers, for example, can be found using the max function like this: max( 1, -2, pi, lastElement ).

The expression language includes a set of operators. Multiplication is performed by *, so that 3 * 4 yields 12. Division uses /, while addition and subtraction use + and -, respectively, as one might expect. The full list of operators is given below, in order of precedence. Operators with higher precedence are performed before operators with lower precedence, so that expressions like 3 + 4 * 2 yield 11, not 14. The precedence of mathemetical operators follows the Order of Operations we are taught in elementary school. Precedence can be controlled using parentheses, so per the previous example, the result 14 could be arrived at using (3 + 4) * 2.

In addition to the mathematical operators, there are relational operators: ==, !=, >, >=, < and <= all return true if their operands are equal, not equal, etc. In addition, the two special relational operators === and !== check equality/inequality not just of value, but of data type, such that "3" == 3 is true, but "3" === 3 is false (because the left operand is string type, and the right a number).

Note that equality/inequality comparison of arrays or objects (non-primitive types), such as array1 == array2 does not perform a "deep inspection" of the array/object and is effectively not a valid comparison for most practical purposes. Rather, it determines if the two operands are the same object in memory (as JavaScript does), and thus is most likely false and not truly a relevant comparison. Specifically, the following expressions are expected to be false: [1,2,3] == [1,2,3], { abc:1, def:2 } == { abc:1, def:2 } because although they are equivalent in terms of their contents, they are not, in fact, the same object in memory. The more complex s=[1,2,3], t=s, s == t is true, however, because s and t do refer to the same object in memory.

The boolean operators are && for and and || for or, such that false && true is false and false || true is true. The ! unary boolean operator negates its right-side operand, so !true is false.

The bitwise operators, following "C" (and Java, and JavaScript, and others) are & for bitwise AND, | for bitwise OR, and ^ for exclusive-OR (XOR).

The array element accessor is square brackets [] and should contain the array index. Arrays in Reactor expressions are zero-based, so the first element of an array is [0]. If the index given is less than 0, a runtime error occurs. If the index is positive or zero but off the end of the array, null is returned.

The member access operator "dot" (.) is used to traverse objects. For example, referring to the power state of an entity may be entity.attributes.power_switch.state, which starts with an entity object, drops to the list of attributes within it, and the "power_switch" capability within the attributes, and finally to the "state" value. The right-side operand of the dot operator must be an identifier, so it may not contain special characters. If a member name contains any non-identifier characters, the array access syntax can be used: entity.attributes['forbidden-name'].value.

The ternary operator pair ? : common to C, C++ and Java (and others) is available: <boolean-expression> ? <true-expression> : <false-expression>. If the result of the boolean-expression given is true, the true-expression is evaluated; otherwise, the false-expression is evaluated.

The coalesce operators are ??, ?#, ?. and ?[. Coalesce operators help handle null values in the middle of complex expressions more gracefully. For example, value ?? 0 will result in the value of the variable value if it is not null, but if it is null, will yield 0. The numeric coalesce operator ?# provides a quick test if the left operand is (or can be) a number (integer or real), and if so returns the numeric value; if not, it returns the right operand (which can be any type). The access coalesce operators are used for object member and array element access: if an identifier struct is intended to hold an object, but turns out to be null, a reference to struct.name in an expression would throw a runtime evaluation error; using struct?.name will instead result in null with no exception thrown. This is convenient because you can carry it down?.a?.long?.list?.of?.member?.names without crashing if something is undefined. Likewise if beans was intended to be an array but ended up null, the expression beans[2] would throw an error, while beans?[2] would result in null.

The in operator is used to establish if an object contains a specified key (e.g. key in obj) or an array contains an element at the given index (e.g. 15 in arr). It is important to note that this operator works on keys only, not values, and in particular, cannot be used to search an array for a value (i.e. 4 in [ 4, 5, 6 ] is always false). To find an array element, use the indexOf() function. The first statement can be used to find a value in an object.

The .. range operator produces an array containing all integers from the left operand to the right, so 3..6 results in [3,4,5,6]. A for-style counting loop can be implemented using each with the range operator as its operand: each i in 0..9: <statement> would execute <statement> 10 times.

Multiple expressions can be chained together by separating them with a comma. The result of a chained expression is the last expression evaluated.

The following is the list of operators supported in order from lowest precedence to highest. Operators on the same line have equal precedence and are evaluated left-associative (from left to right) unless otherwise indicated:

  • , (chain, right associated, effectively)
  • = (assignment, right associative)
  • ? (ternary operator first)
  • : (ternary operator second)
  • ?? (coalesce), ?# (coalesceNaN)
  • ||, or (logical OR)
  • &&, and (logical AND)
  • | (bitwise OR)
  • ^ (bitwise XOR)
  • & (bitwise AND)
  • ==, ===, !=, !== (equality/inequality, non-associative)
  • <, <=, >, >= (comparison, non-associative)
  • <<, >> (bit shift)
  • +, -
  • *, /, % (mod)
  • ** (power, right associative)
  • - (unary minus)
  • !, not (not/negation, right-associative)
  • ., ?., ?[ (member access)

Data Types

The data types known to the expression language are boolean, number, string, array, object, and the following special type/values (both a type and a value):

  • null, which basically is used to mean "no value";
  • NaN, which stands for "Not a Number", which results when a conversion to number fails (e.g. 5 * "hello" or int( 'what is this?' ));
  • Infinity, which results from division by zero and other similar math failures.

Arrays and objects can be constructed and used on the fly: [ 5, 99, 23, 17 ] constructs a four-element array, while { name: 'spot', type: 'dog', weight: 33 } constructs an object.

Statements

The expression language has a couple of "lightweight statements" that function as a hybrid of a statement and an expression. These are:

  • each <element-identifier> [, <element-identifier> ] in <array-or-object-expression>: <bodyexpression> — the each statement will iterate over the given array or object (or expression resulting in an array or object), each time placing an array value or object element in the named variable (and the key or index in the second named variable, if given), and then execute the body expression. The body expression result, if non-null, is pushed to an array that forms the each expression result. For example, each num of [ 4,7,33 ]: num * 2 will return an array [ 8, 14, 66 ], while each v,k in { "alpha": 1, "beta": 2 }: k will return ["alpha", "beta"].
  • first <element-identifier> [, <element-identifier> ] in <array-or-object> with <test-expression> [ : <result-expression> ] — the first statement will search through the elements of an array or object (top level, no traversal) and return the first value that for which <test-expression> is true (or truthy). The result is the value matched, unless the optional : <result-expression> clause is given, in which case the result will be that of the expression. Example: first val,key in devices with val.type=="window": val.name + ' ' + key will find the first device in an array or map (object) of device objects for which the device object key type equals the string window, and rather than return the device object, return the device name and key as a space-separated string.
  • do <statement-list> done — since the limited syntax of each and first allow only a single expression to be executed in their loop bodies, the do...done statement creates a "compound expression" that wraps a series of expressions into one, thus them to be executed within the loop. The standard multi-statement result rule applies: the result of the statement block is the result produced by the last expression in the block.
  • For users uncomfortable with the ternary operator (? :) syntax, an if <conditional> then <true-expression> else <false-expression> endif statement may be used. The true- and false-expressions may be a do...done block.
  • define name([arguments]) expression — defines name as a function implemented by the given expression. The optional arguments is a comma-separated list of identifiers to receive passed-in values. For example: define circumferece( r ) 2 * r * 3.14159265 would define the function circumference to return the circumference of a circle given its radius. Thereafter, the expression circumference( 10 ) would return 62.831853.

Functions

Arithmetic Functions

  • abs( number ) — returns the absolute value of its argument;
  • sign( number ) — returns the sign of its argument: -1 if negative, 0 if zero, 1 if positive;
  • floor( number ) — returns the largest integer less than or equal to its argument;
  • ceil( number ) — returns the next integer greater than or equal to its argument;
  • round( number, precision ) — rounds number to precision decimal digits;
  • trunc( number ) — returns the integer portion of its argument (e.g. trunc(-3.4) is -3, where floor(-3.4) is 4);
  • cos/sin/tan( radians ) — trig operations;
  • log/exp( number ) — natural logarithm and exponential;
  • pow( base, power ) — raises base to the powerth power (e.g. pow(10,3) is 1000);
  • sqrt( number ) — square root (of number > 0);
  • random() — returns a random number greater than or equal to 0 and less than 1;
  • min/max( ... ) — returns the smallest/largest value of its arguments; if an argument is an array, the array is scanned; non-numeric values are ignored, so these functions return null unless at least one number (type) value is found;
  • isNaN( various ) — returns true if the argument is non-numeric.

String Handling Functions

  • len( string ) — returns the length of the string;
  • substr( string, start, length ) — returns the portion of the string from the startth character for length characters;
  • upper/lower( string ) — converts the string to upper/lower-case;
  • match( string, regexp [ , ngroup [ , flags ] ] ) — matches, if possible, the regular expression regexp to the string, and returns the matched string, or null if no match; if ngroup is given and the regexp contains groups, the matched part of that group is returned; if flags is "i", a case-insensitive match is done;
  • find( string, regexp [ , flags ] ) — like match(), but returns the index of the first character of the match, rather than the matched string, or -1 if no match; the meaning of (optional) flags is the same as for match();
  • replace( string, regexp, replacement [ , flags ] ) — replaces the first substring matched by the regular expression regexp with the replacement string and returns the result; the optional flags (a string) may include "i" for case-insensitive search, and "g" for global replacement (all matches in string are replaced; combined would be "ig"); the $ is a special character in the replacement string and follows the JavaScript semantics (for String.replace()).
  • rtrim/ltrim/trim( string ) — removes whitespace from the right/left/both side(s) of the string;
  • split( string, regexp [, max ] ) — splits the string at the matching regular expression and returns an array (e.g. split( "1,5,8", "," ) returns ["1","5","8"]).

Type Handling Functions

  • int( various ) — attempts conversion of its argument to an integer; returns NaN if the argument cannot be converted, otherwise, it returns the integer;
  • float( various ) — attempts conversion to a floating-point value;
  • bool( various ) — attempts conversion to boolean; in this expression language, the strings "0", "no", "off" and "false", the empty string, the number 0, and boolean false all result in false; otherwise, the result is true;
  • str( various ) — converts the argument to a string;
  • isnull( various ) — more a test than a conversion, returns true if the argument is null.
  • typeof( various ) — returns the data type of the argument. While JavaScript reports arrays and null as objects, lexpjs reports them more specifically as array and null respectively.

Date/Time Handling Functions

  • time( [ year [, month [, day [, hour [, minute [, second ]]]]]] ) — returns the current time if no arguments are given; otherwise a date/time is constructed using as many arguments as are provided; the result is a Unix Epoch time in milliseconds;
  • dateparts( [time] ) — returns an object with keys year, month, day, hour, minute, second, and weekday (0-6, 0=Sunday) for the given time, or the current time if not given.

Important notes with respect to date handling (currently; this will evolve):

  • The numbering of months is 1-12 in Reactor (not 0-11 as in JavaScript);
  • All time functions operate in the timezone set for the runtime. There are currently no UTC functions.

Array/Object Handling Functions

  • len( array ) — returns the number of elements in the array;
  • keys( object ) — returns, as an array, the keys in the given object;
  • values( object ) — returns, as an array, the values in the given object;
  • clone( various ) — returns a (deep) copy of its argument; this is particularly useful for arrays and objects;
  • join( array, joinstring ) — returns a string with the elements of array converted to strings and joined by joinstring (e.g. join([4,6,8], ":") results in the string "4:6:8", while join([9], ":") would be simply "9");
  • list( ... ) — returns an array of its arguments; this is legacy syntax (i.e. list(5,7,9) is the same as writing [5,7,9], so this function is now obsolete and may be removed later);
  • indexOf( array, value ) — if value is present in array, the index (>=0) is returned; otherwise -1 is returned;
  • count( array ) — returns the number of non-null elements in array;
  • sum( array ) — returns the sum of non-null elements of array; note that only a single argument, which must be an array, is accepted;
  • median( array ) — returns the statistical median of the elements of array;
  • slice( array, start, end ) — returns a new array containing the elements of array from start (zero-based) to, but not including, end;
  • insert( array, pos, newElement ) — inserts newElement into array before pos (zero-based); the array is modified in place and is also returned as the function value;
  • remove( array, pos [ , numRemove ] ) — removes elements from array starting at pos; if numRemove is not given, only the one element at pos is removed, otherwise numRemove elements are removed from the array; the array is modified in place and also returned as the function value;
  • push( array, value [ , maxlen ] ) — appends value at the end of array; if maxlen is given, elements are removed from the head of the array to limit its length to maxlen elements; the array is modified in place and also returned as the function value;
  • unshift( array, value [ , maxlen ] ) — insert value at the beginning of array; if maxlen is given, elements are removed from the end of the array to limit its length to maxlen elements; the array is modified in place and also returned as the function value;
  • pop( array ) — removes the last element of array and returns it; returns null if array is empty; the array is modified in place;
  • shift( array ) — removes the first element of array and returns it; returns null if array is empty; the array is modified in place;
  • arrayConcat( a, b ) — returns a new array that is the concatenation of a and b; for example, arrayConcat( [1,2,3], [1,3,5] ) returns [1,2,3,1,3,5];
  • arrayIntersection( a, b ) — returns a new array containing all values in array a that are also in array b; for example, arrayIntersection( [1,2,3], [1,3,5] ) returns [1,3];
  • arrayDifference( a, b ) — returns a new array containing all values of array a that do not appear in array b; for example, arrayDifference( [1,2,3], [1,3,5] ) returns [2];
  • arrayExclusive( a, b ) — returns a new array containing all values of the arrays a and b that appear only in either, but not both (this is often referred to as the symmetric difference); for example, arrayExclusive( [1,2,3], [1,3,5] ) returns [2,5];
  • arrayUnion( a, b ) — returns a new array containing all values of the arrays a and b; for example, arrayUnion( [1,2,3], [1,3,5] ) returns [1,2,3,5];
  • sort( array [, comparison] ) — sort the given array, returning a new array (the given array is not modified). The given array may contain data of any type. The default sort is a case-insensitive, ascending, (host) locale-aware string sort (so the array is assumed to contain strings, and if it contains any other type the values are coerced to strings prior to comparison). To sort differently (e.g. descending, numeric, etc.), comparison can be given as the either the name of a defined function taking two arguments as the values to be compared, or an expression that compares the local variables $1 and $2 (defined by the sort() function as it runs). In either case, the result must be an integer: 0 if the two values are equal; less than 0 (e.g. -1) if the first value sorts before the second; or greater than zero (e.g. 1) if the first value sorts after the second. The comparison must be stable: given two values, it must return the same result every time it runs. Do not apply randomness or other heuristics to the comparison that may violate this requirement, as this can lead to long runtimes or even infinite loops in the attempt to sort. Example: sort an array of numbers descending (largest values first): sort( numbers, $1 == $2 ? 0 : ( $1 < $2 ? 1 : -1 ) )
  • isObject( various ) — returns true if the argument is an object;

Conversion Functions

  • hex( num ) — returns the hexadecimal (string) representation of the numeric argument (or "NaN" if non-numeric);
  • toJSON( various ) — returns the argument as a JSON-formatted object (string);
  • parseJSON( json ) — returns the data represented the (parsed) JSON string argument;
  • btoa( str ) — returns the Base64-encoded representation of the string argument;
  • atob( str ) — returns a string containing the decoded Base64 argument (string).
  • urlencode( string ) — URL-encodes the given string.
  • urldecode( string ) — URL-decodes the given string.

Special Reactor Functions

  • getEntity( id ) — returns the entity matching id, which may be an entity ID, a canonical entity ID, or an entity name. When using anything other than the canonical ID, the first matching entity found is returned. Searching by entity name can be somewhat canonicalized by preceding the name with the controller ID, as in "vera>Hall Motion Sensor". This function retrieves entities that are otherwise not in context. The result is an object with the entity's data, or null. Expressions of the form getEntity( "vera>Hall Light Switch" ).attributes.power_switch.state are a typical form of use.
  • matchEntities( criteria ) — returns an array of canonical IDs of entities matching the criteria specified. The criteria may be either a string containing comma-separated capability names, or an object with any of the following keys: controller, capability, group. When using the object form, the value for each key may be either a string or an array of strings. For example, { controller: 'vera', capability: [ 'binary_sensor', 'value_sensor' ] } would return all entities from the controller with ID vera that provide the binary_sensor or value_sensor capability. The intent of this function is to rapidly produce a reduced list of entities that can then, if necessary, be further filtered using each statements. Building on the example:

    sensors = matchEntities( { controller: 'vera',
      capability: [ 'binary_sensor', 'value_sensor' ] } )
    
    low_battery = each e in sensors:
                    getEntity( e ).attributes.battery_power?.level < 0.5 ? e : null
    

    The final result of the low_battery expression would be all of the sensors entities found by matchEntities() that have the battery_power capability and a battery level under 50%. Note that this isn't a particularly good example for battery state tracking. More likely, you'll want to match all devices that have the battery_power capability (e.g. matchEntities( "battery_power" ) ), not just the sensor types shown.

  • performAction( entity_id, action, args ) — perform an action on the entity identified by entity_id. This function always returns null (even if there is an error). The action argument must be a string containing the name of an action in a capability supported by the device (e.g. media_player.mute or dimming.set). The args argument must be an object with key/value pairs corresponding to the action's required parameters. Example to set a dimmable light to 25%: performAction( 'hub>Hallway Light', 'dimming.set', { level: 0.25 } )

  • isRuleSet( rule_id ) — returns true if the specified rule is set, false if reset, and null if it does not exist.
  • isRuleEnabled( rule_id ) — returns true if the specified rule is enabled, false if disabled, and null if it does not exist.
  • fileExists( name ) — queries the file system and returns true if the file exists in storage/userfiles directory in the Reactor installation; false otherwise.
  • fileRead( name [, options ] ) — reads the named file in the storage/userfiles and returns its content. The optional options object may contain the following keys: encoding to set an encoding (UTF-8 is the default if not otherwise specified); json, when boolean true, causes the file contents to be parsed as a JSON object and returned; lines, when boolean true, returns the file contents as an array, one line per element. This function returns null on any error.
  • fileWrite( name, data [, options ] ) — writes the named file in storage/userfiles. The data must be a string; to write any other type, you can convert it to JSON first by wrapping the value/variable in a toJSON() call (e.g. fileWrite( 'test.json', toJSON( obj ) )). The optional options argument must be an object containing any combination of the following keys: encoding for the encoding type (UTF-8 is the default); append, if boolean true, causes the contents to be appended to the file; mode to set the file mode if the file is created (default 0660, read/write for owner and group, none for world).
  • strftime( format_string [, time ] ) — Formats time to a human-readable form controlled by format_string. If time is not given or null, the current time is used. The format string implements most of the specifiers and options of the Unix date command. The following patterns are supported:

    • %% — insert a literal "%" into the string
    • %a — inserts the abbreviated day of the week (e.g. "Mon")
    • %A — inserts the full day of the week (e.g. "Monday")
    • %b — inserts the abbreviated month name (e.g. "Feb")
    • %B — inserts the full month name (e.g. "February")
    • %c — equivalent to %a %b %e %T %z %Y, which produces output in the same form as the Linux date command (e.g. "Fri 27 Aug 2021 12:05:24 PM -0400")
    • %C — inserts the two-digit century number
    • %d — inserts the two-digit day of the month (01-31)
    • %D — date, equivalent to %m/%d/%y
    • %e — inserts the day of the month, space padded (e.g. day 3 is " 3")
    • %f — inserts the number of milliseconds (0-999)
    • %F — equivalent to %Y-%m-%d
    • %H — two-digit (24-hour) hour (00-23)
    • %I — two-digit (12-hour) hour (01-12)
    • %j — day of year (three-digits, zero-filled) (000-366)
    • %k — hour (24-hour), space padded (e.g. 4am = " 4", 4pm = "16")
    • %l — hour (12-hour), space padded (e.g. 4am = " 4", 4pm = " 4")
    • %m — month, two-digit zero filled (01-12)
    • %M — minutes, two-digits zero filled (00-59)
    • %p — "AM" or "PM"
    • %P — "am" or "pm"
    • %r — equivalent to %I:%M:%S %p
    • %R — equivalent to %H:%M
    • %s — Unix Epoch timestamp (with fractional milliseconds)
    • %S — seconds, two-digit zero filled (00-59)
    • %T — equivalent to %H:%M:%S
    • %u — day of week, numeric (1=Monday, 7=Sunday)
    • %w — day of week, numeric (0=Sunday, 6=Saturday)
    • %x — locale-specific date representation (result depends on locale configuration of the host)
    • %X — locale-specific time representation (result depends on locale configuration of the host)
    • %y — two-digit year, zero-filled (00-99)
    • %Y — four-digit year
    • %z — timezone offset (e.g. +/-hhmm, like -0500)

    Additionally, the Unix date command's formatting options (0, _, -, ^) are supported, as well as the width modifier. Examples:

    • %k at 4am → " 4" (default with no options, has leading space)
    • %02k at 4am → "04" (now same result as %H)
    • %-k at 4am → "4" (has leading space removed)
    • %H at 4am → "04" (default with leading zero)
    • %_H at 4am → " 4" (leading zero replaced with space, now same as %I)
    • %8y in 2021 → "00000021" (nonsensical, but illustrates field width specification with default padding)
    • %A → "Friday"
    • %^A → "FRIDAY"

    Note that the strftime() function is locale-aware, so if you are using a non-English locale, the time "words" (names of months and days of the week, for example) will be in the language defined by the active locale.

  • format( format_string [, arg1 [, arg2 [, ...]]] ) - Formats format_string according to its substitutions, inserts argn values where indicated. The general form for substitution is {n} where n is the (zero-based) argument number (e.g. {0} would insert arg1's formatted value). The argument number can be omitted (e.g. {}), and the next argument will be used. Optionally, the argument number can be followed by one of the format specifiers shown below, separated from the argument number by a colon (:). The format specifier may be preceded by options, given in the following order with any allowed to be omitted: an optional zero-padding flag (0), an optional alignment indicator (<, > or ^ for left aligned, right aligned, or centered, respectively), an optional field width (if not given, the field is as wide as the argument value needs), and for floating-point format specifiers (e, f and g) a dot (.) followed by precision (number of digits). For example, "{2:>9.2f}" would format the third argument as a floating point value in a 9-character field, using two digits precision right of the decimal, right justified in the string (padded with spaces to make it 9 characters), so 23.169 would be formatted as "⋅⋅⋅⋅23.17" (for visibility here each "⋅" represents a space in the result string); {0:06d} would format a number in the first argument as six digits zero filled (e.g. 123 would be "000123"). The format specifiers available are:

    • s — plain string (this is the default format if not given, e.g. {0}
    • q — format as a quoted string (if the argument contains quotes, they are escaped in the result string)
    • d — decimal integer form (argument must be numeric)
    • b — binary integer form (argument must be numeric), e.g. value 15 would result in "1111"
    • o — octal integer form (argument must be numeric), e.g. 167 would result in "247"
    • x — hex, lower-case (argument must be numeric), e.g. 167 would result in "a7"
    • X — hex, upper-case (argument must be numeric), e.g. 167 would result in "A7"
    • f — fixed-point floating point, e.g. {0:.2f} given pi would result in "3.14"
    • e — exponential (scientific) form, e.g. {0:.4e} given 123,456 would result in "1.2346e+5"
    • g — general, switches between f and e as needed for best readability (estimated)
    • % — formats number as a percentage, e.g. 0.15 would format as "15%"

    Examples (spaces shown as "⋅" for clarity only):

    • format( "Temp is {0}F", 72.33178 ) → "Temp⋅is⋅72.33178F"
    • format( "Temp is {0:.1f}F", 72.33178 ) → "Temp⋅is⋅72.3F"
    • format( "Temp is {0:8.3f}F", 72.33178 ) → "Temp⋅is⋅⋅⋅72.332F"
    • format( "Temp is {0:08.3f}F", 72.33178 ) → "Temp⋅is⋅0072.332F"
    • format( "Temp is {0:<8.3f}F", 72.33178 ) → "Temp⋅is⋅72.332⋅⋅F"
    • format( "In order: {} {} {} {}", "a", "b", "c", "d" ) → "In order: a b c d"
    • format( "Mixed: {2} {} {0} {}", "a", "b", "c", "d" ) → "Mixed: c d a b"

Reserved Words

As a result of the syntax, the following words are reserved and may not be used as identifiers or function names: true, false, null, define, each, in, first, of, with, if, then, else, endif, do, done, and, or, not, NaN, Infinity. Note that keywords and identifiers are case-sensitive, so while each is not an acceptable identifier, Each or EACH would be.

Consequences of Disabling Auto-Evaluation

Auto-evaluation is a feature of Global Variables under which they are updated automatically if they refer to another Global Variable or entity that changes.

Consider a Global Variable named sensor_updated that is defined as getEntity( 'hubitat>76' ).lastupdate. With auto-evaluation enabled (the default), this global variable will be re-evaluated and updated whenever the specified target of the getEntity() function changes. However, if auto-evaluation is turned off for this variable, then it will only be updated when a rule or reaction that references it is run.

Now let's consider what happens if we create another (global) variable called update_timestamp with the expression strftime( "%T", sensor_updated ). This global variable is now dependent on sensor_updated. So we have now created a dependency chain:

    entity               global var                global var
  hubitat>76   ---->   sensor_updated   ---->   update_timestamp
      ^                      ^                        ^
      |                      |                        |
  when this                this                    and so
   changes                changes                 does this

In the default case, with auto-evaluation enabled, a change to the entity will cause sensor_updated to be re-evaluated, and if its value changes, then update_timestamp will also be re-evaluated.

But, if auto-evaluate is disabled for sensor_updated, we break the chain:

  hubitat>76     X     sensor_updated   --->   update_timestamp
      ^          ^-----(auto-eval off)                ^
      |                      ^                        |
      |                      |                        |
  when this               this does             and so neither
   changes                NOT change              does this

In this case, a change to the entity does not update sensor_updated, and thus update_timestamp will also not be updated. If a rule or reaction later references sensor_updated, however, then it will be updated at that moment, and update_timestamp will also be updated, because it is dependent on sensor_updated (unless, of course, auto-evaluate is also turned off for update_timestamp as well).

So keep in mind that turning off auto-evaluation on a global variable doesn't just affect that variable, but will break a chain that affects all values derived from that variable as well.

Updated: 2021-Nov-27