Overview Formula

This block performs a freely entered calculation on its inputs. Instead of chaining several arithmetic and comparison blocks, the calculation is simply written down as text, for example (E1+E2)/2 or if E1>20 then E2 else E3.

The formula is edited using the button next to the "Formula" parameter. The editor checks the entry as you type and points out where something is wrong; only a valid formula can be applied.

The block works exclusively with the values currently present at its inputs. It remembers nothing, has no loops and accesses nothing else. If more is required, the Lua script is the right tool.


Inputs

TR
Trigger
Starts the calculation when the calculation mode is set to "Only on trigger". The calculation runs on the rising edge, that is when the value changes from 0 to any value other than 0. In the other modes this input has no effect and may be left unconnected.
E1 ... E8
Input 1 to 8
The values the formula works with. These are exactly the names used in the formula. The "Number of inputs" parameter determines how many inputs the block has. An unconnected input supplies 0.

Outputs

AW
Result
The result of the formula.
AB
Result as bit
1 as long as the result is not 0, otherwise 0. Handy for formulas that test a condition, because comparisons always return 1 or 0.
FE
Error
1 as long as the formula cannot be calculated - either because it contains an error or because the calculation does not work out, for example a division by zero. Otherwise 0.

Parameters


Formula
The button opens the editor in which the calculation is entered. The permitted notation is described below.

Number of inputs
How many value inputs the block gets, 1 to 8. The formula may only use inputs that actually exist.

Calculation
When the calculation runs:
  • On input change - as soon as an input value changes. This is the normal case.
  • Only on trigger - only on the rising edge at input TR. Useful when several inputs arrive one after another and the calculation should only run on a complete set of values.
  • Cyclic - at the interval set under "Cycle time", regardless of whether anything has changed.

Cycle time
Interval between two calculations in seconds. Only shown when the calculation mode is set to "Cyclic".

Decimal places
How many decimal places the result is rounded to. -1 means the result is not rounded at all.

On error
What output AW shows while no calculation is possible:
  • Hold last value - the last valid result stays in place.
  • Output 0 - the output goes to 0.
Output FE reports the error in either case.

How a formula is written


Inputs
In the formula the input values are called E1 to E8 - exactly as they are labelled on the block. The trigger input TR is not used in the formula.

Upper and lower case
Does not matter. E1, e1, IF and if are all equivalent.

Numbers
Always with a decimal point: 0.5, not 0,5. In a formula the comma separates the values of a function.

Spaces
May be used freely. E1+E2 and E1 + E2 are the same. Line breaks are allowed as well, so long formulas can be laid out clearly.

True and false
There are no separate truth values. A comparison returns 1 when it applies and 0 when it does not. Conversely, any value other than 0 counts as true in a condition.

Arithmetic

Symbol
Meaning
Example with E1 = 20 and E2 = 8
+
Addition
E1 + E2 gives 28
-
Subtraction
E1 - E2 gives 12
*
Multiplication
E1 * E2 gives 160
/
Division
E1 / E2 gives 2.5
%
Remainder of a division
E1 % E2 gives 4, because 20 divided by 8 goes twice and leaves 4
-
Sign
-E1 gives -20
( )
Brackets
Without brackets, multiplication comes before addition: E1 + E2 * 2 gives 36, whereas (E1 + E2) * 2 gives 56

Comparisons

Symbol
Meaning
Example with E1 = 20 and E2 = 8
<
less than
E1 < E2 gives 0
<=
less than or equal
E1 <= E2 gives 0
>
greater than
E1 > E2 gives 1
>=
greater than or equal
E1 >= E2 gives 1
==
equal
E1 == E2 gives 0. Note: two equals signs, a single one is reported as an error
!=
not equal
E1 != E2 gives 1

Logic

Symbol
Meaning
Example with E1 = 1 and E2 = 0
and
&&
and - both must apply
E1 and E2 gives 0
or
||
or - either one is enough
E1 or E2 gives 1
not
!
not - inverts
not E1 gives 0, not E2 gives 1

Functions

