Skip to main content

Data Tables

Data Tables store rows of structured data on the device, like a small database. Where a variable or a setting holds one value, a table holds many records with the same fields.

Typical uses:

  • Lists that users maintain, such as authorized card IDs, watering schedules or Modbus devices to poll.
  • Logs and time series, such as sensor readings every minute or door events.
  • Lookup data, such as a table that maps product codes to recipes.

Where to find tables​

Open the Features step and select Data Tables in the Data & Storage category. Each table is a tab. Click + to add a table and Add Field to add fields to the selected table.

The Data Tables page with readings and zones tabs. The readings table has timestamp, temperature and humidity fields.
OptionDescription
Debug PrintPrints table operations to the debug output.
Storage(Zephyr) The flash partition that holds the tables.

Designing a table​

Double-click a table's tab, or choose Edit from its ⋯ menu, to open the table's properties:

The editor tab for the zones table with Name, Description, Storage, Max Records, Table Structure, a Fields list, Index Field and Storage.
PropertyDescription
NameThe table's name, used in blocks, the Web Console and scripts. Must start with a letter.
DescriptionAn optional note, up to 225 characters.
StorageFlash keeps the data across reboots. In Memory keeps the table in RAM: it's faster, but it is emptied at every boot. See In-memory tables.
Max RecordsThe maximum number of rows, up to 65535. 0 means unlimited. What happens when a table is full depends on its structure.
Table StructureTable for records you look up, update and delete. Log for append-only records, such as time series.
Number of Key Fields(TiOS) The number of fields, counted from the first, that together must be unique. An insert that would duplicate a key fails.
FieldsThe table's columns. See Fields.
Index Field(Zephyr, Table structure) A field to index in RAM, which speeds up Equals lookups on that field.
Storage(Zephyr) The flash partition for this table.
The Storage dropdown of a table open, showing Flash and In Memory.

Table or Log?​

TableLog
Update and delete rowsYesNo, append only
When fullNew rows are rejectedThe oldest rows are dropped to make room
Good forLists, schedules, lookupsReadings, events, audit trails
Web Console pageDatabaseLog (shown when the Log feature is enabled)
Zephyr

On Zephyr, Max Records limits Log tables. Table-structure tables grow until the storage is full.

Fields​

Select a table's tab and click Add Field, then click the pencil icon on a row to open the field:

The editor tab for the duration field with Field ID, Display Name, Field Type Number(value), Minimum 0, Maximum 3600, Default Value 600 and an empty Options list.
PropertyDescription
Field IDThe field's name in blocks and scripts. Up to 32 letters, digits and underscores; can't start with a digit.
Display NameThe column title in the Web Console.
Field TypeString(text), Number(value), DateTime or Time.
Minimum / MaximumFor numbers, the allowed range. For text, the length. Not used for DateTime and Time.
Default ValueThe value used when a row is added without one.
OptionsAn optional list of label/value pairs. The Web Console shows the field as a dropdown.
Field typeHoldsTakes
String(text)Text, up to 128 charactersMaximum + 1 bytes
Number(value)A 32-bit floating-point number4 bytes
DateTimeA date and time (Unix timestamp)4 bytes
TimeA time of day (seconds since midnight)4 bytes

A row can take at most 128 bytes: one byte plus the size of each field. Keep Maximum of text fields as small as the data allows.

In-memory tables​

A table with Storage set to In Memory holds up to 30 rows in RAM. For Table-structure tables you can fill in In-Memory Rows in the editor; the table is reloaded with those rows at every boot. This makes in-memory tables handy for fixed lookup data that you want to edit in the project rather than on the device.

System tables​

Two Log tables are managed for you:

  • LOG is added when the Log feature is enabled. It stores the messages of the Add to Log block.
  • DEBUG is added when General › Debug Print Output is set to Store in Logs.

Both have a timestamp and a value field. You can't rename them or change their fields.

Using tables in blocks​

The table blocks are in the Data & Storage category of the block library. Every block has a Table setting that selects the table it works on.

