[SOLVED] Can't get the Thing to post mqtt command upon receiving state change

  • Platform information:
    • Hardware: RPi2
    • OS: Openhabian 2.4.0-1 (Release Build)
    • Java Runtime Environment: OpenJDK Runtime Environment (Zulu8.40.0.178-CA-linux_aarch32hf) (build 1.8.0_222-b178)
    • openHAB version: 2.4)I guess_
  • Issue of the topic: I cannot make the Thing’s ON/OFF channel send a command message when the state is changed.
  • Please post configurations (if applicable):
    • Items configuration related to the issue: two Tasmotas as generic MQTT things, linked automatically.
    • Services configuration related to the issue: Mosquitto running on another RPi2 and .

I just started playing with OpenHab several days ago. Installed/configured OpenHabian, separate Pi for MQTT broker&databases. Basic functionality works, as far as I can tell.

I have two Tasmotas. One is monitoring some switch. On every switch flip it posts its state to the topic cmnd/custom-topic/SWITCH
Second sits in another room and has a relay attached and is subscribed to the standard cmnd/relay/Power .
What I am trying to achieve is, basically, a wireless/mqtt/openhab switch extender. When switch changes its state, so must do the remote relay. Later I plan to add some more logic to this (that’s why I do not send the right command just from the Switch Tasmota to Relay one), but I decided to start simple.
Switch (controlling) Tasmotas is configured as Generic MQTT Thing. Is has On/Off Switch type channel with state topic and command topic. State topic is cmnd/custom-topic/SWITCH . Command topic is cmnd/relay/Power, with Is command enabled.

Problem is, the command is never published. The state topic is monitored OK, I can see the widget changes accordingly. The relay itself works: I can trigger it from mqtt client. I even created an auxiliary “switch” channel with command topic only for cmnd/relay/Power and I can use it to trigger relay manually. But for the main switch channel, I cannot make the incoming mqtt message to trigger the outbound command.

I found this Trigger a rule by MQTT command? topic but it made things less obvious to me. Before reading it I hoped that there’s something simple that I am missing. But I read about MQTT Bridge publishTrigger and did not understand a bit. What is the purpose of that channel, what will trigger what and generally what would be the use case for this feature – documentation left me puzzled. I played with this option, but it gave me nothing (and I did not really understand what this channel is doing)

PS Pretty much everything was configured from PaperUI.

  1. Did you configure the MQTT Broker Thing?
    2.Is the MQTT Broker Thing showing as Online in PaperUI?
  2. Show your Thing definition.
  3. Did you create Items and link them to the Channels on the Thing?
  4. Errors in the log?

Rich, thanks for the reply. Answers:

1 Yes
2 Yes
3 Since it’s configured in UI, here’s the screenshot:

4 I used Simple mode, items are created& linked automatically for each channel, so yes.
5 No errors. Just info about topic(s) updates.

What is interesting though – when the command topic is configured, the “Control” widget in PaperUI loses the presentation of a state topic: instead of OPEN/CLOSE status it shows nothing. If I deconfigure command topic, the status is shown correctly. But it looks like a widget bug, because in HABPanel I see the status as expected. Also log shows that Thing’s state changes.

Have you seen the follow profile feature that you can apply to an incoming state channel to trigger another Item’s command path?


To be clear about what commandTopic does, the incoming message from MQTT is converted into an openHAB command instead of a state update.
You would normally use that for something like a control panel, a user interface.

EDIT - that’s rubbish, it describes postCommand parameter !!
commandTopic does the opposite to that.

You probably do not want that, as you want to use advanced features linking channels.

Well its a bit confusing. I thought it looks like this:

But you say its an openHAB command. You are not talking about mqtt message, right?

Quite right - I was talking rubbish, and got confused with postCommand.:crazy_face:

postCommand takes an MQTT message and turns it into an openHAB command.

commandTopic takes an openHAB command and turns it into an MQTT message.

Btw, isnt’ this command topic Thing setting duplicating the publish from a Rule functionality?

How are you issuing the command?

What type of item is the channel linked to? You’ve mentioned on and off but also open closed. Those are commands for to different types of items.

You use a Thing when the topic you are publishing to is know ahead of time. Use a Rule and the Action when the topic you need to publish to changes or needs to be calculated instead of being hard coded.

How are you issuing the command?

I am posting to the state topic, and I expect the Thing to post to command topic when state message comes. I am using MQTT Explorer for that. Also the ESP module is updating this topic from time to time.

What type of item is the channel linked to? You’ve mentioned on and off but also open closed.

Actually its Contact, I referenced it as on/off by mistake. Payload is 0/1. If I remove the command topic part, Thing correctly shows its state in PaperUI Control widget.

As an experiment, I created another Thing of the Switch type. Also configured State and Command mqtt topics, Is command too. The Control widget has an On/Off control. It acts funny: when I slide it left/right manually from UI, commands are sent and my relay clicks. When I am updating mqtt state (incoming) topic, the switch visually changes its state as expected but no message comes to command topic. Neither relay clicks not MQTT Explorer shows me the message, so I assume it is not being sent for real.

Will not happen. When an MQTT message arrives at openHAB, the stateTopic willpost it to a channel and onwards to an Item.
If an openHAB command arrives at an Item, a channel will pass it a binding, and in this case the MQTT binding will use the commandTopic to create an outbound MQTT message.
Note that there is no link between inbound state and outbound command. That’s quite deliberate, state and command kept separate else practically everything would end up in an action/response/action loop.

You need to do something else to create that link.
One way is rules - listen for a state update, issue a command.

Another way is follow profile - configure a channel to convert incoming state to a command on an Item. Binding can then process command in the usual way. This needs an Item with two channels.

Another way is to use postCommand to cause the binding to massage incoming MQTT into an openHAB command, which the MQTT binding can once again use for an outgoing message. This needs to be done via an Item with two channels.

An extra complication is that Contact type Items in openHAB do not handle commands, so probably not suitable for the way you are working here.`

Thanks, rossko57!