Publish more datas with mqtt

hi everybody, i would like you to ask something. I'm doing a project in which i receive some datas from a meteo page and publish only temperature and humidity on a broker. I have already used an inject (to ask the datas every hour), the http request for the meteo datas, the json fuction and the fuction for changing the temperature from kelvin to celsius. My problem is about publishing temperature and humidity on thingspeak (i have the structure of the topic for publishing datas on the two fields that i have) because for the moment i have used two different mqtt out, but in this way every hour the programm publish just or the temperature or the humidity and not both. I hope i have been clear to explain my problem, because is the first time i use node red and none teach me anything. Thank you very much

Hi and welcome.

...you can use the mqtt-out node in a generic way without a static topic in its configuration setting. The msg.topic will be used as the mqtt-topic to publish the msg.payload to....so set the msg.topic object accordingly. This is also stated in the help / documentation for the node.

but in this way on the broker it will be published the entire msg.payload, isn't it? Because my problem is also that i have to publish only a part of the payload and not the entire one, for two times (for the temperature and for the humidity)

...then you need to split it into separate messages.
Can you export the flow and attach it here, as well as the output of a debug n8de, showing the structure of your current payload? Then it is easier to help...

Remove unwanted values, then split the incoming object, then create a topic from the object property name.
e.g.