BlockWhat it doesOutputs
Table Insert RowAdds a row. Each field is a parameter, pre-filled with the field's default value.Done
Table LookupFinds one row where a field matches a value.Record Found with row_index and every field, or Record Not Found
Table Lookup Compound(Zephyr) Finds one row that matches several conditions, all of which must be true.Same as Table Lookup
Table Update RowChanges fields in the first row that matches a condition.Done
Table Update Row Compound(Zephyr) Same, with several conditions.Done
Table Delete RowDeletes the first row that matches a condition.Done
Clear TableDeletes all rows.Done
Table Row CountCounts the rows.Row Count with rows

Adding rows​

This flow logs the temperature and humidity every time the temperature changes. Get Current DateTime provides time_now for the timestamp field:

On Variable Changed for temperature, followed by Get Current DateTime and Insert row into readings.Settings of the Table Insert Row block: Table is readings, with parameters timestamp = time_now, temperature and humidity.

Looking up rows​

Table Lookup searches one field with an operator: Equals, Not Equal, Greater Than, Greater Than Equal To, Less Than or Less Than Equal To.

Settings of the Table Lookup block: Table zones, Wrap Around Disabled, Field Name zone, Operator Equals, field_value North and find_index 0.
  • field_value is the value to compare with. It can contain variables and parameters from earlier blocks.
  • find_index picks which match to return when several rows match: 0 for the first, 1 for the second, and so on. To go through all matches, repeat the lookup with an increasing index until Record Not Found fires.
  • With Greater Than and Less Than operators, matches are ordered by the field's value, so index 0 is the closest match. For example, Time Greater Than Equal To 08:00 returns the next entry of a schedule.
  • Wrap Around: when nothing matches a Greater or Less operator, return the row with the lowest value (for Greater) or the highest value (for Less) instead. Use it for schedules that wrap past midnight: after the last entry of the day, the lookup returns the first one.

Record Found passes every field of the row to the blocks that follow, together with row_index:

On Command water_now, followed by a Table Lookup on zones WHERE zone == North. Record Found prints the zone and duration; Record Not Found prints Zone not found.

Updating, deleting and counting​

Table Update Row and Table Delete Row use the same field, operator and value as a lookup, and act on the first matching row only. They continue to Done whether or not a row matched, so if you need to know, look the row up first.

On Command report, followed by Count rows in readings and a Debug Print of rows readings stored.

Storage and reboots​

  • Flash tables keep their rows across reboots and power loss. In Memory tables start empty (or with their In-Memory Rows) at every boot.
  • Flash has a limited number of write cycles. Logging every second for years is fine for a Log table; rewriting the same row many times a second is not.
Changing a table erases its data

When you upload an application in which a table's fields or structure have changed, the device can't read the old rows. On Zephyr the changed table is recreated empty. On TiOS the flash disk is formatted and all tables are emptied. Export the data from the Web Console first if you need it.

Managing tables from the Web Console​

The Web Console lets users view, search, add, edit, delete, import and export rows. Tables are not shown until you add them:

  1. Open Features › Web Console and select Database in the console preview.
  2. Click Edit and then Add Table, and choose the table. Name sets the title of its tab.
The Web Console Tables list with one entry: table zones, name Watering Zones.The Database page of the Web Console preview with a zones tab, Refresh, Add Entry, Search and Actions buttons, and columns Zone (String), Start Time (Time) and Duration (s).

Only Table-structure tables can be added to the Database page. Log tables are shown on the Log page. On Zephyr, adding, editing, deleting and clearing rows require the Web Console administrator account (Web Console › Advanced).

Tables can also be read and changed from custom pages and synchronized with AppBlocks Cloud, where device groups can manage the same table on all their devices.

Troubleshooting​

Message in ProblemsWhat to do
Table requires at least one fieldAdd a field.
Fields total maximum size of table … must not exceed 128 bytesLower Maximum of text fields or remove fields.
Duplicate field namesGive each field a unique Field ID.
Field ID cannot start with a number / cannot be a reserved keywordRename the field.
Number of Key Fields must not exceed number of fieldsLower Number of Key Fields.
In Memory Table must have less than 30 rowsRemove rows from In-Memory Rows, or store the table in flash.
Default value must be a numberEnter a numeric default for Number fields.
Table is not validA block refers to a table that was deleted. Choose another table in the block.

Examples​