Mqtt commands

  • Platform information:
    • Hardware: _CPUArchitecture/RAM/storage_Pi4 4gb
    • OS: _what OS is used and which version_Openhabian
    • Java Runtime Environment: which java platform is used and what version
    • openHAB version:3.2
  • Issue of the topic: please be detailed explaining your issue
    I have openHAB 3.2 running on a Pi4. I use ZigBee lights, plugs and sensors via zigbee2mqtt.
    All is working as I expect but I have just seen that I could send commands direct to MQTT in rules instead of sending to items. Is there any benefit or advantage to do it this way or should I just stick with sending commands to items?

I would say to stick to commanding your items as much as possible, as that’s really the basis of openHAB. That way you have an item state that can be persisted in openHAB. Also, it makes it easy to swap different items (which may not use MQTT) in and out of your rules.

I’m sure there are use cases for sending commands directly to MQTT, but whenever you can set up an item I don’t see a reason to do it.

1 Like

The limitation of the usual Things/channels structure is that you can only send out the info of one Item at a time.

On the rare occasion where you need to combine multiple Item states, say to build a complex JSON message, that’s the time to use rules and Actions.

Remember, anything you want in the UI has to go to/from an Item anyway.

2 Likes

Another use case where sending the messages from rules is when the message represents a momentary event (e.g. like a button press) where the long term state of an Item after the event is meaningless. No need to set up an Item in that situation.

But I want to step back a second and expand on what Russ said about commanding Item being the basis of openHAB.

At its most basic, openHAB is a bridge between 350+ different technologies and APIs. In order to achieve this amazing feat, it uses a common approach of using layers of abstraction. Those layers include Things and Items. Once you have an Item to represent a sensor or control point on a device it no longer matters what technology that device uses. A Switch is a Switch whether it’s connected to a Zwave light switch or an MQTT topic. Rules, the UI and everything else in OH doesn’t need to know nor does it need to care when using Items.

If you bypass the Items you lose that abstraction. Now your rules for sure and other parts of OH also need to understand that this MQTT Switch is an MQTT Switch and has to be handled differently. As rossko57 pointed out, there are situations where the Item model can’t do what you need in which case we have the actions that let you publish messages from rules. But that’s a last resort sort of thing, not the standard way to try to do it all the time.

2 Likes