Tasmota SHT30 data to generic temp plugin

I am trying to connect output from a Tasmota device to Nymea generic temperature sensor.

Nymeas MQTT client reciewes this Json

{“Time”:“2022-12-08T12:29:05”,“SHT3X”:{“Temperature”:22.5,“Humidity”:41.6,“DewPoint”:8.8},“TempUnit”:“C”}

I am following this tutorial
https://nymea.io/documentation/users/advancedusage/sensor-on-mqtt-server

trying to extract temperature from the object by adapting the last line in this script

ThingEvent {
thingId: “…” // Internal MQTT client
eventName: “triggered”
onTriggered: {
console.log(“MQTT event reveived:”, JSON.stringify(params));
temp1.value = (1.0 * params[“data”] + 20) / 70
}
}

No luck. I get a (nan) whatever I try using experience from Node-red

Any help is much apreciated

Hans

What does this line print?

console.log(“MQTT event reveived:”, JSON.stringify(params));

Via Magic script I se in log
image

Copied from log:
9: MQTT event reveived: {“27ec8baf-0c13-4d0a-aaee-313582592695”:“tele/tasmota_E0397B/SENSOR”,“8af98566-79d9-4e65-b1dc-9067e4f93af1”:“{"Time":"2022-12-08T13:42:05","SHT3X":{"Temperature":25.4,"Humidity":32.1,"DewPoint":7.5},"TempUnit":"C"}”,“data”:“{"Time":"2022-12-08T13:42:05","SHT3X":{"Temperature":25.4,"Humidity":32.1,"DewPoint":7.5},"TempUnit":"C"}”,“topic”:“tele/tasmota_E0397B/SENSOR”}

With

 temp1.value = (1.0 * params[“data”] + 20) / 70

you’re accessing the “data” in this payload, which is the entire

“{"Time":"2022-12-08T13:42:05","SHT3X":{"Temperature":25.4,"Humidity":32.1,"DewPoint":7.5},"TempUnit":"C"}

you can’t multiply this whole thing with 1.0 etc, you’d need to get the exact value you’re interested in, e.g. for temperature: params["data"]["SHT3X"]["Temperature"]

So your statement should be something like

 temp1.value = (1.0 * params[“data”]["SHT3X"]["Temperature"] + 20) / 70

I get a type error

9: MQTT event reveived: {“27ec8baf-0c13-4d0a-aaee-313582592695”:“tele/tasmota_E0397B/SENSOR”,“8af98566-79d9-4e65-b1dc-9067e4f93af1”:“{"Time":"2022-12-08T14:57:06","SHT3X":{"Temperature":28.4,"Humidity":29.3,"DewPoint":8.8},"TempUnit":"C"}”,“data”:“{"Time":"2022-12-08T14:57:06","SHT3X":{"Temperature":28.4,"Humidity":29.3,"DewPoint":8.8},"TempUnit":"C"}”,“topic”:“tele/tasmota_E0397B/SENSOR”}
11: TypeError: Cannot read property ‘Temperature’ of undefined’

well, try to print the steps indvidually

console.log(“MQTT event reveived:”, JSON.stringify(params));
console.log(“MQTT event reveived:”, JSON.stringify(params["data"]));
console.log(“MQTT event reveived:”, JSON.stringify(params["data"]["SHTX"]));

Eventually you’ll find the mistake

  11  console.log("MQTT event reveived:", JSON.stringify(params[`data`]));       
   12 console.log("MQTT event reveived:", JSON.stringify(params[`data`][`SHT3X`]));       

Console log gives
11: MQTT event reveived: “{"Time":"2022-12-08T17:01:12","SHT3X":{"Temperature":28.6,"Humidity":29.0,"DewPoint":8.8},"TempUnit":"C"}”

12: MQTT event reveived: undefined

hmm… is that a string? Do you perhaps need to convert it to a json object beforehand?

JSON.parse(params["data"])["SHT3X"]

That was the problem! Now it works…

temp1.value =(1.0*(“MQTT event revelded:”, JSON.parse(params[“data”])[“SHT3X”][“Temperature”]+20)/70);

Thank you very much for your time!

If I had just red the last lines of the tutorial…
" You can adjust the formula above according to your liking. For instance if your MQTT client can only send JSON, you will get the entire JSON message in the data parameter and can unpack and extract relevant information using JSON.parse().=