How to extract temperature DS18B20 from mqtt string thru sensor ID

One problem I’ve found is that my temperature display in ClassicUI and HABpanel are not in sync with the Tasmota reading on the TasmotaUI. There seems to be quite a delay before they update. At the moment my Tasmota is set to send a tele/nomemcu1/SENSOR message every minute.
What could be causing the delay?

By more than a minute? I think the Tasmota UI data is continuously updated, whilst of course anything using the tele MQTT topic will be behind. Usually the tele topic publishes every 5 minutes: did you change that?

I must have changed it at sometime, probably when I was chasing problems and didn’t want to wait too long between updates. I’ll increase the time once I solve this.
The Tasmota UI and the tele/nodemcu1/SENSOR message are in sync. It’s just the Openhab side that is slow.

If this is true (MQTT message published by Tasmota is in sync with the Tasmota UI) then the problem lies either with the MQTT broker, or with openHAB. I would start a new topic though, as this is derailing this thread.

Your events.log will document and timestamp changes.

Let’s start with -
The openHAB JSONPATH transformation service is not JSONPATH, and has differences e.g. in the way it handles arrays. The return is always just a plain single string, for example.
Online validators not entirely trustworthy here.

We can do that. @CrazyIvan359 was really close.
I like to play with trialling this stuff in a rule, transfer to binding config when satisfied.

// test data
val rawjson = '{"Time":"2020-10-10T20:33:27","DS18B20_1":{"Id":"3C01B6078704","Temperature":19.4},"DS18B20_2":{"Id":"3C01B607DE10","Temperature":19.9},"DS18B20_3":{"Id":"3C01B607FA45","Temperature":19.9},"TempUnit":"C"}'
// showing how to select list by key
var results = transform("JSONPATH", "$.DS18B20_2", rawjson)
logInfo("test", "list by key: " + results)
// showing how to select list element directly by key
results = transform("JSONPATH", "$.DS18B20_2.Temperature", rawjson)
logInfo("test", "direct by key: " + results)
// but we can't rely on that key here, we need to select list by content of Id sub-key
// note the escapes and the TWO dots to look in the nested lists
results =  transform("JSONPATH", "$..[?(@.Id==\"3C01B607DE10\")]", rawjson)
logInfo("test", "list by content: " + results)
// finally, showing how to select list element by content
results =  transform("JSONPATH", "$..[?(@.Id==\"3C01B607DE10\")].Temperature", rawjson)
logInfo("test", "list element: " + results)
2020-10-11 02:21:43.040 [INFO ] [.eclipse.smarthome.model.script.test] - list by key: {Id=3C01B607DE10, Temperature=19.9}
2020-10-11 02:21:43.042 [INFO ] [.eclipse.smarthome.model.script.test] - direct by key: 19.9
2020-10-11 02:21:43.054 [INFO ] [.eclipse.smarthome.model.script.test] - list by content: {Id=3C01B607DE10, Temperature=19.9}
2020-10-11 02:21:43.058 [INFO ] [.eclipse.smarthome.model.script.test] - list element: 19.9
4 Likes

@rossko57 that is excellent! I will be using this when I switch to Tasmota.

I will admit I was just doing quick and dirty tests earlier.

In TASMOTA you can change the way mqtt is presented you can do setoption4 1 to change them

Thanks @rossko57
This did the trick. My thing looks like this now and works great!

  Thing topic sonoff_TH_Thing "Toevoer vent." @ "Tuoro" {
    Channels:
      Type switch : ventilator   [ stateTopic="stat/ventilator/POWER",  commandTopic="cmnd/ventilator/POWER", on="ON", off="OFF" ]
      Type number : temperature1 [ stateTopic="tele/ventilator/SENSOR", transformationPattern="JSONPATH:$..[?(@.Id==\"3C01B6078704\")].Temperature" ]
      Type number : temperature2 [ stateTopic="tele/ventilator/SENSOR", transformationPattern="JSONPATH:$..[?(@.Id==\"3C01B607DE10\")].Temperature" ]
      Type number : temperature3 [ stateTopic="tele/ventilator/SENSOR", transformationPattern="JSONPATH:$..[?(@.Id==\"3C01B607FA45\")].Temperature" ]
    }

Many thanks, regards Willem

@denominator what does that result in? I think I can see what it will do, but the documentation is not clear. If it does what I think it does, this is the first thing I went looking for.

As per OP example this will change the topic to

stat/ventilator/DS18B20_1

You can turn it on and off to see what it dose setoption4 1 ON setoption4 0 OFF

But that wouldn’t help get the sensor by address, now we can’t even search a single message to match the address. That would give us 3 separate messages for each sensor with no way of matching the address.

No they will have to program the sensor from factory default to have unique addresses so tasmota doesn’t randomly select their name.

That sounds like you’d have to build a custom binary, not a problem for some, but for the less technical the JSONPATH approach is easier.

1 Like