Hi all,
I’ve been testing formatBeforePublish with different MQTT channel types and noticed that its behavior is highly type-dependent. The official docs say:
- formatBeforePublish: Format a value before it is published to the MQTT broker. The default is to just pass the channel/item state. If you want to apply a prefix, say “MYCOLOR,”, you would use “MYCOLOR,%s”. Currently only “%s” is supported.
In practice, it works differently depending on the channel type.
Observed behavior of formatBeforePublish by channel type
| Channel type | %s works? |
%d works? |
Notes |
|---|---|---|---|
| Color | Both work; numeric formatting is applied for %d. |
||
| Dimmer | Only %s works; %d is ignored, formatting not applied. |
||
| Color Temperature | Same as Dimmer; only %s works. |
||
| Rollershutter | Neither %s nor %d works; raw value is published. |
||
| Number | Only %d works; numeric formatting applied. |
Example things snippets
Color channel (works with %d or %s)
Type color : colorBulbC8CE39 "colorBulbC8CE39" [
stateTopic="zigbee2mqtt/0x00178801102bbc90",
transformationPattern="JSONPATH:$.color\",\"JSONPATH:$.brightness",
commandTopic="zigbee2mqtt/0x00178801102bbc90/set",
formatBeforePublish= "{\"color\": {\"hue\": %d, \"saturation\": %d}, \"brightness\":%d}",
colorMode="HSB"
]
Dimmer channel (only works with %s)
Type dimmer : dimmerBulbC642E4 "dimmerBulbC642E4" [
stateTopic="zigbee2mqtt/0x00178801011fa261",
transformationPattern="JSONPATH:$.brightness∩JS:zigbee2mqtt_brightness_in.js",
commandTopic="zigbee2mqtt/0x00178801011fa261/set",
formatBeforePublish= "{\"brightness\": \"%s\"}",
transformationPatternOut="JS:zigbee2mqtt_brightness_out.js",
min=0,
max=100,
step=1,
unit="%"
]
Color Temperature channel (only works with %s)
Type colorTemperature : ctBulbC8CE39 "Color Temp" [
stateTopic="zigbee2mqtt/0x0017880110abcd/set",
commandTopic="zigbee2mqtt/0x0017880110abcd/set",
formatBeforePublish= "{\"color_temp\": \"%s\"}",
min=153,
max=500
]
Rollershutter channel (does not support formatting)
Type rollershutter : rollerBulb1234 "Roller" [
stateTopic="zigbee2mqtt/0x0017880110efgh",
commandTopic="zigbee2mqtt/0x0017880110efgh/set"
]
Number channel (only works with %d)
Type number : numberBulb5678 "Number Example" [
stateTopic="zigbee2mqtt/0x00178801101234",
commandTopic="zigbee2mqtt/0x00178801101234/set",
formatBeforePublish="%d",
min=0,
max=100
]
Key takeaways
- Type-sensitive behavior:
formatBeforePublishbehaves differently depending on the channel type. - Dimmer & Color Temperature channels only accept
%s. - Rollershutter does not support formatting.
- Number channels only work with
%d. - The documentation (“only
%sis supported”) is not fully accurate; behavior varies by channel type.
Question for the community
Is this type-specific behavior intentional?
It seems that numeric formatting (%d) is selectively supported depending on the channel type.
I hope this helps clarify formatBeforePublish usage for others — any additional insights or confirmations are appreciated!