BME280 Output Object Splitting

I am setting up a BME280 to read pressure. It has three outputs in the Object as follows:

I used a split and a switch to pull out the pressure by setting the switch to output values greater than 800. That worked only because the pressure units are so small making the number range very large, but I wanted to learn how to split up a generic Object into separate messages. Even though my solution worked in this case, I feel my learning on splitting Objects was cut short.

Here is my flow:

I have read all of the forum topics related to splitting Objects into separate messages and tried many of the techniques. Because I am low on the learning curve, I was not successful.

Could someone help jump-start me on how to build a flow to separate the three numbers from the Object into separate messages? A sample flow would be very helpful.

Alan

Here is the above flow:

[{"id":"a5e1fda2.3642b","type":"inject","z":"ba9d77c1.7c6f8","name":"","topic":"","payload":"Trigger","payloadType":"str","repeat":"5","crontab":"","once":false,"onceDelay":"1","x":304,"y":956,"wires":[["6e44b71b.f404"]]},{"id":"6e44b71b.f404","type":"Bme280","z":"ba9d77c1.7c6f8","name":"","bus":"1","address":"0x77","topic":"bme280","extra":false,"x":397,"y":1099,"wires":[["bfa5f63e.ceaa78","cec83e90.654538"]]},{"id":"29d3c20b.ac19ce","type":"debug","z":"ba9d77c1.7c6f8","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":886,"y":999,"wires":},{"id":"bfa5f63e.ceaa78","type":"split","z":"ba9d77c1.7c6f8","name":"","splt":"\n","spltType":"str","arraySplt":1,"arraySpltType":"len","stream":false,"addname":"topic","x":625,"y":1169,"wires":[["504589a9.2abd68","29d3c20b.ac19ce"]]},{"id":"45ac8027.a4ed4","type":"debug","z":"ba9d77c1.7c6f8","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":1002,"y":1218,"wires":},{"id":"504589a9.2abd68","type":"switch","z":"ba9d77c1.7c6f8","name":"","property":"payload","propertyType":"msg","rules":[{"t":"gt","v":"800","vt":"num"}],"checkall":"true","repair":false,"outputs":1,"x":765,"y":1218,"wires":[["45ac8027.a4ed4"]]},{"id":"cec83e90.654538","type":"debug","z":"ba9d77c1.7c6f8","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":559,"y":964,"wires":}]

First please read https://discourse.nodered.org/t/how-to-share-code-or-flow-json/506

I think this is what you are trying todo

please copy and paste this when importing

