MQTT: send message automatically after state update

  • Platform information:
    • Hardware: RasPi 3
    • OS: Openhabian
    • openHAB version: openHAB 2.5.10 Release Build

I have an RasPi running OpenHabian and an arduino, which I use for controlling inputs and outputs.
I have a button connected to the arduino as well as a LED. Pressing the button shall update an item in OpenHab. This update should lead to switch the LED on or off.

The item is configured with state topic Button1/buttonState and and command topic Button1/buttonControl.

When I press the hardware button connected to the arduino I can see an update of the item in OpenHab. Also, when I update the item manually I can see that the arduino gets the notification as well. So, basically evertyhing works.

But it seems to me, that the item doesn’t automatically send a command when it gets updated. I somehow expected exactly this behaviour. Is this something I can configure somewhere?
Everytime it receives the message that the button was pressed it should send a command to every subscriber that the button actually was pressed.

What am I missing here?

Thanks!

You will have to show your configuration because there is a lot of ambiguity in what you describe and what you are asking.

Please explain this statement.

Items don’t send commands when they are updated.

In relation to event-based rule triggers the manipulator commands sendCommand and postUpdate act differently. The following table summarizes the impact of the two manipulator commands on the rule execution due to the used trigger:

In relation to event-based rule triggers the manipulator commands sendCommand and postUpdate act differently. The following table summarizes the impact of the two manipulator commands on the rule execution due to the used trigger:

Command \ Rule Trigger received update received command changed
postUpdate ⚡ rule fires (depends)
sendCommand (❌) see below ⚡ rule fires (depends)
Change through Binding ⚡ rule fires ⚡ rule fires (depends)

Do you use a separate MQTT client to keep an eye on the MQTT messages to make sure you don’t have a denounce issue?

If you have multiple bindings on the same item you may need to include a postCommand setting in your openhab MQTT thing,

As @rlkoshak points out there is not enough information in your post to know exactly how to help you. Please post your Thing, items and rule config (if you have any)

Screenshots can help heaps or just copy paste config files using code fences,

364b0639df751f9346f9ea7559d0c03b903c3298

@denominator my assumption was that everytime I update a value in OpenHab all subscribers to this value get notified about this update. But it seems to be wrong.

I just started using OpenHab, so I don’t thing I have any fancy stuff :slight_smile: I have just a switch item.
I am not sure what config do you need exactly. But I can try to provide as much as you need. Since now, I don’t have configured any rules.

Here you can see the thing:

        {
      "acceptedItemType": "Switch",
      "kind": "STATE",
      "uid": {
        "segments": [
          "mqtt",
          "topic",
          "ca70548f",
          "Button1_ONOFF"
        ]
      },
      "channelTypeUID": {
        "segments": [
          "mqtt",
          "switch"
        ]
      },
      "label": "Button1_ONOFF",
      "configuration": {
        "properties": {
          "commandTopic": "Button1_ONOFF/buttonControl",
          "stateTopic": "Button1_ONOFF/buttonSate"
        }
      },
      "properties": {},
      "defaultTags": []
    }

And the item:

  "Button1_ONOFF": {
"class": "org.eclipse.smarthome.core.items.ManagedItemProvider$PersistedItem",
"value": {
  "groupNames": [],
  "itemType": "Switch",
  "tags": [],
  "label": "Button1_ONOFF"
}

}

What I want to have: everytime there is an ON or OFF state send by the arduino I want to notify all subscribers about this status change, meaning I have to send the corresponding commandTopic.

I assume I have to write a rule for this?

Thanks!

Welcome to openhab just when you finish learning how to get started the whole UI is going to change in OH3 but most of the concepts are the same.

It looks like you have not linked the items.

Here is an example of a thing with 2 channels

