Send Temperature to MQTT

System:
Raspberry Pi 4 (4GB)
Openhab 3.2
MQTT Binding 3.2
Mosquitto MQTT server

I have been setting up the MQTT binding with one Broker and Generic MQTT Thing. I am able to get values from topics in the server. But I need to take the temperature from a sensor in openhab and send its state to the MQTT server. I have tried both via a channel on the Generic MQTT Thing by linking it to the temperature item and setting topic name in the command field, but it does not send any values to the MQTT server. Tried a switch Item in this way and that worked, so I tought that temperature Items maybe is not supported to send a command to MQTT server. I then tried using rules but I can only get it to send strings I write in the rule not dynamic vales from the temperature item.

What would be the best way to send my item state (number) to the MQTT server?

Thanks!

Please post the Item and Thing configurations which have failed.

Here is the Generic MQTT thing. As mentions I get communication, but only one way from the MQTT server to openhab not the other way.

Here is my attemt at a rule

rule "Send temp MQTT"

    when
        Item test changed
    then
        var temp_ute = temp_a_ute.state
        val mqttActions = getActions("mqtt","mqtt:broker:RaspberryPi_Grimstad")
        mqttActions.publishMQTT("mytopic", "temp_ute" , true)
    end

Your temperature sensor Item does not ordinarily get commands, I would think? And as you have worked out, you need a command to publish to MQTT.

Follow profile applied to a link between your Item and the MQTT channel should do what you need - it will act on changes to your Item state and pass them to the MQTT channel as though they were commands.

1 Like

Not sure I understand this but the temperature item gets changed when the sensor send a new temperature. I use the tellstick binding for that and that workes great.

So what you meen than is that rules would not be the way to go here?
I edited the link with the Follow Profile like this:


Is that what you ment?

I still can not see that the state have been passed over to the MQTT server though

It’s important to grasp the difference between command and state as used with internal openHAB Items.
command - “do something” - turn the light on, raise the rollerblind
state - “condition report” - the light is on, the rollershutter is at position 75%

So an Item linked to a temperature sensor never normally gets a command - you can’t instruct a sensor to make the environment hotter. Reports coming from your tellstick binding only update the Item state.

No.

That makes sense, thanks.

I have changed it to Follow in the link. But still nothing.
Do have to configure something else than MQTT Command Topic?

I have created a other virtual item to read from the same topic just to check that its not my other MQTT client that have a problem here. But it do not get updated either.

So I think I figured it out. It had to due with the Follow Profile, but also my temperature item was defined as Number:Temperature and therefore haveing a C (for celcius) and therefore not sendig anything. Changed the item to just Number and it seems to be working now. Thanks for the guidense.

Remember, follow profile only does something when the Item state changes.

I’ve never seen an MQTT topic without / characters, is that allowed?

A useful way to look at activity in MQTT is a third party tool like mqtt.fx, or to look at your broker’s log.

Your openhab.log may report problems, always worth a look.

Your temperature Item was sending something, but not anything a plain number MQTT channel could handle. You could use a string type channel.

Yes i know that, and my temperature is changing a lot now as we approche night here so that is good for my testing.

The topic has a name “temp”

Yes the value was not a pure number therefore noting was sent. I have a desktop application for windows that reads all the topics on the broker so when I tested with a string I saw it. But for my setup I need it to be a number because I need to do maths with it.

Yes was looking here but nothing related to MQTT was showing here.

The soulution than was to change the item to a pure Number item not Number:Temperature

It’s a clumsy solution, there’s nothing to stop you linking a Number:Temperature type Item to an MQTT string type channel. Using transformations, you can choose to publish with or without units.

MQTT binding does not yet have good handling of Quantity types, with units, but hopefully will be improved one day.

Yes that would be work. But on my other client which are reading the value want it to be a pure number value. And for me it does not matter if the item is a Number or Number:Temperature. Using a transformation would require one extra step which was not nessecery here.