Webinar
EMQX Cloud Message History: From MQTT Topics to SQL Queries →
Back

Designing a Message History Table: Decisions to Make Before the First Write

EMQX Cloud TeamEMQX Cloud Team
Dec 1, 2026Product
Designing a Message History Table: Decisions to Make Before the First Write

Message History turns an MQTT topic into a table in EMQX Tables in a few steps. If you haven't seen it yet, start with the launch article.

Setup is quick, which makes it easy to skip the design work. Most of a mapping is easy to change later, but a few choices aren't, because the table takes its shape from the first message that's successfully written to it. This post covers those choices so the table you create on day one is still the one you want a month later.

1. The First Write Sets the Column Types

The message example in the wizard only previews the schema; it doesn't create the table or write a record. If the target table doesn't exist yet, the first successful write creates it, and the converted values in that write decide each column's type, which every later write then has to match.

That makes the data type you pick for each Field one of the most important settings in the wizard, and the types on offer depend on the example value you paste. The Console's own default example shows the risk: it sets "humidity": 58 to an integer type, even though real readings like 58.5 may follow.

Before the first write:

  • Choose Float for any measurement that can carry a decimal, even if your example value happens to be a whole number.
  • Paste an example that looks like real traffic, including every field you plan to store, with realistic values.
  • Use a value transformation when you need a specific type. Click the transformation button beside Data Type and enter an expression such as float(payload.humidity). Entering float(...) switches the Data Type to Float, and the dialog previews the converted value.

Here, humidity is set to Signed Integer, so change it to Float before you continue.

The Value Transformation dialog with float(payload.humidity) applied to the humidity field.

If a column already has the wrong type, writes that don't match it can fail while the table keeps its existing column type. Because the only way out is a new table and mapping, it's worth getting this right before the first message arrives.

2. Decide What Becomes a Tag

Tags identify and group a time series, while Fields hold the values that change. A good rule of thumb is that if you'll filter or group by something, it should be a Tag, and if you'll average, chart, or compare it, it should be a Field.

DataRoleExamples
Text used to filter or group recordsTagdevice_id, region, model
Measurements and changing valuesFieldtemperature, humidity, voltage

The wizard lists every detected value under Fields, including IDs, so for each identifier you'll filter or group by, use the move icon on its row (its tooltip reads Move … to Tags).

device_id moved to Tags, with clientid added as a second Tag, both stored as strings.

The wizard's own note on Tags is worth repeating: avoid values that are unique for every message, such as a message ID, because Tags are meant to group many readings together.

Tags have a few rules worth knowing up front:

  • Tags are always strings. A numeric device ID or a Boolean needs converting to a string before it can be a Tag.
  • Some values can't be Tags at all. The complete payload, objects, arrays, and null values can't be Tags.
  • Every mapping needs at least one Field, and every Tag, Field, and timestamp column needs a unique name.

Your payload doesn't have to carry every identifier. Use Add Tag or Add Field to store MQTT metadata such as clientid, topic, qos, username, or peername. If devices don't put their ID in the payload, mapping clientid as a Tag keeps every row tied to the client that sent it. If your topic structure encodes something useful, such as a site or a line, mapping topic as a Tag lets you group by it later.

3. Plan One Table per Message Shape

A mapping writes the values from one topic filter into one table, and wildcards let it cover many devices. In this solar fleet example, energy/+/+/telemetry writes every site and inverter to the same table, because every inverter sends the same payload. Plan each table around messages that share a consistent structure, such as readings from the same device type or application. When two kinds of messages have fields or value types that don't line up, give each its own table instead of forcing them together.

You also choose how much of each message to keep:

  • Map only the values you'll query. Unmapped members of the payload don't become columns.
  • Or store the complete payload as a single Field. This keeps everything, but as one stored value, not as separate columns you can filter and aggregate.

Map the values you query regularly as their own columns, and if a new need comes up later, you can always add a mapping.

4. Choose the Timestamp and Its Precision

Every record needs a timestamp, and the launch article covers when to use the Broker's received time and when to use the device's own timestamp. The detail that trips people up is precision.

Broker Received Time always uses milliseconds, but if you use a numeric timestamp from the payload, check that Time Precision matches the unit your devices send. For timestamps from recent years, counting the digits is a quick check:

Example valueDigitsPrecision
176120640010Seconds (s)
176120640000013Milliseconds (ms)
176120640000000016Microseconds (us)
176120640000000000019Nanoseconds (ns)

A payload timestamp selected as the Timestamp Source, with Time Precision set to match its unit.

A wrong precision doesn't always show up as an error. In testing, a timestamp in seconds configured as milliseconds counted as a Success, but it was read as a date in January 1970, outside the table's 30-day TTL, and no query ever returned the row. After the first write, check the timestamps in Data Explorer as well as the metrics.

Message History also accepts RFC 3339 timestamp strings such as 2026-09-23T08:00:00Z, but only when the example payload uses one: paste an example with the string, select that field as the Timestamp Source, and include the Z or a UTC offset. Because a mapping built from a numeric timestamp rejects RFC 3339 strings, pick one format and keep every device on it. A field selected as the Timestamp Source is also removed from Tags and Fields and can't use a custom conversion expression, so the payload has to send it in a usable format.

5. Know What Happens When the Payload Changes

A firmware update can change the payload sent to an existing table:

  • New fields aren't stored automatically. Message History doesn't add columns for fields it hasn't been told to map, unless the mapping stores the complete payload as a single Field.
  • Adding a field takes an edit. Update both the Rule SQL and the EMQX Tables action in Data Integration, or create a new mapping.
  • A type change can break writes. If a mapped field changes type, writes to the existing table can fail, because the table keeps its original column type.
  • A missing field drops the whole message. If a mapped field isn't in the payload, its conversion fails and the message is skipped rather than stored with an empty value. Publish the changed payload before rolling it out to every device.
  • An incompatible structure needs a new table. Create a new mapping and target table instead of mixing incompatible records.

After any edit, publish a test message, check the mapping's Success, Failed, and Dropped metrics, and query the table. Keep in mind that those metrics only count writes to EMQX Tables, so a message that fails earlier, in the conversion step, never reaches them. A missing field or a value that can't be converted (such as "hot" for a Float) shows up only in the rule's statistics under Data Integration > Flow Designer and in Diagnostics > Logs. When you're ready to remove a mapping, delete it from Message History so EMQX Cloud can clean up the resources it manages.

The rule's statistics in Flow Designer: 10 messages matched, but 2 failed in the conversion step and never reached the Message History metrics.

6. Check Existing Tables and Reused Connectors

Two settings are fixed when a table is first created, its TTL and its timestamp column name, and a new mapping never changes them on a table that already exists. If you point a mapping at an existing table, inspect that table's timestamp column, TTL, and schema first.

When the wizard creates a new connector, the tables it creates use the TTL you set in the Storage Location step, which defaults to 30 days. If a Message History connector already exists for the same Tables deployment, database, and user, the wizard reuses it instead, and that connector's TTL and timestamp column name are read-only and apply only to tables first created through it.

Before the First Write: A Quick Checklist

  • Every measurement that can carry a decimal is set to Float.
  • The example message includes every field you plan to store, with realistic values.
  • Identifiers you'll filter by are Tags, and they're strings.
  • clientid or topic is mapped as a Tag if the payload doesn't identify the device.
  • One table holds one message shape.
  • The timestamp source and its precision match what devices send.
  • The TTL fits how far back you'll need to look.
  • If the table or connector already exists, you've checked its settings.
  • After the first message, you've found the row in Data Explorer with the right timestamp.

Next in the Series

This is Part 1 of the Message History series. Part 2 puts the table to work with SQL, going beyond the basic queries to trends and gaps in device data.

Want to see these decisions in a real setup? Join the Message History webinar on Thursday, December 17, to watch a mapping built and queried end to end, and bring your questions. Register for the webinar.

Message History is available on EMQX Cloud Dedicated and Dedicated Flex deployments running EMQX 6.1.4 or later. New to EMQX Tables? Explore EMQX Tables and start a 14-day trial. On Dedicated Flex? Request a free month while the offer runs. Already on Dedicated or Dedicated Flex? Create your first mapping.

Talk to an Expert
Contact Us →

Article by

EMQX Cloud Team

EMQX Cloud Team

The EMQX Cloud team provides a secure, reliable, and fully managed MQTT service. The team assists IoT developers in easily launching MQTT services unburdened and unworried about infrastructure maintenance.