# Basic Question on MQTT Binding

**URL:** <https://community.openhab.org/t/basic-question-on-mqtt-binding/12487>\
**Category:** Bindings\
**Tags:** sensor, mqtt\
**Created:** [July 18, 2016, 10:20am UTC](https://community.openhab.org/t/basic-question-on-mqtt-binding/12487 "2016-07-18T10:20:51Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![demarcog](https://community.openhab.org/letter_avatar_proxy/v4/letter/d/5daacb/32.png) [@demarcog](https://community.openhab.org/u/demarcog)\
**Post date:** [July 18, 2016, 10:20am UTC](https://community.openhab.org/t/basic-question-on-mqtt-binding/12487/1 "2016-07-18T10:20:52Z")

</div>

I am trying to test MQTT binding.  
I put a simple test item in my test.items.  
`Number Temp1 "Temperature [%1,f]" {mqtt="<[broker:openhab/temp:state:default]"}`

Now, suppose I would like to know from another MQTT client the state of the sensor, i.e. I would read  
the temperature value. Which kind of message I have to publish on the topic openhab/temp?

---

<div class="post-metadata">

**Author:** ![watou](https://community.openhab.org/user_avatar/community.openhab.org/watou/32/70595_2.png) [@watou](https://community.openhab.org/u/watou)\
**Post date:** [July 18, 2016, 10:26am UTC](https://community.openhab.org/t/basic-question-on-mqtt-binding/12487/2 "2016-07-18T10:26:26Z")

</div>

The item label should probably be `"Temperature [%.1f]"`. On the topic `openhab/temp` on the broker `broker`, you would publish a message like “23.2” or “0.6” or “93”, etc. (without the quote marks). Note that additional formatting, like spaces before or after the number, is not allowed, unless you also replace `default` with a transform that strips them off.

---

<div class="post-metadata">

**Author:** ![demarcog](https://community.openhab.org/letter_avatar_proxy/v4/letter/d/5daacb/32.png) [@demarcog](https://community.openhab.org/u/demarcog)\
**Post date:** [July 18, 2016, 10:39am UTC](https://community.openhab.org/t/basic-question-on-mqtt-binding/12487/3 "2016-07-18T10:39:42Z")

</div>

Thank you watou.  
Maybe there is something I am missing. The pub message is sent by the sensor, by means of some script or persistence binding.  
Let’s say every minute. But if I am on a client which submitted to the sensor channel (openhab/temp), there is a way to know which is the current value of the sensor? Is it possible by using MQTT or shall I use other mechanism, like REST?

I am asking this because I am making a webpage which connects to the borker via WebSocket. And I would know the reading of the sensor by publishing some message on the channel, like “send temperature reading to the borker”.

Regards

---

<div class="post-metadata">

**Author:** ![rlkoshak](https://community.openhab.org/user_avatar/community.openhab.org/rlkoshak/32/75251_2.png) [@rlkoshak](https://community.openhab.org/u/rlkoshak)\
**Post date:** [July 18, 2016, 1:56pm UTC](https://community.openhab.org/t/basic-question-on-mqtt-binding/12487/4 "2016-07-18T13:56:41Z")

</div>

I’m not 100% certain I understand your question, but I did implement something similar to what you describe in my setup. I have a topic that all my sensors subscribe to. When a message is published to that topic the sensors publish their current readings to their respective topics. I mainly use this to give OH a way to poll for their current state in a System started rule so OH’s internal state matches the current state of the real world after a restart.

---

<div class="post-metadata">

**Author:** ![demarcog](https://community.openhab.org/letter_avatar_proxy/v4/letter/d/5daacb/32.png) [@demarcog](https://community.openhab.org/u/demarcog)\
**Post date:** [July 18, 2016, 4:06pm UTC](https://community.openhab.org/t/basic-question-on-mqtt-binding/12487/5 "2016-07-18T16:06:31Z")

</div>

I think you got precisely what I would like to do.  
Now I did it by doing a nodejs script on the openhab hosting platform which listens on a specific channel to send REST request to Openhab and publish the results back to the broker.  
It will be great if could show me the details of what you did: Did you write a script or somethingelse?  
Regards

---

<div class="post-metadata">

**Author:** ![rlkoshak](https://community.openhab.org/user_avatar/community.openhab.org/rlkoshak/32/75251_2.png) [@rlkoshak](https://community.openhab.org/u/rlkoshak)\
**Post date:** [July 18, 2016, 5:15pm UTC](https://community.openhab.org/t/basic-question-on-mqtt-binding/12487/6 "2016-07-18T17:15:23Z")

</div>

All of my sensors are currently running off of Raspberry Pis (GPIO, network, and BT dongles). The script I use is published here:

> **[GitHub - rkoshak/sensorReporter: A python based service that receives sensor inputs...](https://github.com/rkoshak/sensorReporter/)**
>
> A python based service that receives sensor inputs and publishes them in various ways.

The openHAB Item and Rule for requesting an update is:

Item:

```
Switch T_Update "Request update from MQTT sensors" <network>
String N_D_Update { mqtt=">[mosquitto:entry_sensors/getUpdate:state:*:default]" }

```

Rule:

```java
rule "Get update at startup"
when
    System started or
    Item T_Update received command
then
    logInfo("entry", "Requesting an update from the MQTT sensors")
    N_D_Update.postUpdate("update")
end

```

Another developer added code to use the REST API instead of MQTT but I haven’t looked to see if it supports this request for status update feature.
