MQTT-in node - autodetect string, buffer and JSON object

I am changing a lot of stuff in my flows and had to distinguish strings from objects coming from mqtt-in.

The "auto-detect (string or buffer)" does not capture JSON.

Looking in the code, the else formats it as a string. Could this be changed to trying to parse it as json first (try/catch) catch would parse it as string:

  if (isUtf8(payload)) { 
                    try { payload = JSON.parse(payload); }
                    catch { payload = payload.toString(); }
                }

source location

Just changed it in my running instance, works quite nicely. Although it would be nice to remove the option from the pulldown menu, it would have a lot of implications for existing flows. But the auto-detect/parse of/to json is quite helpful.

I am sure I am not capturing all the exceptions, but I want to eliminate the manual capturing/parsing.

The reason it won't be done is exactly because "it would have a lot of implications for existing flows..." as in, it would break a lot of flows.

My test is not the test to use.

I am talking about the autodetect feature, it should be possible to test whether or not the data is a string, buffer or a json object and if it is the latter send it as a json object. It is a bit redundant to use:

mqtt-in -> json node
in auto-detect mode.

It is an improvement, sometimes you have to break stuff to move forward.

I don't fully understand what you mean here, I thought the data from mqtt could only be a buffer or a string. What do you mean by a json object. By definition JSON is a string.

You are correct with the terminology; I mean a string formatted as an javascript object.

string vs object. The string is not useful and could be converted automagically.

If the improvement breaks people then it's not much of an improvement. The option is there to output a parsed JSON object if you want.

1 Like

The option is there to output a parsed JSON object if you want.

That is correct, except that I have to use multiple mqtt nodes and filter/convert its output first.
I have reduced the number of mqtt nodes from 20+ to 1, I see it as an improvement.

Possibly a solution would be to add an option to the dropdown list "Autodetect JSON or string" (or something similar) which attempts to parse a string and returns the object if successful, otherwise returns the string as is.

Wow, I know that IoT is where digital meets physical but I didn't know that Node-RED had already progressed to the ability to break people!!

:skull_and_crossbones:

We certainly don't want that :wink:

I think this forum is littered with the remains of those irreparably damaged by node-red.

2 Likes

I'm sure you know IT folk who have rolled out a "simple update" on a Friday afternoon, and then had to spend the weekend undoing the mayhem.

2 Likes

Urm, no, never :cough: :blush:

3 Likes

I was gonna suggest exactly this but decided to wait it out ...

If I may jump in here I have a terribly naive question. I have a bunch of Sonoff switches running just fine, have their status on Dashboard, etc....

What I cannot figure out is how to parse data received from a Sonoff TH10 (tasmota) device to extract variable data (like temp and humidity). I have looked around and if I've seen it explained, I didn't know it. Here is an example response:

Beechnut/Sauna-TH10/stat/STATUS10 : msg : Object
object
topic: "Beechnut/Sauna-TH10/stat/STATUS10"
payload: "{"StatusSNS":{"Time":"2019-11-28T16:11:11","SI7021":{"Temperature":22.3,"Humidity":45.5},"TempUnit":"C"}}"
qos: 0
retain: false
_msgid: "57bab8b7.eff8a8"

I actually can get it to work (if the MQTT respons ehas the proper data) by using

    temperature = msg.payload.StatusSNS.SI7021.Temperature;
    humidity = msg.payload.StatusSNS.SI7021.Humidity;

however there are a bunch of MQTT messages that cause the error msg in debug of:

function : (error)
"TypeError: Cannot read property 'SI7021' of undefined"

that I can't seem to stop occurring. There has to be a better method, I just don't know what it is.
st

This is not the right topic, open a new one with your question.