Function
Meaning
Example
min(a,b)
the smaller of the two values
min(E1,E2) with E1 = 18 and E2 = 21 gives 18
max(a,b)
the larger of the two values
max(E1,E2) with E1 = 18 and E2 = 21 gives 21
abs(x)
absolute value, the value without its sign
abs(E1-E2) with E1 = 18 and E2 = 21 gives 3
round(x)
round to the nearest whole number
round(E1) with E1 = 21.6 gives 22
floor(x)
round down to the next whole number
floor(E1) with E1 = 21.6 gives 21
ceil(x)
round up to the next whole number
ceil(E1) with E1 = 21.2 gives 22
sqrt(x)
square root
sqrt(E1) with E1 = 16 gives 4. A negative value sets output FE
pow(a,b)
a to the power of b
pow(E1,2) with E1 = 3 gives 9
limit(x,lo,hi)
restrict a value to a range
limit(E1,0,100) gives 100 for E1 = 137, 0 for E1 = -5 and 42 for E1 = 42
pi
the number pi, 3.14159...
pi*pow(E1,2) with E1 = 2 gives 12.566

Conditions


Structure
if condition then value else value

If the condition applies, the value after then is used, otherwise the one after else. The else part must always be given - the block has to output a value in every case.

Simple example
if E1 > 20 then 1 else 0
Gives 1 as soon as E1 rises above 20, otherwise 0.

Several stages
if E1 > 30 then 3 else if E1 > 20 then 2 else 1
Gives 3 above 30, 2 between 20 and 30 and 1 otherwise. Conditions can be nested as deeply as required.

Only the applicable branch is calculated
if E2 == 0 then 0 else E1 / E2
If E2 equals 0, the part after else is not calculated at all. This is a clean way to protect a division without output FE reacting.

Practical examples

Task
Formula
Explanation
Average of two sensors
(E1+E2)/2
E1 and E2 are the two room sensors. The brackets matter - without them only E2 would be halved.
Temperature difference without a sign
abs(E1-E2)
Always returns the positive distance between flow and return, no matter which value is larger.
Restrict a setpoint
limit(E1,18,26)
Forces the setpoint entered through the visualisation into a sensible range.
Convert a control value to per cent
limit(E1*10,0,100)
Turns a value from 0 to 10 into a percentage from 0 to 100. The limit catches outliers.
Release only when two conditions apply
E1 > 200 and E2 < 25
Gives 1 when the brightness is above 200 and the temperature is below 25 - otherwise 0. Output AB can be used directly as a release signal.
Switch between two setpoints
if E3 then E1 else E2
E3 is the operating state: when it is set, the comfort setpoint E1 applies, otherwise the setback setpoint E2.
Fill level in per cent
limit((E1-E2)/(E3-E2)*100,0,100)
E1 is the measured value, E2 the value when empty and E3 the value when full. The result is restricted to 0 to 100.
Protect a division
if E2 == 0 then 0 else E1/E2
Prevents output FE from reacting when the divisor is temporarily 0.

Messages in the editor

For every error the editor names the position where it occurs and highlights it in the text. Only a formula without errors can be applied with OK.
Message
Example
The correct way
The formula is empty
(no text)
E1
This character does not belong in a formula
E1 $ E2
E1 * E2
Unknown name
value(E1)
abs(E1) - only the functions listed above are allowed
This input does not exist
E1 + E3 with two inputs
Raise the "Number of inputs" parameter to 3 or write E1 + E2
A value is missing here
E1 +
E1 + E2
A bracket is missing or superfluous
(E1+E2
(E1+E2)
The function expects a different number of values
min(E1)
min(E1,E2)
"then" is missing after the condition
if E1 2
if E1 then 2 else 0
"else" is missing for "then"
if E1 then 2
if E1 then 2 else 0
There is something after the formula
E1 E2
E1 + E2 - the operator is missing here
The formula is too extensive
very long formulas
Formulas with hundreds of calculation steps are too extensive. Split the calculation across several formula blocks

Errors during operation

Some errors only appear during the calculation because they depend on the input values. In these cases output FE goes to 1 and output AW behaves as set by the "On error" parameter. As soon as the calculation succeeds again, FE returns to 0 by itself.
Cause
Example
Remedy
Division by zero
E1 / E2 with E2 = 0
if E2 == 0 then 0 else E1/E2
Remainder of a division by zero
E1 % E2 with E2 = 0
if E2 == 0 then 0 else E1%E2
Square root of a negative number
sqrt(E1) with E1 = -4
sqrt(abs(E1)) or if E1 < 0 then 0 else sqrt(E1)
Result too large
pow(E1,E2)
With very large values the result can no longer be represented. Check the value range of the inputs and restrict the result with limit

Limitations

The formula works exclusively with the values currently present at the inputs. It remembers nothing from one calculation to the next, has no loops and accesses neither addresses nor the time of day. Where that is needed, the Lua script is the right tool - that is what it is made for.

See also the general parameters of all function blocks.