[{"id":"1667747f.c84c3c","type":"inject","z":"bf9e1e33.030598","name":"incoming","props":[{"p":"payload.temperature","v":"20","vt":"num"},{"p":"payload.humidity","v":"65","vt":"num"},{"p":"payload.other","v":"200","vt":"num"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":140,"y":40,"wires":[["50f18bec.aa1f24"]]},{"id":"50f18bec.aa1f24","type":"change","z":"bf9e1e33.030598","name":"remove unwanted values","rules":[{"t":"move","p":"payload","pt":"msg","to":"hold","tot":"msg"},{"t":"set","p":"payload.temperature","pt":"msg","to":"hold.temperature","tot":"msg"},{"t":"set","p":"payload.humidity","pt":"msg","to":"hold.humidity","tot":"msg"},{"t":"delete","p":"hold","pt":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":360,"y":40,"wires":[["8c743b73.5c9398"]]},{"id":"8c743b73.5c9398","type":"split","z":"bf9e1e33.030598","name":"","splt":"\\n","spltType":"str","arraySplt":1,"arraySpltType":"len","stream":false,"addname":"key","x":350,"y":80,"wires":[["e22b911a.c105b"]]},{"id":"e22b911a.c105b","type":"change","z":"bf9e1e33.030598","name":"","rules":[{"t":"set","p":"topic","pt":"msg","to":"\"mqtt/topic/\" & $$.key","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":530,"y":80,"wires":[["7058f197.2bcf","fca8474f.dbeba"]]},{"id":"fca8474f.dbeba","type":"mqtt out","z":"bf9e1e33.030598","name":"","topic":"","qos":"0","retain":"","respTopic":"","contentType":"","userProps":"","correl":"","expiry":"","broker":"d675b749.04b9c8","x":710,"y":80,"wires":[]},{"id":"7058f197.2bcf","type":"debug","z":"bf9e1e33.030598","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":430,"y":180,"wires":[]},{"id":"d675b749.04b9c8","type":"mqtt-broker","name":"Localhost","broker":"localhost","port":"1883","clientid":"","usetls":false,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","closeTopic":"","closeQos":"0","closePayload":"","willTopic":"","willQos":"0","willPayload":""}]

yes sure, that's the structure @hominidae



thanks @E1cid, the only problem is that i understand your solution and the idea but i'm not sure i know how to do this on node red :sweat_smile:
sorry but i've never done something with it and nobody explained me but i have to use it for a university project

I showed you how to do it in the flow.json example i posted.
copy json, press ctrl i in editor and paste the json then press import.

...what I meant, was a proper export, not screenshots.
Also you could show the output of a debug node after the json node. We need to see the message content.

What yoy can try anyway is to add a split node after the json node, then see what/ how the original msg gets split into several new ones. Add a debug node and observe the content of the messages

thank you very much, I resolved some problems with your suggestions. Now the only thing I'm not able to do is publishing the temperature and the umidity on the broker, because i have two different topics to use (because this datas has to been published in different tables and so they have diefferent field to write in their topic) but i have also to use only one mqtt out. That's my project, sorry but before i didn?t understand about doing the export @hominidae @E1cid
[
{
"id": "84b6531c.4e773",
"type": "tab",
"label": "Flow 3",
"disabled": false,
"info": ""
},
{
"id": "caa05324.53041",
"type": "inject",
"z": "84b6531c.4e773",
"name": "interrogazione ogni ora al meteo",
"props": [
{
"p": "payload"
},
{
"p": "topic",
"vt": "str"
}
],
"repeat": "3600",
"crontab": "",
"once": false,
"onceDelay": 0.1,
"topic": "",
"payload": "",
"payloadType": "date",
"x": 180,
"y": 80,
"wires": [
[
"eb5e3cf0.ef39"
]
]
},
{
"id": "eb5e3cf0.ef39",
"type": "http request",
"z": "84b6531c.4e773",
"name": "",
"method": "GET",
"ret": "txt",
"paytoqs": "ignore",
"url": "http://api.openweathermap.org/data/2.5/weather?q=Rogeno,it&APPID=24e95b46b8c23e25c547af81612717ac",
"tls": "",
"persist": false,
"proxy": "",
"authType": "",
"x": 470,
"y": 80,
"wires": [
[
"9c9c128.84842f"
]
]
},
{
"id": "9c9c128.84842f",
"type": "json",
"z": "84b6531c.4e773",
"name": "",
"property": "payload",
"action": "",
"pretty": false,
"x": 690,
"y": 80,
"wires": [
[
"27f2ebc0.270b54"
]
]
},
{
"id": "28b51f0b.331d1",
"type": "change",
"z": "84b6531c.4e773",
"name": "remove unwanted values",
"rules": [
{
"t": "move",
"p": "payload",
"pt": "msg",
"to": "hold",
"tot": "msg"
},
{
"t": "set",
"p": "payload.main.temp",
"pt": "msg",
"to": "hold.main.temp",
"tot": "msg"
},
{
"t": "set",
"p": "payload.main.humidity",
"pt": "msg",
"to": "hold.main.humidity",
"tot": "msg"
},
{
"t": "delete",
"p": "hold",
"pt": "msg"
}
],
"action": "",
"property": "",
"from": "",
"to": "",
"reg": false,
"x": 1090,
"y": 80,
"wires": [
[
"2cf91488.ad665c"
]
]
},
{
"id": "2cf91488.ad665c",
"type": "split",
"z": "84b6531c.4e773",
"name": "",
"splt": "\n",
"spltType": "str",
"arraySplt": 1,
"arraySpltType": "len",
"stream": false,
"addname": "key",
"x": 1010,
"y": 200,
"wires": [
[
"bdc6102f.b3daa"
]
]
},
{
"id": "af499681.b86b98",
"type": "change",
"z": "84b6531c.4e773",
"name": "",
"rules": [
{
"t": "set",
"p": "topic.temp",
"pt": "msg",
"to": "channels/1703388/publish/fields/field1/AJ091ZVF36D2DYGE",
"tot": "jsonata"
},
{
"t": "set",
"p": "topic.humidity",
"pt": "msg",
"to": "channels/1703388/publish/fields/field2/AJ091ZVF36D2DYGE",
"tot": "jsonata"
}
],
"action": "",
"property": "",
"from": "",
"to": "",
"reg": false,
"x": 1260,
"y": 240,
"wires": [

]
},
{
"id": "bdc6102f.b3daa",
"type": "debug",
"z": "84b6531c.4e773",
"name": "",
"active": true,
"tosidebar": true,
"console": false,
"tostatus": false,
"complete": "topic",
"targetType": "msg",
"statusVal": "",
"statusType": "auto",
"x": 1500,
"y": 100,
"wires":
},
{
"id": "27f2ebc0.270b54",
"type": "function",
"z": "84b6531c.4e773",
"name": "",
"func": "msg.payload.main.temp = Math.round((msg.payload.main.temp-273.15)*100)/100;\n\nreturn msg;",
"outputs": 1,
"noerr": 0,
"initialize": "",
"finalize": "",
"x": 850,
"y": 100,
"wires": [
[
"28b51f0b.331d1"
]
]
}
]

I corrected your changes and added the correct topics in a lookup object. I also removed the json node as there is a setting to output an object in the http request node.

[{"id":"caa05324.53041","type":"inject","z":"84b6531c.4e773","name":"interrogazione ogni ora al meteo","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":420,"y":160,"wires":[["eb5e3cf0.ef39"]]},{"id":"eb5e3cf0.ef39","type":"http request","z":"84b6531c.4e773","name":"","method":"GET","ret":"obj","paytoqs":"ignore","url":"http://api.openweathermap.org/data/2.5/weather?q=Rogeno,it&APPID=24e95b46b8c23e25c547af81612717ac","tls":"","persist":false,"proxy":"","authType":"","x":470,"y":80,"wires":[["27f2ebc0.270b54"]]},{"id":"27f2ebc0.270b54","type":"function","z":"84b6531c.4e773","name":"","func":"msg.payload.main.temp = Math.round((msg.payload.main.temp-273.15)*100)/100;\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":670,"y":80,"wires":[["28b51f0b.331d1"]]},{"id":"28b51f0b.331d1","type":"change","z":"84b6531c.4e773","name":"remove unwanted values","rules":[{"t":"move","p":"payload","pt":"msg","to":"hold","tot":"msg"},{"t":"set","p":"payload.temp","pt":"msg","to":"hold.main.temp","tot":"msg"},{"t":"set","p":"payload.humidity","pt":"msg","to":"hold.main.humidity","tot":"msg"},{"t":"delete","p":"hold","pt":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":910,"y":80,"wires":[["2cf91488.ad665c"]]},{"id":"2cf91488.ad665c","type":"split","z":"84b6531c.4e773","name":"","splt":"","spltType":"str","arraySplt":1,"arraySpltType":"len","stream":false,"addname":"key","x":1010,"y":200,"wires":[["af499681.b86b98"]]},{"id":"af499681.b86b98","type":"change","z":"84b6531c.4e773","name":"","rules":[{"t":"set","p":"lookup","pt":"msg","to":"{\"temp\":\"channels/1703388/publish/fields/field1/AJ091ZVF36D2DYGE\",\"humidity\":\"channels/1703388/publish/fields/field2/AJ091ZVF36D2DYGE\"}","tot":"json"},{"t":"set","p":"topic","pt":"msg","to":"lookup[msg.key]","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":1210,"y":160,"wires":[["bdc6102f.b3daa"]]},{"id":"bdc6102f.b3daa","type":"debug","z":"84b6531c.4e773","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":1480,"y":100,"wires":[]}]

ps, to post json and code use
```
code
```
you can use the </> button in the post editor to get the backticks.
What you posted is corrupt, try importing it and see what happens.

thank you very much, you helped me a lot. I have just tried the flow with your corrections and in reality the only problem that i have again is that i don't know how to use the mqtt out to publish both the topics. I have tried putting in the topic space # or channels/1703388/publish/fields/#/AJ091ZVF36D2DYGE or channels/1703388/publish/fields/+/AJ091ZVF36D2DYGE but none of them is right

leave the topic blank in the mqtt out node, it will then use msg.topic.

okay, thanks. Probably in this moment the broker that I'm using doesn't work because also in that way It doesn't connecting and I have the same situation with another flow that yesterday worked. I will try tomorrow again, thank you very much (i hope i won't have to disturb you another time :slight_smile: )

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.