IMD SolutionComputing LibraryShop main store

Explainer

Why a multi-entity MQTT device needs a mapping layer

Edge computing and IIoT Published 8 min read By IMD Solution engineering team

Short answer

A Tasmota device reports every function under one configured topic, so no wildcard or broker rule can split a fan-light into two entities. Keep two logical entities in your own namespace and map each message by device topic plus payload field: POWER1 or FanSpeed decides, not the topic.

On this page
  1. Can one Tasmota device publish a fan and a light as two MQTT topics?
  2. Why can't MQTT wildcards split one device topic into two entities?
  3. What identifies the function inside one device topic?
  4. How do I build the mapping layer for a multi-function device?
  5. Should the mapping live in Tasmota rules, the broker or Node-RED?
  6. What should the canonical topics for a device's two functions look like?
  7. What happens to a message whose function field matches no row?
  8. When does a device-only lookup table stop being enough?
  9. Questions asked

A multi-entity MQTT device cannot be split by topic routing: the topic names the device, and only the payload names the function. On a Tasmota fan-light such as the Sonoff iFan, the relay state and the fan speed arrive on the same device topic, separated by the payload fields POWER1 and FanSpeed. No wildcard, broker rule or topic rename turns that one payload into two topics. The working design keeps two logical entities in your own namespace and maps every message by device topic plus payload field, with unknown pairs going to a diagnostic path instead of a guessed entity. The sections below trace the message path, give the lookup rows and a worked example, and end with the decision for each device type.

Can one Tasmota device publish a fan and a light as two MQTT topics? #

No: a Tasmota device reports every function it has under one configured device topic, so the fan and the light arrive on the same path and are separated only by the payload field or channel, such as POWER1 or FanSpeed.

A Tasmota unit keeps a single configured topic for the whole device and publishes its command, state and telemetry traffic under that topic's branches, which is why the answer is no at the protocol level. The functions differ inside the messages: on this fan-light the light relay is the POWER1 channel and the fan speed travels as a number in the FanSpeed field (levels 1 to 4 on this fan), and both can appear in the same state message. Which relay number drives which function is fixed by the device's own wiring and firmware, so read it from the documentation for that model rather than assuming a numbering. The fan side adds a reported gap: the MQTT interface offers no single set-speed topic to subscribe to, so the decision of what speed to command belongs to your entity logic, not to any topic.

Why can't MQTT wildcards split one device topic into two entities? #

A wildcard subscription selects topic paths; it cannot create a topic level or split one payload into two messages, so subscribing to the device topic still hands your flow a single message that contains both the fan and the light.

Wildcards earn their place when many devices share one consistent hierarchy: subscribing to a whole branch reduces the number of input nodes without changing what each message is. They do nothing for the split you need here. The router that tests whether a topic contains /fan/ or /light/ cannot fire, because neither level exists in the device topic, and both functions fall through to whichever branch matches the device's name. That is the exact failure the mapping layer exists to remove.

Separate the two problems this can look like from the flow side. If the device publishes and the input receives nothing, the fault is in the network link, the broker connection or the subscription, and no topic rewrite repairs it: a message that has already arrived has crossed those hops. If the input receives the message and the wrong flow handles it, the mapping is the suspect.

What identifies the function inside one device topic? #

The topic identifies the device and the payload identifies the function: in one RESULT message from this fan-light, the JSON field POWER1 carries the relay state and the field FanSpeed carries the fan speed, so the pairing of device topic and field name is the routing key.

Separate the message roles before normalizing any of them, because a command acknowledgment is not automatically the state report and the two can disagree about the same relay. Compare the result of a command with the message that actually reports state before you treat either as the entity's status. The roles on this device look like this:

