[SOLVED] Toggle Zigbee plug through a Bridge using MQTT

Hi all,

First post. I have been working with openHAB for about two weeks now so forgive my stupidity, for context I have a Sonoff Zigbee Bridge (ESP8266) running Tasmota and I am trying to communicate with a S26 ZB Smart Plug via the Bridge using MQTT.

This post is mainly a sanity check, I have achieved the outcome I want with the method below but I was wondering if this is the correct, and most importantly the cleanest way to do it? My assumption is that the MQTT addon bindings are design to talk directly to MQTT devices and not to a ZigBee device connected to a MQTT Bridge?

.things

Bridge mqtt:broker:1 "Mosquitto MQTT Broker" @ "Office" [ host="openhabian", clientId="openhabian", username="openhabian", password="" ]

Thing mqtt:topic:1 "MQTT Topic" (mqtt:broker:1) {
    Type number : S26x1_Power [stateTopic="zigbee/switches/s26x1/power"]
}

zigbee/switches/s26x1/power is published from ZbReceived using a Tasmota rule.

.items

Number S26x1_State "S26x1 State" { channel="mqtt:topic:1:S26x1_Power" }
Switch S26x1_Switch "S26x1 Switch"

Using a virtual switch and number to track the state.

.rules

rule "SB26x1_Switch"
when
    Item S26x1_Switch changed
then
  val mqttActions = getActions("mqtt","mqtt:broker:1")
  mqttActions.publishMQTT("cmnd/tasmota_E24D4E/ZbSend", '{ "Device": "0xBAED", "Send": { "Power": "' + S26x1_Switch.state + '" } }')
end

rule "SB26x1_State"
when
  Item S26x1_State changed
then
  val state = if (S26x1_State.state == 1) ON else OFF

  if (state !== S26x1_Switch.state) {
    logInfo("zbbridge", "S26x1 state was changed outside of openHAB.")

    S26x1_Switch.sendCommand(state)
  }
end

Thanks,
Mike

The MQTT binding only knows MQTT. It sends and receives messages over the MQTT protocol. What is at the other end of the MQTT topic is, from the MQTT binding’s perspective, irrelevant.

Some observations:

  • Why not use a switch channel? Then you wouldn’t need the rules at all. In the Channel config you can format the outgoing message JSON, translate the 0/1 to ON/OFF and link to just one Switch Item that gets updated from messages on the state topic and sends the JSON to the command topic when a command is sent to the Item.

  • If you’ve already created a Channel and linked it to an Item, it’s odd to use the publishMQTT Action.

  • Review the docs for the difference between updates and commands. It’s an important distinction. In the second rule, you should be updating the Item, not commanding it.

That’s great, I knew it didn’t feel right.

I have made changes based on your observations…

.things

Thing mqtt:topic:1 "MQTT Topic" (mqtt:broker:1) {
    Type switch : S26x1_Switch "S26x1 Switch" [ stateTopic="zigbee/switches/s26x1/power", on="1", off="0", commandTopic="cmnd/tasmota_E24D4E/ZbSend", formatBeforePublish="{ \"Device\": \"0xBAED\", \"Send\": { \"Power\": \"%s\" } }" ]
}

.items

Switch S26x1_Switch "S26x1 Switch" { channel="mqtt:topic:1:S26x1_Switch" }