Extract temperature from an MQTT string

Before I start, yes I have RTFM, trouble with the documentation is it shows examples but doesn’t show what the incoming data is so it is hard to work out what the example is doing. I’ve also searched this forum but found nothing that helps.

I have a Tazmotized nodeMCU sending out an MQTT string every 5 min which looks like this;
tele/nodemcu1/SENSOR = {“Time”:“2020-05-30T14:41:21”,“DS18B20”:{“Id”:“0301A2796992”,“Temperature”:24.3},“TempUnit”:“C”} and I want to extract the temperature value to display in my sitemap.
This is my item
Number WaterHeaterTemp “Water Heater Temperature [%.1f °C ]” {mqtt="<[mqtt:topic:efdc987b:DS18B20:SENSOR/Temperature:state:REGEX(.,(.),.*)]"}

and the entry in my sitemap is;
Text item=WaterHeaterTemp

From the documentation it appears I can do that with regex but the example isn’t very helpful.
I would prefer to extract it only if the “id” is correct then I can easily add further sensors later with different Ids.

Looks like you’re using the V1 binding for MQTT. If you’re happy to move to the V2 binding then this may help:

Edit:
I’m now back at a PC. Using the V2 binding your files might includes something like the following:

thing file

	Thing mqtt:topic:nodemcu1 "nodemcu1" {
		Channels:
			Type number : temperature "Temperature" [ 
				stateTopic="tele/nodemcu1/SENSOR",
				transformationPattern="JSONPATH:$.DS18B20.Temperature"
			]
	}

items file

Number nodemcu1Temperature "nodemcu1 Temperature" { channel="mqtt:topic:nodemcu1:temperature" }

sitemap file

Text item=nodemcu1Temperature label="nodemcu1 temperature [%.1f °C]" icon="temperature"

In addition to the above answer:
The most examples are probably using the JSONPATH Transformation, which needs to be installed.
Your received string is a JSON string (Tasmota took care of that).
Regarding the ID, your MQTT setup will subscribe for this item only for messages send by this device (nodeMCU), unless you hook up more sensors to the device the readings are already seperated…

My MQTT binding is V2.5.3, what made you think it is V1?

While my items and sitemap are files I created my thing in PaperUI.
I tried your suggestions and also applied the JSON statement to my thing in PaperUI but none of them returned a result then I noticed that the thing from your file, I called it nodemcu1b to distinguish from my other one, was showing OFFLINE-BRIDGE-OFFLINE in PaperUI.
This is the first time I’ve created a thing from a file, I used PaperUI the other times and I entered your example exactly but I figured it needed a Bridge line as well. When I did that the thing status showed ONLINE but the text editor is showing that “mqtt:topic:nodemcu1” in the Thing line is incorrect. My complete things file reads;
Bridge mqtt:systemBroker:embedded-mqtt-broker [host=“openhab”, secure=false] {
Thing mqtt:topic:nodemcu1 “nodemcu1b” @ “myHome/Outside” {
Channels:
Type number : temperature “Temperature” [
stateTopic=“tele/nodemcu1/SENSOR”,
transformationPattern=“JSONPATH:$.DS18B20.Temperature”
]
}
}

What have I got wrong?

Your things migth be MQTT version2. The items you posted however use the version1 syntax, that way they will never show a value!

BTW when posting code PLEASE use the tags to format your code in a readable way.

This

It’s as simple as that.
That is a link to MQTT binding version 1.
If binding version 1 is not installed, it does nothing.
If binding version 2 is installed or not, it still does nothing.

To link to a channel of any binding (like MQTT version 2) you use syntax
{channel=" ..."}

1 Like

It took me some time to work out what to do with the files too, so no problem! You’re right, you do need the bridge. I don’t use the embedded one myself, so can’t comment on the implementation in your things file - looks OK though.

However, as the others have mentioned, you will have to change your ITEM. The one you posted in your original post is based on syntax for V1 of the binding. As you have V2, you need syntax more like the one in my edits to my first post.

Also, it sounds like you may have things/items already defined in PaperUI. I’m not sure what happens when re-defining them via files (using the same names), but I suspect it’d be better to completely remove any of the things and items defined in PaperUI.

Thank you to all that responded.
I cleaned up my openHAB by deleting the thing created in PaperUI and any references to it in the items and sitemap and now it is working. Visual Studio Code is still showing a syntax error in this part of the thing file, “mqtt:topic:nodemcu1” but it is working. The temperature is low because the sensor is not in the water heater yet and it’s just showing ambient temperature.
image
Now to replace my other things created with PaperUi with files.

Thanks once again, I’m grateful for the help from members of this forum.

1 Like

Now I’m getting to understand more. I think my problem is starting by copying examples in this forum and not taking notice of the date they were posted.
So the “mqtt:topic:nodemcu1” was the old syntax which still worked but changing it to “topic nodemcu1” and VSC does not show an error and again it works.
Now I’ve copied and modified the things file for a SonoffS26 but it’s not working but I’ll call for help in a new thread if I can’t nut out why it is not working.
Thanks again for your help.

I restarted openhab2 and now I’m no longer displaying the temperature.
I’ve looked everywhere but can’t find a reason it was working before the restart.

I reverted to mqtt:topic:nodemcu1 in the things file and all is OK.

I’ve now added two more DS18B20 in parallel with the first one so I need to extract the temperature for each sensor. The mqtt string is;

`tele/nodemcu1/SENSOR = {"Time":"2020-06-18T16:29:40","DS18B20-1":{"Id":"3C01A8160EFB","Temperature":22.9},"DS18B20-2":{"Id":"3C01A8162221","Temperature":42.1},"DS18B20-3":{"Id":"51D7FD1D64FF","Temperature":23.7},"TempUnit":"C"} (retained)`

Before adding the other sensors my thing looked like this;

    Thing mqtt:topic:nodemcu1 "nodemcu1" @ "myHome/Outside" {
            Channels:
                Type number : temperature "Temperature" [ 
                    stateTopic="tele/nodemcu1/SENSOR",
                    transformationPattern="JSONPATH:$.DS18B20.Temperature"
                    ]   
        }
I'm still not fluent in transformation patterns and I tried adding -1 to DS18B20 and also tried substituting it with the id but neither worked. How do I extract each individual temperature?

Replace your current channels with:

Type number : temperature1 “Temperature” [
stateTopic=“tele/nodemcu1/SENSOR”,
transformationPattern=“JSONPATH:$.DS18B20-1.Temperature”
]
Type number : temperature2 “Temperature” [
stateTopic=“tele/nodemcu1/SENSOR”,
transformationPattern=“JSONPATH:$.DS18B20-2.Temperature”
]
Type number : temperature3 “Temperature” [
stateTopic=“tele/nodemcu1/SENSOR”,
transformationPattern=“JSONPATH:$.DS18B20-2.Temperature”
]

As I said above, I’ve done that. It was the first thing I tried but I just see “-” instead of the temperature value.
I’m just doing one for a start to get it right first.
Do I need to modify my items or sitemap files if I’m just doing the one?

Nope, that should work. Did you wait long enough for the sensor to actually transmit the next value?

Edit: ‘Nope’ as in you shouldn’t have to change items or sitemap if the only thing you changed was a “-1” added into the transformationPattern.

I have waited for a couple of updates, 1 minute apart, but no change, still get “-”.

I thought maybe I had a typo so I cut and pasted your example for the first channel and waited for an update but it’s still the same.

Hi are you using Tasmota firmware?

Not sure if this helps

SetOption64 Switch between - or _ as sensor name separator
0 = sensor name index separator is - (hyphen) (default)
1 = sensor name index separator is _ (underscore)
Affects DS18X20, DHT, BMP and SHT3X sensor names in tele messages

Also look at this

I’m a beginner in all this ,this may help

Ah, very interesting. So the transformationPattern chokes on a hyphen?