Best practice to read mqtt json values into openhab items?

I am using SMA Solar system in combination with SBFspot software to read the data via Bluetooth.
The SBFspot publish the data via MQTT in the following format:

{“Timestamp”: “26/01/2021 09:16:04”,“SunRise”: “26/01/2021 07:07:00”,“SunSet”: “26/01/2021 16:07:00”,“InvSerial”: 2100433119,“InvName”: “SN: 2100433119”,“InvTime”: “26/01/2021 09:15:57”,“InvStatus”: “Ok”,“InvTemperature”: 48.340,“InvGridRelay”: “Closed”,“EToday”: 0.037,“ETotal”: 36975.379,“PACTot”: 40.000,“UDC1”: 216.850,“UDC2”: 216.380,“IDC1”: 0.103,“IDC2”: 0.101,“PDC1”: 21.000,“PDC2”: 21.000}

How can I configure the mqtt things/items to read those values ?
Thanks
Thomas

What is your openHAB version, and are you using files or the UI?

Assuming you are already familiar with Things and Items:

  1. Install the JSONPATH Transformation Service in openHAB.
  2. Create a separate Channel in your Thing for each value or string that you would like to extract.
  3. For each Channel, subscribe to the relevant MQTT topic.
  4. For each Channel, use the JSONPATH transformation to extract the value of interest in the form
JSONPATH:$.path.to.value

Hi,

Have OH3 and text configuration files.
attached my mqtt configuration, could you guide me with a short example ?
JSONpath transformation is installed.

thanks
Thomas

Switch  eib_backlight 		"Eib Display Backlight"  					(Status)  { channel="mqtt:topic:mysecureBroker:display:onoff", autoupdate="true" }
Switch  mqtt_relais_0 		"mqtt_relais_0"  							(Status)  { channel="mqtt:topic:mysecureBroker:relais_0:onoff", autoupdate="true" }
Switch  mqtt_relais_1 		"mqtt_relais_1"  							(Status)  { channel="mqtt:topic:mysecureBroker:relais_1:onoff", autoupdate="true" }

Number  bme280_temperature   "Temperature [%.1f °C]"   <temperature> (Status,Box)  { channel="mqtt:topic:mysecureBroker:bme280:bme280_temperature", autoupdate="true" }
Number  bme280_humidity      "Humidity [%.1f %%]"      <humidity>    (Status,Box)  { channel="mqtt:topic:mysecureBroker:bme280:bme280_humidity", autoupdate="true" }
Number  bme280_pressure      "Pressure [%.1f hPa]"     <pressure>    (Status,Box)  { channel="mqtt:topic:mysecureBroker:bme280:bme280_pressure", autoupdate="true" }

Number  ads1115_voltage0     "ADC0 humidity raw"        <humidity>    (Status,Box)  { channel="mqtt:topic:mysecureBroker:ads1115:ads1115_voltage0", autoupdate="true" }
Number  ads1115_voltage1     "ADC1 humidity raw"      	<humidity>    (Status,Box) 	{ channel="mqtt:topic:mysecureBroker:ads1115:ads1115_voltage1", autoupdate="true" }
Number  ads1115_voltage2     "ADC2 humidity raw"      	<humidity>    (Status,Box)  { channel="mqtt:topic:mysecureBroker:ads1115:ads1115_voltage2", autoupdate="true" }
Number  ads1115_voltage3     "ADC3 humidity raw"      	<humidity>    (Status,Box)  { channel="mqtt:topic:mysecureBroker:ads1115:ads1115_voltage3", autoupdate="true" }


String  sma_data       "SMA Data"    	<temperature>    (Status)      { channel="mqtt:topic:mysecureBroker:sbfspot:sma_data", autoupdate="true" }
Bridge mqtt:broker:mysecureBroker [ host="192.168.1.11", secure=false, username="openhabian", password="XXXXXXXXX" ]

{
    Thing topic display {
    Channels:
		Type switch : onoff "Eib Display Backlight" [ commandTopic="eibdisplay/output/backlight/set", on="ON", off="OFF" ]
    } 
	
		Thing topic relais_0 {
    Channels:
		Type switch : onoff "mqtt_relais_0" [ commandTopic="mqtt1/output/gpio20/set", on="ON", off="OFF" ]
    }
	Thing topic relais_1 {
    Channels:
		Type switch : onoff "mqtt_relais_1" [ commandTopic="mqtt2/output/gpio20/set", on="ON", off="OFF" ]
    } 
	Thing topic bme280 {
    Channels:
		Type number : bme280_temperature   "Temperature"	[stateTopic="mqtt2/sensor/bme280_temperature"]
		Type number : bme280_humidity    "Humidity"		[stateTopic="mqtt2/sensor/bme280_humidity"]
		Type number : bme280_pressure   "Pressure"		[stateTopic="mqtt2/sensor/bme280_pressure"]
    } 
	Thing topic ads1115 {
    Channels:
		Type number : ads1115_voltage0   "Humidity"		[stateTopic="mqtt2/sensor/ads1115_voltage0"]
		Type number : ads1115_voltage1   "Humidity"		[stateTopic="mqtt2/sensor/ads1115_voltage1"]
		Type number : ads1115_voltage2   "Humidity"		[stateTopic="mqtt2/sensor/ads1115_voltage2"]
		Type number : ads1115_voltage3   "Humidity"		[stateTopic="mqtt2/sensor/ads1115_voltage3"]
    }

 }

We can try. What is the MQTT topic that your device publishes the JSON string to?

Great, thanks for the help !

in the MQTT explorer it show up like this:

sbfspot={“Timestamp”: “26/01/2021 11:02:07”,“SunRise”: “26/01/2021 07:07:00”,“SunSet”: “26/01/2021 16:07:00”,“InvSerial”: 2100433119,“InvName”: “SN: 2100433119”,“InvTime”: “26/01/2021 11:02:04”,“InvStatus”: “Ok”,“InvTemperature”: 49.370,“InvGridRelay”: “Closed”,“EToday”: 0.287,“ETotal”: 36975.629,“PACTot”: 163.000,“UDC1”: 229.070,“UDC2”: 237.150,“IDC1”: 0.327,“IDC2”: 0.316,“PDC1”: 75.000,“PDC2”: 75.000}

this doesn’t work

Number sma_InvTemperature “SMA InvTemperature” (Status) { channel=“mqtt:topic:mysecureBroker:sbfspot:JSONPATH($.InvTemperature)”, autoupdate=“true” }

Thing topic SMA {
Channels:
	Type number : sma_temp "SMA Temperatur" 		[ commandTopic="sbfspot/sma_InvTemperature"]
} 
2021-01-26 12:05:20.865 [INFO ] [el.core.internal.ModelRepositoryImpl] - Loading model 'mqtt.items'

2021-01-26 12:05:20.896 [ERROR] [el.item.internal.GenericItemProvider] - Binding configuration of type 'channel' of item 'sma_InvTemperature' could not be parsed correctly.

org.openhab.core.model.item.BindingConfigParseException: UID segment 'JSONPATH($.InvTemperature)' contains invalid characters. The last segment of the channel UID must match the pattern '[\w-]*|[\w-]*#[\w-]*'.

	at org.openhab.core.model.thing.internal.GenericItemChannelLinkProvider.createItemChannelLink(GenericItemChannelLinkProvider.java:86) ~[?:?]

	at org.openhab.core.model.thing.internal.GenericItemChannelLinkProvider.processBindingConfiguration(GenericItemChannelLinkProvider.java:76) ~[?:?]

	at org.openhab.core.model.item.internal.GenericItemProvider.internalDispatchBindings(GenericItemProvider.java:372) [bundleFile:?]

	at org.openhab.core.model.item.internal.GenericItemProvider.internalDispatchBindings(GenericItemProvider.java:341) [bundleFile:?]

	at org.openhab.core.model.item.internal.GenericItemProvider.processBindingConfigsFromModel(GenericItemProvider.java:212) [bundleFile:?]

	at org.openhab.core.model.item.internal.GenericItemProvider.modelChanged(GenericItemProvider.java:407) [bundleFile:?]

	at org.openhab.core.model.core.internal.ModelRepositoryImpl.notifyListeners(ModelRepositoryImpl.java:301) [bundleFile:?]

	at org.openhab.core.model.core.internal.ModelRepositoryImpl.addOrRefreshModel(ModelRepositoryImpl.java:139) [bundleFile:?]

	at org.openhab.core.model.core.internal.folder.FolderObserver.checkFile(FolderObserver.java:249) [bundleFile:?]

	at org.openhab.core.model.core.internal.folder.FolderObserver.processWatchEvent(FolderObserver.java:312) [bundleFile:?]

	at org.openhab.core.service.WatchQueueReader.lambda$3(WatchQueueReader.java:322) [bundleFile:?]

	at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:515) [?:?]

	at java.util.concurrent.FutureTask.run(FutureTask.java:264) [?:?]

	at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:304) [?:?]

	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128) [?:?]

	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628) [?:?]

	at java.lang.Thread.run(Thread.java:834) [?:?]


That’s because you need to put the transform into the Channel itself, as per

amongst other issues.

Maybe try:

Thing topic SMA {
    Channels:
	    Type number : sma_temp "SMA Temperatur" [
            stateTopic="sbfspot",
            transformationPattern="JSONPATH:$.InvTemperature"
        ]
}

with the Item:

Number sma_InvTemperature "SMA InvTemperature" (Status) { channel="mqtt:topic:mysecureBroker:SMA:sma_temp" }

THANKS a lot !

working fine now.
Thanks
Thomas

1 Like