MQTT switch help

It helps more to describe the path from your device -> MQTT broker -> MQTT binding -> transform -> item -> sitemap.

  1. Your device publishes the string “ON” to the topic “/switch” on the broker “broker” when it turns on, and “OFF” when it turns off.
  2. You have a Switch item defined in your items file like this: Switch mySwitch { mqtt="<[broker:/switch:state:default]" }.
  3. You have the item appear in your sitemap with Text item=mySwitch
  4. At this point, you can see the switch turn off and on in the openHAB Classic UI.

Problem is, your device publishes “1” for on and “0” for off. So instead of Switch mySwitch { mqtt="<[broker:/switch:state:default]" }, you have replace default with a transform to turn the MQTT message into the “ON” or “OFF” that openHAB needs for switches. So you would have your item be Switch mySwitch { mqtt="<[broker:/switch:state:MAP(onoff.map)]" } and you would have a file transform/onoff.map with

0=OFF
1=ON

in it. Does this make sense so far?

1 Like