Change msg to object

Hi there,

I’m new to node red and have probably an easy problem which I can't solve: I receive data via mqtt and need to write it to an ifluxdb2 database.
The mqtt data comes from two different sources.

The first flow (with debug 2) results in a data format that can be written to the influxdb2 database.
The lower flow results in a different data format that I can't write to the database.

Unfortunately, I can’t find a way to get the data from the lower flow (debug 3) to the structure that I get from the top flow.

Could you please help me to change the format of the output of the lower flow so that it matches the structure of the top flow?

Thanks a lot!

Hi and welcome to the forum.

You would use a join node to join message streams into a single message.
here is a example it use a simple Jsonata expression to get the mqtt final path element, and use that to create the object.
e.g

[{"id":"fcc4b529731fda3f","type":"inject","z":"d1395164b4eec73e","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"topic/stateOfChange","payload":"20","payloadType":"num","x":260,"y":7760,"wires":[["1eec6c61bbe8e391"]]},{"id":"1eec6c61bbe8e391","type":"change","z":"d1395164b4eec73e","name":"","rules":[{"t":"set","p":"topic","pt":"msg","to":"$split($$.topic, \"/\")[-1]","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":480,"y":7780,"wires":[["158b250490ab597b"]]},{"id":"0817d1086bad6b9b","type":"inject","z":"d1395164b4eec73e","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"topic/CurrentPower","payload":"20","payloadType":"num","x":260,"y":7820,"wires":[["1eec6c61bbe8e391"]]},{"id":"158b250490ab597b","type":"join","z":"d1395164b4eec73e","name":"","mode":"custom","build":"object","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","useparts":false,"accumulate":false,"timeout":"","count":"2","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":630,"y":7780,"wires":[["80c69075d2a5448b"]]},{"id":"80c69075d2a5448b","type":"debug","z":"d1395164b4eec73e","name":"debug 2575","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":770,"y":7780,"wires":[]}]

How to import/export a flow

Or if you want to turn each message from the mqtt node into and object with one property.

[{"id":"fcc4b529731fda3f","type":"inject","z":"d1395164b4eec73e","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"topic/stateOfChange","payload":"20","payloadType":"num","x":260,"y":7760,"wires":[["1eec6c61bbe8e391"]]},{"id":"1eec6c61bbe8e391","type":"change","z":"d1395164b4eec73e","name":"","rules":[{"t":"set","p":"topic","pt":"msg","to":"$split($$.topic, \"/\")[-1]","tot":"jsonata"},{"t":"move","p":"payload","pt":"msg","to":"payload[msg.topic]","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":480,"y":7780,"wires":[["80c69075d2a5448b"]]},{"id":"0817d1086bad6b9b","type":"inject","z":"d1395164b4eec73e","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"topic/CurrentPower","payload":"20","payloadType":"num","x":260,"y":7820,"wires":[["1eec6c61bbe8e391"]]},{"id":"80c69075d2a5448b","type":"debug","z":"d1395164b4eec73e","name":"debug 2575","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":770,"y":7780,"wires":[]}]

Hi E1cid,

thank you very mutch. That was what I've been looking for. I prevouly tried the join node but without the change node.

I'm happy that it works now, but i have no clue why. Can you explain what the following means?

image

Thanks again!

It is a JSONata expression. It splits msg.topic at the /and the selects the end element. -1 counting from the end , or 0 would count from the beginning.
String functions · JSONata
Simple Queries · JSONata