{
  "statusInfo": {
    "status": "ONLINE",
    "statusDetail": "NONE"
  },
  "editable": false,
  "label": "Anabel FAN",
  "bridgeUID": "mqtt:broker:myMQTTBroker",
  "configuration": {
    "payloadNotAvailable": "Offline",
    "availabilityTopic": "AIFAN/tele/LWT",
    "payloadAvailable": "Online"
  },
  "properties": {},
  "UID": "mqtt:topic:myMQTTBroker:fan2",
  "thingTypeUID": "mqtt:topic",
  "channels": [
    {
      "linkedItems": [
        "Anabel_light",
        "AnabelFANPower1"
      ],
      "uid": "mqtt:topic:myMQTTBroker:fan2:Power1",
      "id": "Power1",
      "channelTypeUID": "mqtt:switch",
      "itemType": "Switch",
      "kind": "STATE",
      "label": "Anabel Light ",
      "defaultTags": [],
      "properties": {},
      "configuration": {
        "commandTopic": "AIFAN/cmnd/POWER",
        "stateTopic": "AIFAN/stat/POWER",
        "off": "OFF",
        "on": "ON"
      }
    },
    {
      "linkedItems": [
        "Anabel_fan",
        "AnabelFANFanspeed"
      ],
      "uid": "mqtt:topic:myMQTTBroker:fan2:fanspeed",
      "id": "fanspeed",
      "channelTypeUID": "mqtt:dimmer",
      "itemType": "Dimmer",
      "kind": "STATE",
      "label": "Anabel Fan",
      "defaultTags": [],
      "properties": {},
      "configuration": {
        "0": "OFF",
        "1": "LOW",
        "2": 100,
        "3": "HIGH",
        "commandTopic": "AIFAN/cmnd/FANSPEED",
        "stateTopic": "AIFAN/stat/FANSPEED",
        "transformationPattern": "JSONPATH:$.FanSpeed"
      }
    }
  ]
}

In paper ui you can check if there is a link

The top three items have no link shown by the dot in the circle.

well, I guess I have done it.

When I press the button which is connected to the arduino I can see the switch in paper ui toggling from ON to OFF. It works fine.
Also when I set the switch in paper ui by hand my LED connected to the arduino switches ON and OFF. But the connection between the button and the LED doesn’t work and I am not sure where to link them.

Just double checking here: openHAB is not an MQTT broker. You have to actively do something in openHAB (a rule, a UI button) to publish something to an MQTT topic.

Yes. What message is your LED expecting, and on what topic?

@hafniumzinc

pressing the button on the arduino sends either an ON or OFF to “Button1_ONOFF/buttonSate”.

At the same time the arduino listens to either an ON or OFF comming from “Button1_ONOFF/buttonControl”.

Now I need a rule that when the switch in OpenHab is set to either ON or OFF it sends an ON or OFF via Button1_ONOFF/buttonControl.

Create a channel and use Button1_ON_OFF/buttonControl as the command topic. Link that to your openHAB Switch Item. And ON command send to that Item will be published to that topic.

OK, I think I get it. You can use a follow profile on the Item. Here’s what worked for me in openHAB2:

OH2 things file

Bridge mqtt:broker:MosquittoMqttBroker "Mosquitto MQTT Broker" [
	host="192.168.1.92",
	secure=false,
	port=1883,
	clientID="OpenHAB2",
	username="",
	password=""
]

Thing mqtt:topic:arduino "Arduino" (mqtt:broker:MosquittoMqttBroker) {
    Channels:
        Type switch: button_state [
            stateTopic="Button1_ONOFF/buttonState"
        ]
        Type switch: led_state [
            commandTopic="Button1_ONOFF/buttonControl",
            on="ON",
            off="OFF"
        ]
}

OH2 items file

Switch sButtonState { channel="mqtt:topic:arduino:button_state", channel="mqtt:topic:arduino:led_state"[profile="follow"] }

Now, if Button1_ONOFF/buttonState receives ON, the sButtonState will forward that to the led_state Thing Channel, which sends ON to the MQTT topic Button1_ONOFF/buttonControl.

@rlkoshak isn’t that exactly what I have done?

it is also linked to my switch item. I can switch the LED on and off when pressing the switch in paper UI, but the LED doesn’t react to incomming messages. So it may be that the switch says ON while the LED is actually OFF.

@hafniumzinc

I don’t have any config files. I just used paper ui so far :frowning:

This whole thing is about states and commands.

When you press your physical button, it will change the state of your openHAB Item.

However, to publish an MQTT message using your Item you would need to command the Item.

You’re trying to use your openHAB Item as some sort of message forwarder, or broker, which it can’t do by default because an incoming message only changes the Item’s state, but wont trigger a command.

My solution using the follow profile (using two channels) is one way of forcing the command. Another method is to use rules.

I’m sure you can use a follow profile using PaperUI, but I don’t use it so I’m no help there!

@hafniumzinc

thanks man! I got it and it is working fine :smirk:

You can set the “follow” when linking the channel to an item. The connection is set to “system:default” by default. But you can chage it to “system:follow”.

Really like it. Now I can move on :slight_smile: