[SOLVED] Publishing number values from openHAB to MQTT

Hello everyone,

I’m new to openHAB and MQTT. To learn how to use both together I have them set up as follows:

  • I use the docker image of openHAB 3.2.0 and eclipse mosquitto 2.0.14
  • Ubuntu 20.04.4 is running with VMWare on an intel® Core™ i7-8550U Windows PC

So far I managed to configure switches by creating a topic with MQTT.fx, using a local MQTT broker and creating a MQTT thing with a channel set to said topic and an switch item. It works without trouble and status and changes are observable on my MQTT Explorer as I use the switch on the openHAB GUI.

here is the yaml of the MQTT Thing channel:

UID: mqtt:topic:d49d2ed101:96e2c089e0
label: Test Switch
thingTypeUID: mqtt:topic
configuration: {}
bridgeUID: mqtt:broker:d49d2ed101
location: Labor Raum 2
channels:

  • id: TestSwitch
    channelTypeUID: mqtt:switch
    label: Test Switch
    description: “”
    configuration:
    commandTopic: labor/raum2/testswitch
    stateTopic: labor/raum2/testswitch
    off: OFF
    on: ON

Now I want to send my used memory value from the systeminfo binding to the MQTT topic. But no values reach the topic. I can only publish values with MQTT.fx which are shown in openHAB.
This yaml shows how I set up the channel in a way, that I can send values with MQTT.fx to openHAB and they stay in MQTT Explorer but will be overwritten in openHAB only by the systeminfo item.

UID: mqtt:topic:d49d2ed101:SysteminfoRAM
label: Systeminfo RAM
thingTypeUID: mqtt:topic
configuration: {}
bridgeUID: mqtt:broker:d49d2ed101
location: Labor Raum 1
channels:

  • id: SysteminfoRAMused
    channelTypeUID: mqtt:number
    label: Systeminfo RAM Used
    description: “”
    configuration:
    unit: MB
    step: 0
    qos: 0
    stateTopic: labor/raum1/notebook/systeminforam

My Question/Problem is, how do I get the number value from the systeminfo binding to my topic with openHAB to MQTT?

Thank you all in advance!

Indentation is critical in YAML. Please pose using code fences.

```
code goes here
```

From a quick look, you’ve only defined a stateTopic. If you want to publish something on a command to an Item, you need to define a commandTopic. Then when the Item linked to that Channel receives a command, the command will be published as an MQTT message. If you only have a stateTopic, the Channel is read only and OH will not ever publish to them.

1 Like

Hello and thank you for your reply!

I added the command topic to the channel. Unfortunately the values of the item still won’t be published.

label: Systeminfo RAM
thingTypeUID: mqtt:topic
configuration: {}
bridgeUID: mqtt:broker:d49d2ed101
location: Labor Raum 1
channels:
  - id: SysteminfoRAMused
    channelTypeUID: mqtt:number
    label: Systeminfo RAM Used
    description: ""
    configuration:
      retained: false
      postCommand: false
      unit: MB
      qos: 0
      commandTopic: labor/raum1/notebook/systeminforam
      step: 0
      stateTopic: labor/raum1/notebook/systeminforam

I wonder if it has something to do with the docker containerization of openHAB and mosquitto.

I’m eager to hear more thoughts and tips from the community and thank you all in advance.

You’re not sending any commands to your Item though, are you?
The general openHAB process to communicate to the outside world is to send a command to an Item.

The counterpart process, where the world tells us something, arrives at Items as a state update.
Two deliberately different processes, as they must be if we don’t want to go round and round in loops.

It sounds like your Item is getting state updates from here?
The MQTT binding won’t act on those, not commands.

There is a tool to work around this and bend the usual architecture.
Apply a follow profile to the link between your Item and the MQTT binding. This will change the usual action, so that an Item update gets presented to MQTT binding as though it were a command instead.

And don’t have any stateTopic at all on your MQTT channel - there you are building a loop.

2 Likes

Hello rossko, it worked!

It is as you said. By removing the state Topic and using the command Topic + the follow profile my simple demo works now as I wanted!

image

UID: mqtt:topic:d49d2ed101:SysteminfoRAM
label: Systeminfo RAM
thingTypeUID: mqtt:topic
configuration: {}
bridgeUID: mqtt:broker:d49d2ed101
location: Labor Raum 1
channels:
  - id: SysteminfoRAMused
    channelTypeUID: mqtt:number
    label: Systeminfo RAM Used
    description: ""
    configuration:
      retained: false
      postCommand: false
      unit: MB
      qos: 1
      commandTopic: labor/raum1/notebook/systeminforam
      step: 0

Thanks everyone for your help and explenations! It was my first time asking in a community and I was nervous, but thanks for the positive experience!

Best Regards