Create a single Json object with MQTT response

I have a python code which send 2 values to the same channel.
"sensor/X" and "sensor/Y", and I would like to merge them into a single Json object
{X : value of sensor/X, Y : value of sensor/Y}.

I have done this node :

[{"id":"6497f22a.847fd4","type":"tab","label":"Flow 3","disabled":false,"info":""},{"id":"7843571.5b8c028","type":"debug","z":"6497f22a.847fd4","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":650,"y":600,"wires":[]},{"id":"400510c2.75256","type":"json","z":"6497f22a.847fd4","name":"","property":"payload","action":"","pretty":false,"x":410,"y":600,"wires":[["7843571.5b8c028"]]},{"id":"3af33205.2fc486","type":"inject","z":"6497f22a.847fd4","name":"","topic":"","payload":"{\"topic\":\"tele/Mysonoff_TH10/SENSOR\",\"payload\":\"{\\\"Time\\\":\\\"2018-12-21T16:51:09\\\",\\\"AM2301\\\":{\\\"Temperature\\\":69.4,\\\"Humidity\\\":33.9},\\\"TempUnit\\\":\\\"F\\\"}\",\"qos\":0,\"retain\":false,\"_topic\":\"tele/Mysonoff_TH10/SENSOR\",\"_msgid\":\"dae7f7bc.36e5f8\"}","payloadType":"json","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":880,"y":1360,"wires":[[]]},{"id":"fd6553d8.f5fda","type":"mqtt in","z":"6497f22a.847fd4","name":"","topic":"sensor/#","qos":"2","datatype":"auto","broker":"2ad9c0c8.618a48","x":230,"y":600,"wires":[["400510c2.75256"]]},{"id":"2ad9c0c8.618a48","type":"mqtt-broker","z":"","name":"","broker":"localhost","port":"1883","clientid":"","usetls":false,"compatmode":true,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","closeTopic":"","closeQos":"0","closePayload":"","willTopic":"","willQos":"0","willPayload":""}]

but it does not work.. I am probably sure that I have to write a function in javacript to do it in order to specify how the json will be written but I am kind of lost at this point..

Thank you

It would be helpful to actually see the data you have so far.

However, assuming that both are a single number on their own topic, you will need to decide how you will deal with them coming at different times.

An MQTT in node set to sensor/# will receive both but as separate msg's. So you will need a change or function node to temporarily store each value and then to merge the two in-memory values to a single JSON.

Assuming that they get updated pretty much at the same time, a short delay after the merge should let you send out a single msg with both of the correct values.

A better approach would be to amend your Python code to send out a single topic containing both sensor values as stringified JSON. Then Node-RED could take in a single value and you could use the json node to convert back to a JavaScript object.

Your Python code could continue to send out the individual values but also send out the combined value, this is not an uncommon requirement. If you wanted to, you could send the combined values out to the topic sensor and keep the individual values on sensor/X, etc. Then in Node-RED an MQTT node with sensor as the topic would read the combined value and sensor/# would get the individual values.

1 Like

Thank you very much! It helps me!