Role Example on this device Use in the mapping
Command to the device cmnd/kitchen_fan/POWER with payload TOGGLE Outbound path; a row converts a canonical command into this form
Command result stat/kitchen_fan/RESULT with payload {"POWER1":"ON"} Acknowledgment; confirms what the device did
State report stat/kitchen_fan/POWER with payload ON Dedicated report; normalize this into the logical entity's status
Telemetry tele/kitchen_fan/STATE or tele/kitchen_fan/SENSOR Periodic JSON; parse the fields your rows map

kitchen_fan is an example device topic here; the command, state and telemetry branches follow the pattern the device's own documentation describes for its configured topic.

How do I build the mapping layer for a multi-function device? #

Build the map as a lookup keyed on device plus payload field: this fan-light needs two rows inbound (POWER1 and FanSpeed) and two outbound, and each new device adds its own rows instead of new code.

Keep the inbound and outbound translations as separate tables with separate responsibilities: inbound rows decode device messages into canonical state, outbound rows convert canonical commands into device-specific form. For this fan-light the rows are:

Key (device plus field) Inbound row produces Outbound row accepts
kitchen_fan + POWER1 home/downstairs/kitchen/light/fan-light/status with {"state":true} or {"state":false} A light set command, converted to the relay command for that channel
kitchen_fan + FanSpeed home/downstairs/kitchen/fan/ceiling/status with {"speed":2} for a level of 2 A speed set command, converted to the device's numbered speed command with the level as payload

Work one message end to end. The device publishes stat/kitchen_fan/RESULT with payload {"POWER1":"OFF","FanSpeed":2} — an example payload in which both functions report together, as they do on this device. The decoder splits the topic into branch stat, device kitchen_fan and role RESULT, parses two fields from the one payload, then follows two rows: the FanSpeed row publishes home/downstairs/kitchen/fan/ceiling/status with {"speed":2}, and the POWER1 row publishes home/downstairs/kitchen/light/fan-light/status with {"state":false}. The arithmetic that sizes the design: one message in, two canonical messages out, and 1 device topic times 2 mapped fields equals 2 logical entities; ten identical devices still need only 10 device subscriptions yet publish 10 times 2, which is 20 canonical topics.

The same lookup serves the outbound path in reverse: a canonical speed command for level 2 becomes the device's numbered speed command with payload 2, and a light command becomes the relay command for the POWER1 channel. Each new device model appends rows to the same tables; nothing in the downstream flows changes.

Should the mapping live in Tasmota rules, the broker or Node-RED? #

Keep the mapping on the client side, in your Node-RED translation layer: the broker routes whole messages by topic and cannot split a payload, and device-side rules can re-shape a message but scatter firmware quirks across every device you flash.

The device side is for device quirks. A Tasmota rule or a backlog sequence can bridge a quirk such as a relay that needs a fixed boot order, and that is where such a fix belongs; but a rule lives inside one device's configuration and must be re-created on every unit, so the entity split should not depend on it. The broker side cannot carry the split either: routing and access rules act on whole topics, and a broker does not parse payloads into entities. The placement rule is the one Sparkplug B device tags guide states for that protocol: fix the client, not the broker. Centralizing the translation in one place keeps the quirks auditable in a single table instead of distributed across firmware.

What should the canonical topics for a device's two functions look like? #

Give each function its own canonical entity and keep the type level intact: for this device that means home/downstairs/kitchen/fan/fan-speed/status for the speed and home/downstairs/kitchen/light/fan-light/status for the light, both mapped from the same physical device.

The type-first layout is worth keeping because flows depend on it: a subscription to the light branch is how "every light off" reaches every light, and a fan-light parked wholly under the fan branch would silently drop out of that logic. The two entities above preserve it. The alternative convention puts the function in the entity name, for example fan:light and fan:speed under one device name; that keeps a unit's controls visibly grouped and is a legitimate choice if applied consistently and checked against existing parsing.

What not to do is reshape the canonical schema to mirror the device's internal layout: normalization exists so a different device or protocol can replace this one without downstream edits. The same discipline applies as in the guide to naming Sparkplug B group, edge node and device IDs: the name encodes the structure your flows route on. When logic needs both values together, it subscribes to the two canonical topics — the same pattern as combining two MQTT sensor inputs in one Node-RED calculation.

