Extract temperature from an MQTT string

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?

Sorry not tried that

But found it on here,does it work?

I think it may be as simple to circumvent as
transformationPattern="JSONPATH:$.\"DS18B20-1\".Temperature"
i.e. quote the string containing the hyphen for JSONPATH, but escape the quotes to hide them from parser.

Dumb question I know but how do I format text in a post to appear as your things, items and sitemaps do here?

Not a dumb question, because it’s not obvious unfortunately!

This post has all the details, but the big issue is that the code fence icons don’t actually show up. The buttons are still there - you can hover over them and a tooltip will appear.

In short, type three backticks, then enter your code on a newline, then end with three more backticks on a new line.

Thanks to helpful people on this forum I now have my multiple parallel DS18B20s working.
As a relative new comer I know how helpful it is to view some working code so I thought I would post my working code for anyone searching and coming across this thread.
I’ve changed the JSON transformation from that above by adding the id of each sensor rather than the DS18B20_n because I had a problem when fitting by pcb into it’s case. One sensor stopped working due to a broken wire but it was hard to determine which one because if 1 or 2 of 3 fail the other sensors are renumbered but the id is unique to each device.

thing file

Thing mqtt:topic:nodemcu1 "nodemcu1" @ "myHome/Water_Heater" {
		Channels:
			Type number : temperature "Temperature" [
				stateTopic="tele/nodemcu1/SENSOR",
				transformationPattern="JSONPATH:$.3C01A8160EFB.Temperature"
				]
				Type number : temperature2 "Temperature2" [
				stateTopic="tele/nodemcu1/SENSOR",
				transformationPattern="JSONPATH:$.3C01A8162221.Temperature"
				]
				Type number : temperature3 "Temperature3" [
				stateTopic="tele/nodemcu1/SENSOR",
				transformationPattern="JSONPATH:$.51D7FD1D64FF.Temperature"
				]
	}

item file

Number nodemcu1Temperature "nodemcu1 Temperature" { channel="mqtt:topic:nodemcu1:temperature" }
Number nodemcu1Temperature2 "nodemcu1 Temperature2" { channel="mqtt:topic:nodemcu1:temperature2" }
Number nodemcu1Temperature3 "nodemcu1 Temperature3" { channel="mqtt:topic:nodemcu1:temperature3" }

sitemap file

Frame label="Water Heater" { 
             Text       item=nodemcu1Temperature3 label="Top Temp [%.1f °C]" icon="temperature"
             Text       item=nodemcu1Temperature label="Centre Temp [%.1f °C]" icon="temperature"
             Text       item=nodemcu1Temperature2 label="Bottom Temp [%.1f °C]" icon="temperature"
                        }

This is my ClassicUI.

2 Likes

I’m curious on that change, did the received JSON string change from what you have posted above?

See post 11. Now that I know how to do it I’ve edited it to format the included code better.
The JSON string was taken before I found out about the hyphen problem but it doesn’t matter when using the id.

Hi
Going to be trying with two DS18B20 soon on a D1mini so will be trying your method ,thanks for posting the solution on here.

I am running D1 mini’s and would like to add a temperature sensor. Then send the data to OH3 via MQTT. I have a PIR attached to the D1 which I pull the input up however we are only dealing with a boolean value.

I am interested how you solved the problem, personally I would define an item for it and then push a value into mosquito and see if it is read correctly in OH3. Maybe that is a good project for to do this afternoon.

My test thing/item worked as I will only be sending one value however I can see I have a lot to learn from this post.