[{"id":"a21e68f9.74436","type":"function","z":"33a0358a.93f17a","name":"Setup Object","func":"\nmsg.payload = \n{\"TempatureC\":\"32.54\",\"humidity\":\"51.8845672\",\"pressure_hPa\":\"987.982345\"};\nreturn msg;","outputs":1,"noerr":0,"x":350,"y":280,"wires":[["b7f08be0.380038"]]},{"id":"b7f08be0.380038","type":"debug","z":"33a0358a.93f17a","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":510,"y":280,"wires":[]},{"id":"f5c838c4.c526d","type":"inject","z":"33a0358a.93f17a","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":180,"y":280,"wires":[["a21e68f9.74436"]]},{"id":"eef947dd.b4d0d8","type":"comment","z":"33a0358a.93f17a","name":"understand the structure of an object","info":"","x":350,"y":240,"wires":[]},{"id":"bb570529.bd6e5","type":"function","z":"33a0358a.93f17a","name":"Setup Object","func":"\nmsg.payload = \n{\"TempatureC\":\"32.54\",\"humidity\":\"51.8845672\",\"pressure_hPa\":\"987.982345\"};\nreturn msg;","outputs":1,"noerr":0,"x":350,"y":360,"wires":[["c09f6353.b671","1528c89c.cb53df","938107a4.269e5"]]},{"id":"391d6ed.8b77012","type":"debug","z":"33a0358a.93f17a","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":950,"y":360,"wires":[]},{"id":"b0057f01.1a1a7","type":"inject","z":"33a0358a.93f17a","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":180,"y":360,"wires":[["bb570529.bd6e5"]]},{"id":"c09f6353.b671","type":"change","z":"33a0358a.93f17a","name":"msg.payload.TempatureC","rules":[{"t":"set","p":"payload","pt":"msg","to":"payload.TempatureC","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":590,"y":360,"wires":[["391d6ed.8b77012"]]},{"id":"1528c89c.cb53df","type":"change","z":"33a0358a.93f17a","name":"msg.payload.humidity","rules":[{"t":"set","p":"payload","pt":"msg","to":"payload.humidity","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":580,"y":400,"wires":[["823eddcb.73709"]]},{"id":"938107a4.269e5","type":"change","z":"33a0358a.93f17a","name":"msg.payload.pressure_hPa","rules":[{"t":"set","p":"payload","pt":"msg","to":"payload.pressure_hPa","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":600,"y":440,"wires":[["d916da65.57f17"]]},{"id":"823eddcb.73709","type":"debug","z":"33a0358a.93f17a","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":950,"y":400,"wires":[]},{"id":"d916da65.57f17","type":"debug","z":"33a0358a.93f17a","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":950,"y":440,"wires":[]},{"id":"e59e3172.896538","type":"comment","z":"33a0358a.93f17a","name":"splitting the opbject into individual msg.payloads","info":"","x":340,"y":320,"wires":[]},{"id":"b3d2d4b5.9c3bb8","type":"function","z":"33a0358a.93f17a","name":"Setup Object","func":"\nmsg.payload = \n{\"TempatureC\":\"32.54\",\"humidity\":\"51.8845672\",\"pressure_hPa\":\"987.982345\"};\nreturn msg;","outputs":1,"noerr":0,"x":350,"y":540,"wires":[["1500db1b.55ecbd","a4df9424.029b88","1e6b00ab.d79e37"]]},{"id":"991d46f5.93bd38","type":"debug","z":"33a0358a.93f17a","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":950,"y":540,"wires":[]},{"id":"395827d3.d28bc","type":"inject","z":"33a0358a.93f17a","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":180,"y":540,"wires":[["b3d2d4b5.9c3bb8"]]},{"id":"1500db1b.55ecbd","type":"change","z":"33a0358a.93f17a","name":"msg.topic and msg.payload.TempatureC","rules":[{"t":"set","p":"payload","pt":"msg","to":"payload.TempatureC","tot":"msg"},{"t":"set","p":"topic","pt":"msg","to":"TempatureC","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":640,"y":540,"wires":[["991d46f5.93bd38"]]},{"id":"a4df9424.029b88","type":"change","z":"33a0358a.93f17a","name":"msg.topic and msg.payload.humidity","rules":[{"t":"set","p":"payload","pt":"msg","to":"payload.humidity","tot":"msg"},{"t":"set","p":"topic","pt":"msg","to":"humidity","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":630,"y":580,"wires":[["5c635b5f.06a3bc"]]},{"id":"1e6b00ab.d79e37","type":"change","z":"33a0358a.93f17a","name":"msg.topic and msg.payload.pressure_hPa","rules":[{"t":"set","p":"payload","pt":"msg","to":"payload.pressure_hPa","tot":"msg"},{"t":"set","p":"topic","pt":"msg","to":"pressure_hPa","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":650,"y":620,"wires":[["e79301f2.6c1ab8"]]},{"id":"5c635b5f.06a3bc","type":"debug","z":"33a0358a.93f17a","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":950,"y":580,"wires":[]},{"id":"e79301f2.6c1ab8","type":"debug","z":"33a0358a.93f17a","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":950,"y":620,"wires":[]},{"id":"fa9cb913.c46528","type":"comment","z":"33a0358a.93f17a","name":"adding a topic to make it better","info":"","x":350,"y":500,"wires":[]},{"id":"fb6316bd.c63668","type":"function","z":"33a0358a.93f17a","name":"function","func":"//Get Values from payload object and put in to a var\nvar TempatureC = msg.payload.TempatureC;\nvar humidity = msg.payload.humidity;\nvar pressure_hPa = msg.payload.pressure_hPa;\n\n\n//send it\nvar msg1 = {payload:TempatureC};\nvar msg2 = {payload:humidity};\nvar msg3 = {payload:pressure_hPa};\nreturn [[msg1,msg2,msg3]];","outputs":1,"noerr":0,"x":560,"y":740,"wires":[["8c4ec24d.16862"]]},{"id":"8c4ec24d.16862","type":"debug","z":"33a0358a.93f17a","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":750,"y":740,"wires":[]},{"id":"28a03896.b722d","type":"inject","z":"33a0358a.93f17a","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":200,"y":740,"wires":[["bca4730a.c85128"]]},{"id":"bca4730a.c85128","type":"function","z":"33a0358a.93f17a","name":"Setup Object","func":"\nmsg.payload = \n{\"TempatureC\":\"32.54\",\"humidity\":\"51.8845672\",\"pressure_hPa\":\"987.982345\"};\nreturn msg;","outputs":1,"noerr":0,"x":390,"y":740,"wires":[["fb6316bd.c63668"]]},{"id":"f5b69ce7.227488","type":"comment","z":"33a0358a.93f17a","name":"using a function to do it","info":"","x":360,"y":680,"wires":[]},{"id":"125ac65b.d1b402","type":"function","z":"33a0358a.93f17a","name":"function","func":"//Get Values from payload object and put in to a var\nvar TempatureC = msg.payload.TempatureC;\nvar humidity = msg.payload.humidity;\nvar pressure_hPa = msg.payload.pressure_hPa;\n\n\n//send it\nvar msg1 = {payload:TempatureC,topic:\"TempatureC\"};\nvar msg2 = {payload:humidity,topic:\"humidity\"};\nvar msg3 = {payload:pressure_hPa,topic:\"pressure_hPa\"};\nreturn [[msg1,msg2,msg3]];","outputs":1,"noerr":0,"x":560,"y":880,"wires":[["30a02e15.1b4412"]]},{"id":"30a02e15.1b4412","type":"debug","z":"33a0358a.93f17a","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":750,"y":880,"wires":[]},{"id":"4ed5ec7e.3fe1f4","type":"inject","z":"33a0358a.93f17a","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":200,"y":880,"wires":[["3414ae6c.b74b2a"]]},{"id":"3414ae6c.b74b2a","type":"function","z":"33a0358a.93f17a","name":"Setup Object","func":"\nmsg.payload = \n{\"TempatureC\":\"32.54\",\"humidity\":\"51.8845672\",\"pressure_hPa\":\"987.982345\"};\nreturn msg;","outputs":1,"noerr":0,"x":390,"y":880,"wires":[["125ac65b.d1b402"]]},{"id":"2495aa21.e48cae","type":"comment","z":"33a0358a.93f17a","name":"Adding a topic and using a function to do it","info":"","x":420,"y":820,"wires":[]}]

your not trying to split ... your want to separate a object into individual msg

NOTE: the function called "Setup Object" is just a place holder for you node BME280. Its basically just giving the same format of output as the BME280 node.

1 Like

For more fun learning JS and writing your own functions.

At some point you may want to convert hPa hectopascal.

You learn more trying to convert another JS code dang close to what you want when you start out.

Here is a function node I put a bunch of comments in for you to use. It converts any temp in Celsius to Fahrenheit

ive set the msg payload to 100 C so it should output 212 F

[{"id":"4580b00c.eda65","type":"function","z":"33a0358a.93f17a","name":"function","func":"\n\n    //Take msg.payload of the temp celcus and save it to a var\n    var tempc = msg.payload;\n\n    //Convert temp celcus to Fahrenheit\n    var tempf = tempc * 9/5 + 32;\n\n    //round the number to get rid of all them decimals\n    tempf = Math.round(tempf * 10000) / 10000;\n\n    //set msg.payload to Fahrenheit\n    msg.payload = tempf;\n\n    //send it!!!!!!!\n    return msg;\n","outputs":1,"noerr":0,"x":480,"y":1040,"wires":[["aeb8e1da.52e69"]]},{"id":"ab7f1e57.42247","type":"change","z":"33a0358a.93f17a","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"100","tot":"num"}],"action":"","property":"","from":"","to":"","reg":false,"x":300,"y":1040,"wires":[["4580b00c.eda65"]]},{"id":"f18031a.a00ead","type":"inject","z":"33a0358a.93f17a","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":120,"y":1040,"wires":[["ab7f1e57.42247"]]},{"id":"aeb8e1da.52e69","type":"debug","z":"33a0358a.93f17a","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":670,"y":1040,"wires":[]}]

code inside function node

//Take msg.payload of the temp celcus and save it to a var
var tempc = msg.payload;

//Convert temp celcus to Fahrenheit
var tempf = tempc * 9/5 + 32;

//round the number to get rid of all them decimals
tempf = Math.round(tempf * 10000) / 10000;

//set msg.payload to Fahrenheit
msg.payload = tempf;

//send it!!!!!!!
return msg;

Note: I see I misspelled Celsius. Too late now no spell check in the function node :slight_smile:

1 Like

@n61ab I've been following you other post on the BME280 because i bought a BME680 a while back. never got around to connecting it up.

I remember when I first got my temp sensor connected up to the raspberry pi 2 years ago. I thought that was going to be the hardest part. NOPE learning how to use node-red and all the things I was going to have to do with all that data the sensor provided was the hard part.

I was (still am) a Java Script nublet and every one in the community here took me under their wing and was very helpful.

Good Luck. Look forward to seeing what you do with your project and sensor.

Be grateful there is a Forum now.

1 Like

The split node is not needed, and the bme can be connected to the switch node directly.
There is lots of helpful documentation.

@meeki007 for C to F conversion, JSONata is (somewhat) easier in a change node:

2 Likes

@bakman2

Oh I like that !!! been putting off learning more about JSON for over a year now. Ya just motivated me to dig into it. I use it now for basic stuff like tables and storing variables to file.

Thanks m8

It is absurdly powerful, but also requires a different mindset. There is a nice tester within node-red, but also on the jsonata site, can reduce a lot of work in certain situations.

1 Like

Thank you meeki007

Your sample flows were great! I learned a lot by going thru each example.

As it turned out, I was "close" to it on my own. I was reversing the to and from on the change rule and then gave up trying to use the change node.

Your example on adding a topic using a change rule will help me down the road when processing the individual messages. I had not thought of that.

I will make sure to use the proper formatting when uploading flow text.

Alan

2 Likes

Hi again meeki007 and others.

I have another "parse" challenge, at least for me. Your example flows for the bme280 were excellent and I learned a lot.

Now I have another string, which I get via a UDP broadcast. The developer is willing to change the format, if necessary.

I want to parse out the elements. I tried to use the same technique you provided, but Node-Red does not break out the individual paths, so I cannot copy and paste it from the de-bug window, for some reason.

I do see, unlike the BME-280 object structure, in this case, there is an additional set of brackets under "meters". I understand in JASON, this is hierarchical, but I expected Node-Red to breakout the individual paths.

Below is the object. I also included a screenshot of the de-bug window. Note that Node-Red does not break out the individual paths for each pair.

The data is coming from another program via the UDP broadcast node. Here is the UDP setup screenshot:

Could you help me to understand how to parse this object", and why Node-Red is not breaking out the individual paths?

Is the payload not in the correct format for Node-Red? I could ask the developer of the data source to change the format if that is the problem. If that is the case, what needs to change?

Alan

===============================

payload: "{"station":"MacPro","callsign":"WA9WUD","nickname":"","ipaddr":"192.168.50.80","meters":{"+13.8A":"13.6 V","PATEMPC":"28.8 C","FWDPWR":"0.0 W","REVPWR":"0.0 W","SWR":"--- ","PATEMPF":"83.9 F"}}"

Here is a screenshot from the de-bug window

That appears to be a JSON string. Feed it into a JSON node and you should get a javascript object out, and you should be able to refer to the fields as, for example, msg.payload.station

[Edit] I don't use that node but possibly there is an option in the Output field of the configuration to automatically parse the data as a JSON string, in which case it should give you the js object directly.

[More edit] I meant to feed it into a JSON node of course, not a debug node. I have corrected it above

1 Like

Thanks Colin!

First, I tried to just type in the path. No luck.

Next, I put a "json" node after the UDP broadcast output. That did the trick. I can now see and copy the paths I want to the "change" node for parsing.

Alan

I've been playing around with one of these devices, and I put the outputs into an "influxdb" database, and then use them to drive a Grafana dashboard. Probably light on coding, if that is what you are playing with here, but lots of fun! The device is great for something so tiny, eh? :slight_smile:

Yes, I did the same and love the influxDB and Grafana combination.

I have each in a Docker Container and so the install was very easy to get them working together.

There is a very good Tube video on how to do the setup. Easy to find with a search.

Alan

1 Like