What happens to a message whose function field matches no row? #

Send any message whose device-plus-field pair has no table row to a diagnostic topic or log, and never route it by device name: a guessed entity produces plausible-looking state that no row can audit and a command that may drive the wrong function.

Run the map default-deny in both directions. Inbound, an unknown field goes to the diagnostic path with the original topic and payload preserved, which is also how a new device model announces its field names to you: log first, add rows, then take the entity live. Outbound, a canonical command with no row is dropped and logged, never forwarded to the device anyway, because the only thing a rowless command can produce is a guess. On a machine or a line, a command that lands on the wrong function is not a cosmetic fault, and the default-deny rule is what keeps an unmapped function from actuating anything.

When does a device-only lookup table stop being enough? #

A device-only key is still enough while every device has exactly one function: keep it, and extend the key to device plus field the day the first two-function device arrives, because a single-function device has nothing for the field to discriminate.

The decision by case:

  • One function per device — a plain relay, a sensor: topic-level routing stands, and the device-key row keeps normalizing the payload.
  • Two or more functions sharing one topic: two logical entities, keys of device plus field, default-deny on unknowns, separate inbound and outbound rows.
  • A mixed estate of both: both row shapes live in one table, and routing every device through it removes the per-device exceptions that otherwise accumulate in flow code — the reason the practice scales across a wild mix of device types.

So the decision: keep the fan-light as two entities, key the map on device topic plus payload field, and let the table, not the topic, carry the function.

Questions asked

Does one MQTT connection carry both entities of a fan-light device?

Yes. Entities are logical, not connections: the Node-RED client keeps one subscription to the device topic, and the mapping layer publishes to two canonical topics from that single input. Adding a second entity costs a table row and a canonical topic, never a new broker connection or client.

What if a future device publishes each function on its own subtopic?

Then topic-level routing works again for that device: a subscription filter or a substring test on the topic can select the function directly. Keep the row in the mapping table anyway, so the canonical payload stays normalized and the estate keeps one ingestion path for mixed devices.

How do I prove the mapping works before trusting it?

Publish a known test message to the device's state topic, for example a RESULT payload carrying FanSpeed 3 and the relay state, and confirm exactly two canonical publications come out with the values you expect. Then send one command per entity through the outbound rows and confirm the device acts on the right function.

Two device models use different field names for the same function. Does the table double?

The rows double, the canonical targets do not. Each model gets its own key, for example one model's fan-speed field and the other model's speed field, both mapped to the same canonical fan entity. Downstream flows never see the difference, which is the point of the mapping layer.

Keep reading

  • How-to

    Combine two MQTT sensor inputs in one Node-RED calculation

    Node-RED accepts many wires into one node but delivers each message separately, so combine the two MQTT streams explicitly. Use a Join node in manual mode with a message count of 2 and msg.topic as the key, or a Function node that stores the latest temperature and smoke flag in context and evaluates both on every arrival.

    Edge computing and IIoT

  • Troubleshooting

    Fix MQTT sensor data missing in Node-RED

    A green MQTT connection in Node-RED proves only a broker session, not that the GSM module published a value or that the flow subscribes to its topic. An http in node cannot receive MQTT publications at all: use an mqtt in node. Trace RS-485 acquisition, publish result, broker identity and topic match in order, and fix the first stage with no positive result.

    Edge computing and IIoT

  • How-to

    How to query InfluxDB over MQTT with a bridge flow

    InfluxDB does not answer queries over MQTT: it serves them over its HTTP API. Put a bridge on the edge PC — a Node-RED flow subscribed to a request topic — that maps a validated, bounded request to a query against the bucket and publishes the result to a separate response topic, so request and reply never loop.

    Edge computing and IIoT

What decides time correlation in multi-device test data logging