Get Started
Part 8

Block Coding

Block coding implements math, date/time operations, text processing, unit conversions, and more by combining blocks.

General

Block coding can be used in every value input in logic. Clicking the button on the right side of a value input offers four input methods.

  • Direct entry: Enter the value as-is.
  • Variable value: Use another variable's current value.
  • Block editor: Use the result calculated by assembled blocks.
  • Lua script: Write Lua code and use its return value.

All four methods must return a value matching the input's type; a mismatched type causes an error at execution time.

Block Editor

Select Block Editor and click Edit Block to open the editor. The editor consists of a toolbox, where blocks are organized by category, and a workspace, where blocks are connected together.

The Edit Block dialog with the toolbox, workspace, and return block
The Edit Block dialog. The toolbox is on the left, the workspace on the right, and the result connects to the return block.

Working with Blocks

  • The return block is already placed in the workspace and is the only path for sending a result out of the block editor. The type of the value connected to it must match the type of the value input.
  • Most blocks have inputs or outputs (some have neither). Blocks with outputs are dragged and connected to other blocks' inputs.
  • Inputs and outputs have data types, and only matching types can be connected. The data types are the same as variable types.
  • Hovering over a block shows its description as a tooltip; right-clicking shows the available commands.
  • Blocks with a variable number of inputs (if, text join, list creation, local function parameters, and so on) use the block's gear button to add, remove, or reorder entries.
  • Connecting a new block to an input that already holds one pushes the existing block out.
  • Shadow blocks are pre-filled for convenience and are deleted automatically when replaced by another block.
  • A finished assembly must be fully connected; empty inputs or outputs cause errors at execution time.
  • Blocks stacked vertically execute in order from top to bottom, and conditional statements and loops enable more complex calculations.
  • The break/continue block can only be used inside a loop block, and a local function's return if block only inside a local function.
Note Positions and indexes do not start at the same number in every category. Text and lists count from 1 and return 0 when nothing is found, while byte arrays, bits/bytes, JSON, and XML count from 0 (JSON returns -1 when nothing is found).
Note For angles, only the inputs of sin, cos, and tan are in degrees. The outputs of the inverse trigonometric functions, the hyperbolic functions, coordinate conversion, vector angles, and complex arguments are all in radians. Use the angle conversion blocks in the Unit Conversion category to switch between them.

Blocks

The toolbox consists of 18 functional categories. Typing a name or description into the search box at the top of the editor narrows the view to the matching blocks, and each block's description is shown as a tooltip when hovering over it.

CategoryContents
VariablesBlocks that read and write project variables.
LogicConditional statements (if), comparison, and boolean operations.
LoopsCount, condition, counting, and list loops, plus break/continue.
MathArithmetic and mathematical functions.
Date/TimeDate and time calculations and conversions.
TextString manipulation.
Byte ArraysBinary data manipulation.
ListsList creation, lookup, and manipulation.
Type ConversionConversion between variable types.
Unit ConversionLength, pressure, energy, speed, temperature, astronomical distance, angle, coordinate (deg/min/sec), and compass conversions.
ScienceSun and moon rise/set times, azimuth and altitude, psychrometric properties, and standard atmospheric pressure.
Bits/BytesBitwise operations.
Complex/VectorComplex number and vector operations.
JSONJSON parsing and generation.
XMLXML parsing and generation.
ColorColor value manipulation and conversion.
Local VariablesTemporary variables scoped to this block editor.
Local FunctionsFunctions defined and called within this block editor.

Local Variables and Local Functions

Local variables exist only inside the block editor that defines them and are used to hold intermediate results. They are separate from project variables, so other logic and dashboards cannot see them. Blocks are provided to create a variable, write a value, read a value, and add a number. The counting and list loops in the Loops category work by storing the index or item in a local variable.

Local functions are for calculations repeated in several places. Functions with and without a return value can be defined, and input parameters are added with the block's gear button. A defined function is run from another block in the same editor using a call block, which also comes in two forms depending on whether a value is returned. To return a value part-way through a function, use the return if block.

Caution Local variables and local functions are valid only inside the block editor that defines them. They are not available in the block editor of another value input, so make values that must be shared into project variables instead (Part 6).