# Expressions

## Expressions

Expressions are used in smart rules “Equation” and “Formula”.

Extra functions can be used on Modbus devices to read/write values from/to Modbus registers.

## Mathematical expressions

### +, -, *, /

``(20.5 + 9.5) / 2    ``(15)``

## Logical expressions

### AND, OR, !, =, >, <

``(!IsRaining OR (Wind>30))``

## Functions

### IF

``IF(logical_expression, value_if_true, value_if_false)``

Returns one value if a logical expression is ‘TRUE’ and another if it is ‘FALSE’.

### LINEAR

``LINEAR(input, value1_input, value1_output, value2_input, value2_output)``
``Example 1:`` LINEAR(250, 0, 0, 50, 500)   ``(25°C)``
``Example 2: ``LINEAR(5, 0, 400, 10, 3000)``
``          (5V input from CO2 sensor (0V=400ppm, 10V=3000ppm) results in 1700ppm)``

### SWITCH

``SWITCH(expression, case1, value1, [case2, ...], [value2, ...], default_value)``

Tests an expression against a list of cases and returns the corresponding value of the first matching case, with a default value if nothing else is met.

### MIN

``MIN(value1, value2)``

Returns the smaller of the two values.

### MAX

``MAX(value1, value)``

Returns the larger of the two values.

## Bit operations

### <<   (Left Bit Shift)

``8 << 2   ``(32)``

### >> (Right Bit Shift)

``32 >> 2   ``(8)``

### & (Bitwise and)

``3 & 1   ``(1)``

### | (Bitwise or)

```2 | 1 (3) ```

Expressions can also interpret hexadecimal numbers. Prefix 0x is required and the rest is not case sensitive.

``0x0A   (10)``
``0xA0A0   (41120)``
``0xa0a0   (41120)``

## Modbus expressions

### Register types

• C – Coil
• SC – Single Coil
• D – Discrete input
• H – Holding register
• SH – Single holding register

The ‘S’ register types are only for write functions and should be used for devices which do not support writing to multiple registers at once.

### Data types

• Int16 (-32,768 to 32,767)
• Uint16 (0 to 65,535)
• Int32 (-2,147,483,648 to 2,147,483,647)
• Uint32 (0 to 4,294,967,295)
• Bool – (-inf, 0> = false, (0, inf) = true
• BigEndianInt16
• LittleEndianInt16
• BigEndianUint16
• LittleEndianUint16
• BigEndianInt32
• LittleEndianInt32
• BigEndianUint32
• LittleEndianUint32

``````MODBUSR(register_type, register_address, data_type)
Example: MODBUSR(H, 20, Int16)/100``````

Returns value of holding register on address 20 in int16 format and divides it by 100.

### MODBUSW (modbus write)

``````MODBUSW(register_type, register_address, data_type, value_to_write)
Example: MODBUSW(H, 20, Int16, Te/100)``````

Writes the temperature of the sensor divided by 100 int int16 format to holding register 20.

### MODBUSWNE (modbus write not equal)

Writes to the register only if the current value is different. Reads the value, checks if equal and writes to the register if not. Useful for saving EEPROM memory.

``````MODBUSWNE(register_type, register_address, data_type, value_to_write)
Example: MODBUSWNE(H, 20, Int16, Te/100)``````

First reads the value from holding register on address 20 in int16 format, compares it to the temperature sensor temperature divided by 100 and only if the values are different writes the temperature of the sensor divided by 100 int int16 format to holding register 20.