How to extract temperature DS18B20 from mqtt string thru sensor ID

I have a tasmotized Sonoff with three DS18B20 sensors connected to it. This Sonoff is used to control a fan that is part of an integent ventilation system.
I am able to extract and display the individual sensor data based on the sensor names i.e. DS18B20_1, DS18B20_2 and DS18B20_3.
I would rather extract the values based on the sensor ID’s to be sure of the position of the individual sensors.
My mqtt thing is:

 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:$.DS18B20_1.Temperature" ]
      Type number : temperature2 [ stateTopic="tele/ventilator/SENSOR", transformationPattern="JSONPATH:$.DS18B20_2.Temperature" ]
      Type number : temperature3 [ stateTopic="tele/ventilator/SENSOR", transformationPattern="JSONPATH:$.DS18B20_3.Temperature" ]
       }

My items are:

Number Zldr_temp         "Temperatuur Zolder [%.1f °C]"     <temperature>  (Zldr) {channel="mqtt:topic:MqttBrokerTuoro:sonoff_TH_Thing:temperature1"}
Number InblaasTemp       "Temperatuur Inblaas [%.1f °C]"    <temperature>  (Zldr) {channel="mqtt:topic:MqttBrokerTuoro:sonoff_TH_Thing:temperature2"}
Number AanzuigTemp       "Temperatuur Buiten [%.1f °C]"     <temperature>  (Zldr) {channel="mqtt:topic:MqttBrokerTuoro:sonoff_TH_Thing:temperature3"}

I am new to openHAB and this is my first ever use of the json tranformation path. I’ve searched the internet for an answer but could not find one, at least not one that I could understand.

With kind regards
Wil

Try looking at this ,it might help

If that doesn’t help we will need the JSON data that gets published at tele/ventilator/SENSOR to help you further

Hi I’ve read that post over and over and tried the solution given there but that does not work for me.
I tried it again but same thing no updated values when I change my thing to :

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/ventilatoter/SENSOR", transformationPattern="JSONPATH:$.3C01B6078704.Temperature" ]
      Type number : temperature2 [ stateTopic="tele/ventilator/SENSOR", transformationPattern="JSONPATH:$.3C01B607DE10.Temperature" ]
      Type number : temperature3 [ stateTopic="tele/ventilator/SENSOR", transformationPattern="JSONPATH:$.3C01B607FA45.Temperature" ]
    }

my json string is:
{“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”}

Kind regards and thanks for the help

You are mixing up ID and payload.
E.g. 3C01B607DE10 is a payload.
I’m afraid, you have to add REGEX-Transformation to filter by payload.
Joerg

This works for me;
transformationPattern=“JSONPATH:$.DS18B20_2.Temperature”

Hi Lester,
That works also for me but when sensor one fails then after a reboot sensor two becomes sensor one, sensor three becomes sensor 2 etc.
Regards Wil

You can do some minor scripting in JSONPATH like this:

$.[?(@.Id=="3C01B6078704")].Temperature

It will find all objects in the root object where Id matches 3C01B6078704. I haven’t tested this in OH.

Hi Joerg,
Never heard of REGEX-Transformation, shall first do some reading on that subject.
How would this look like in an thing?
Regards Will

Hi Michael,
Just tried this in OH but it did not work.
This was what I made of it:

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/ventilatoter/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" ]
    }

Regards Wil

Try:

transformationPattern="JSONPATH:$.[?(@.Id==\"3C01B6078704\")].Temperature"

You need to escape the "

I’m interested to see if this works too!

1 Like

I just did some searching and came up with the same result. I’m also interested because I am thinking of switching back to Tasmota from Espurna.

My two cents on that one: I would be afraid of the Sonoff to fail instead of your concerns.

If you really want to go for such a case, do something like this:
Create an ID channel for each sensor and link them with an ID item, the item name should be consistent whith the corresponding linked temperature item ( like temperature1 and ID1).
Create secondary (proxy) items for the temperatures which are not linked to the temperature channel ( temperature_proxy1).
Create a rule that triggers on the change of any linked temperature item. In the rule check wether the ID item name corresponding to the triggering item name does have the correct ID. In this case update the corresponding proxy item with the temperature.
Use only the proxy items to display and persist the temperatures.

Hi,[quote=“hafniumzinc, post:11, topic:106274, full:true”]
Try:

transformationPattern="JSONPATH:$.[?(@.Id==\"3C01B6078704\")].Temperature"

You need to escape the "

I’m interested to see if this works too!
[/quote]

Regret to say this did not work,

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/ventilatoter/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" ]
    }

Regards Wil

What does “did not work” mean? What result are you getting? Are you getting an error? Anything from the logs?

No error, in the things file but also no data to my sitemap items are not updated.
have not checked the log files yet.

I suspect this may be an issue with the JSONPATH transformation, it should work.

MQTT binding reports:

Incoming payload 'NULL' not supported by type 'NumberValue'

But I can confirm the configuration saved correctly via the REST api:

"transformationPattern": "JSONPATH:$.[?(@.Id==\"3C01B6078704\")].Temperature"

And I have successfully tested the expression at www.jsonpath.com with and without the escapes.

However, this appears to work via rules?

Log file reports:

2020-10-10 22:14:02.240 [WARN ] [ab.binding.mqtt.generic.ChannelState] - Incoming payload 'NULL' not supported by type 'NumberValue'

I got it to work with a JS transform. The only catch is you’ll need a file for every sensor, unless there is a way to pass an additional argument to the JS transform. Messier than the nice JSONPATH one liner, but cleaner than using a rule I think.

On the thing:

transformationPattern="JS:3C01B6078704.js"

File transform/3C01B6078704.js

(function(message) {
    var address = "3C01B6078704"
    var data = JSON.parse(message)
    for (var key in data) {
        if (data[key].hasOwnProperty("Id")) {
            if (data[key].Id == address) {
                return data[key].Temperature;
            }
        }
    }
    return null;
})(input)
1 Like

That works so I’ve changed mine too. As Willem said, using DS18B20_x is a problem if one fails or drops out. The number changes so this is a much better solution.
Thanks again for your assistance.