Openhab2 + Mqtt + json

Hello!
I’m newbie and I have some questions.)
I installed openhab2 and Mosquitto to my Raspberry Pi 3.
Mosquitto is work properly, because I can connect to broker via my Android device and I see data from esp8266 with DHT11 and MQ9. Data send in json, and it look like this:
{“humidity”:24,“temperature”:22,“gas”:120}
My questions:
I want to add this data to openhab. What I need to write in the mqtt.cfg, file.items and file.sitemaps?
Why I have “HOME” sitemap by default and how I can add my data to this sitemep?
Thank you.

Hi,

The community is trying to help everyone, as well as yourself. We will be forever grateful if you could firstly read the documentation (as scarce as it may seem), found at:
http://docs.openhab.org/
There are all the information you need with of course a little reader’s patience! Take a look at the MQTT binding in the docs page!

Thank you,

George

1 Like

Thank you for your answer.
Of course, I tryed some days to configurate it myself.

Is it right configuration for receive my json as string?
file mqtt.cfg:
broker.url=tcp://localhost:1883
broker.clientId=openhab

file items:

String MyString "Value: [%s]" {mqtt="<[broker:events/esp8266/sensors:state:default]"}

file sitemap:

sitemap main1 label="Main Menu"
{
        Frame {
                Text item=MyString label="Now [%s °C]"
        }
}

I see only “Now -C” in OpenHAB Android app and “Now NULL” in Habmin.

First, you don’t need to add the “label” field to your sitemap if you already define a text label in the Items file.

Second, you need to use the JsonPath transform (see https://github.com/openhab/openhab1-addons/wiki/Transformations and JsonPath section) to pull out the value you need from the Json string.

To test, I would first leave the Item definition as is, and remove the “label” field from your Sitemap definition, just to make sure you’re receiving the MQTT string correctly. After that, you can start applying the proper transform to pull out the data you need.

P.S. Make sure you have the JsonPath Transform addon installed in your Paper UI’s Addons section.

1 Like

Thank you very much!
I do not know that I have changed, but I see json string in openhab UI now.

Simple tutorial for config OpenHAB2 + MQTT, (String as result).
In first you need to be sure that Mosquitto and sensor is work properly. For that install app:

sudo apt-get install mosquitto-clients

then run mosquitto_sub command:

mosquitto_sub -t [your/topic] -q 1

[your/topic] look like events/esp8266/sensors for example.
You must see data from your device.

file /etc/openhab2/items/test.items

String MyString "Value: [%s]" {mqtt="<[broker:[your/topic]:state:default]"}

file /etc/openhab2/sitemaps/main1.sitemap

sitemap main1 {
        Frame {
                Text item=MyString
        }
}

file /etc/openhab2/services/mqtt.cfg

broker.url=tcp://localhost:1883
broker.clientId=openhab
1 Like