Also, it's not common to save everything in one table or to store aggregated/min/max values (usually done with queries).
@spachal: Nice sample for beginners.
Consider that, if you have more sensors, it will soon get confusing and hard to maintain (eg: if the ip address changes, you have to edit all request nodes).
A more efficient way would be, to derive the required data from the mqtt topic. So you would be able to store an unlimited number of bme sensors with only 3 nodes.
Here is an example - looks a little weird, cause i extracted it from a bigger flow (you need to have the node "node-red-contrib-influxdb" installed):
Code: Select all
[{"id":"101caa2c.161c66","type":"mqtt in","z":"c421dfeb.3e752","name":"","topic":"red/event/#","qos":"0","broker":"e36a332b.c61f3","x":243,"y":2214,"wires":[["b3315bcf.c85c88"]]},{"id":"b3315bcf.c85c88","type":"json","z":"c421dfeb.3e752","name":"","x":376,"y":2223,"wires":[["fbd0ec8.0bb921"]]},{"id":"fbd0ec8.0bb921","type":"change","z":"c421dfeb.3e752","name":"prep","rules":[{"t":"set","p":"payload.id","pt":"msg","to":"undefined","tot":"jsonata"},{"t":"set","p":"payload.name","pt":"msg","to":"undefined","tot":"jsonata"},{"t":"set","p":"device","pt":"msg","to":"$split(msg.topic, \"/\")[4]","tot":"jsonata"},{"t":"set","p":"measurement","pt":"msg","to":"event","tot":"str"},{"t":"set","p":"system","pt":"msg","to":"$split(msg.topic, \"/\")[3]","tot":"jsonata"},{"t":"set","p":"payload.system","pt":"msg","to":"undefined","tot":"jsonata"},{"t":"set","p":"type","pt":"msg","to":"$split(msg.topic, \"/\")[2]","tot":"jsonata"},{"t":"set","p":"payload.label","pt":"msg","to":"undefined","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":517,"y":2247,"wires":[["a9693cdf.3db24"]]},{"id":"a9693cdf.3db24","type":"function","z":"c421dfeb.3e752","name":"prepare data","func":"newmsg = {}\nnewmsg.payload = {}\nnewmsg.measurement = msg.measurement;\n\nif (typeof msg.payload === \"object\") {\n for (var key in msg.payload) {\n if (isNumeric(msg.payload[key]) && msg.measurement != \"event\") {\n newmsg.payload[key] = parseFloat(msg.payload[key]);\n } else {\n newmsg.payload[key] = msg.payload[key];\n }\n }\n} else {\n if (isNumeric(msg.payload[key]) && msg.measurement != \"event\") {\n newmsg.payload.value = parseFloat(msg.payload);\n } else {\n newmsg.payload.value = msg.payload;\n }\n}\n\n//add tag \"Device\"\nnewmsg.payload = [ newmsg.payload, { \"Device\": msg.device} ];\n//add tag \"Beacon\"\nif (msg.beacon !== undefined)\n newmsg.payload[1].Beacon = msg.beacon;\n//add tag \"Location\" \nif (msg.location !== undefined)\n newmsg.payload[1].Location = msg.location;\n//add tag \"function\" \nif (msg.function !== undefined)\n newmsg.payload[1].Function = msg.function;\n//add tag \"system\" \nif (msg.system !== undefined)\n newmsg.payload[1].system = msg.system;\n//add tag \"type\" \nif (msg.type !== undefined)\n newmsg.payload[1].type = msg.type; \n \nreturn newmsg;\n\nfunction isNumeric(n) {\n return !isNaN(parseFloat(n)) && isFinite(n);\n}\n","outputs":"1","noerr":0,"x":694,"y":2275,"wires":[["4e0f7466.7ef02c"]]},{"id":"4e0f7466.7ef02c","type":"influxdb out","z":"c421dfeb.3e752","influxdb":"970191b.79d187","name":"","measurement":"","precision":"","retentionPolicy":"","x":872,"y":2300,"wires":[]},{"id":"e36a332b.c61f3","type":"mqtt-broker","z":"","broker":"localhost","port":"1883","clientid":"","usetls":false,"compatmode":false,"keepalive":"15","cleansession":true,"willTopic":"","willQos":"0","willPayload":"","birthTopic":"","birthQos":"0","birthPayload":""},{"id":"970191b.79d187","type":"influxdb","z":"","hostname":"192.168.4.240","port":"8086","protocol":"http","database":"infra","name":"opi_infra","usetls":false,"tls":""}]