Exporting item state to mqtt

Hello there,

I’m stuck in solving a problem.
What do I want to do?
I’m getting values from different sensors (e.g. Homematic, Zigbee/Deconz, …) and want them to be published to some MQTT topics.
For example I have an item which is linked to the temperature of an aqara zigbee sensor and this temperature should be published to “openhab/some/queue/topic”.
The MQTT Broker is working with other mqtt stuff and I’m subscribing to mqtt topics with openhab successfully.
I tried to link the item (for example the zigbee thing/channel) additionally to an mqtt thing/channel and configured a command topic for publishing.
My expectation was, that if the item value is changing (as the temperature reading changes) a mqtt message should be published. But surprisingly nothing happened. No message was published.
Maybe this is, because the item is only “updated” without a command?

But how could I easily achieve what I want to do without a lot of work? Of course I could create a second item, which is linked to the mqtt thing/channel and send changes as commands via rules, but this would tripple the work as I would have to maintain additional items and rules.

I would be very grateful for any help.

Regards
Sven

Correct.

Two ways:

  1. Configure the Follow Profile on the link. Updates from the Channel get forwarded as commands to the second Channel Configure the MQTT Channel as the second.

  2. MQTT Event Bus [4.0.0.0;4.9.9.9]

This approach will be a lot less work than you think though.

First of all, you wouldn’t necessarily need extra Items and it would just be one rule.

  1. Add all the Items you care about to a Group
  2. Create a rule triggered by changes to the Group
  3. Use the MQTT binding publishMQTT Action to publish the state to a topic generated based on the name of the triggering Item.

It’d be like four lines of code. But 1 or 2 above would be even less work.

Thank you for you quick reply.
I tried the first way using the Follow Profile and it seems to work.
Maybe I also try the second way once I migrated to OH 4.

I have approximately the same problem.
I have a String type item that receives the average home temperature.
I linked it to the channel, which is the topic openhab/temperature/avgTemp/command
I have enabled the follow option, but I don’t see anything publish when the item string is updated

Please post the complete channel definition, otherwise we can only guess what’s wrong.
At first sight, your topic says command, but a temp sensor does not receive commands, it has a state only.

UID: mqtt:topic:6176e35129:c34e05cf74
label: DST termostato
thingTypeUID: mqtt:topic
configuration: {}
bridgeUID: mqtt:broker:6176e35129
location: Corridoio notte
channels:
  - id: setpoint
    channelTypeUID: mqtt:number
    label: Setpoint
    description: ""
    configuration:
      commandTopic: openhab/dst2/setpoint/command
      unit: °C
      formatBeforePublish: "%1f"
      stateTopic: openhab/dst2/setpoint/state
  - id: DST
    channelTypeUID: mqtt:switch
    label: Stato Termostato
    description: ""
    configuration:
      commandTopic: openhab/dst2/sistema/command
      stateTopic: openhab/dst2/sistema/state
  - id: termosifoni
    channelTypeUID: mqtt:switch
    label: Stato Termosifoni
    description: ""
    configuration:
      commandTopic: openhab/dst2/termosifoni/command
      stateTopic: openhab/dst2/termosifoni/state
  - id: temperaturacasa
    channelTypeUID: mqtt:number
    label: Temperatura casa
    description: ""
    configuration:
      formatBeforePublish: "%2f"
      commandTopic: openhab/temperature/avgTemp/command
  - id: umiditacasa
    channelTypeUID: mqtt:number
    label: Umidità Casa
    description: ""
    configuration:
      formatBeforePublish: "%1f"
      commandTopic: openhab/humidity/avgHumidity/command
      postCommand: true

with OH2 I had created an item string that sent the temperature value to the thermostat and displayed it on the display and a rule that updated the item string with the average of the values taken from the sensors. In OH3 I managed to do almost everything, except that I can’t publish the value of the item string to the thermostat.

String avgTempsMQTT			        "Temperatura interna              [%.1f °C]"   <temperature>  (avgTemps)                       {mqtt=">[mosquitto:openhab/temperature/avgTemp/command:command:*:default]"}

the OH2 item…
Thanks for your help

Shouldn’t this be

%.1f

Missing the dot ???

:wink: yes

but it doesn’t solve the problem

Why a String instead of a Number?

The follow profile causes one Channel to follow the state updates from another Channel. Simply changing the state of the Item will not activate the follow profile. You don’t have another Channel linked to the Item here so the follow profile does nothing for you.

Where is this average temperature coming from? A rule?

But it’s a String Item. You can’t link a number Channel to a String Item and expect everything to work. In particular, %1f only works with Numbers but because it’s a String Item you don’t have a number to format here, you’ve a String. In truth, I’m astonished [%.1f °C] wasn’t throwing errors in OH 2.5 too. %.1f only works with Numbers, not Strings.

  1. Use a Number:Temperature Item, not a String Item; set the unit metadata to °C. Or just use a plain Number Item but change your MQTT config to remove the unit: °C. Either you need to use units or you don’t.

  2. Link this Number:Temperature Item to the MQTT Channel without any profiles. There’s nothing to “follow” here.

  3. Whatever rule is calculating the average temperature needs to sendCommand the new value to the Item. Only Item commands get sent on to the binding and here out as a message over MQTT. Updates never leave openHAB.

That’s it. In short, just configure this Item to be what it is.

perfect, now it’s work!! thanks