But this is what both @knolleary
and me just showed you how to do:
which does the same thing.
Just use either in a function after the mqtt node and you will have a msg object where any msg.payload.xxx
is now msg.xxx
.
But this is what both @knolleary
and me just showed you how to do:
which does the same thing.
Just use either in a function after the mqtt node and you will have a msg object where any msg.payload.xxx
is now msg.xxx
.
(Some one shoot me!)
I posted a flow of what to NOT do in post 12.
So then I added a function
node to give me this:
[{"id":"27f0730a.da3ecc","type":"inject","z":"d103f472.19f3c8","name":"","topic":"","payload":"message payload","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":150,"y":3270,"wires":[["13c865eb.e3f5ba"]]},{"id":"e8c8383b.82e6f","type":"debug","z":"d103f472.19f3c8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":560,"y":3310,"wires":[]},{"id":"13c865eb.e3f5ba","type":"change","z":"d103f472.19f3c8","name":"","rules":[{"t":"set","p":"part1","pt":"msg","to":"part 1","tot":"str"},{"t":"set","p":"part2","pt":"msg","to":"this is part 2","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":150,"y":3320,"wires":[["db3b98d3.d2c5a8"]]},{"id":"db3b98d3.d2c5a8","type":"mqtt out","z":"d103f472.19f3c8","name":"","topic":"temp","qos":"","retain":"","broker":"378c0403.8cda04","x":320,"y":3320,"wires":[]},{"id":"3877c78e.e86728","type":"mqtt in","z":"d103f472.19f3c8","name":"","topic":"temp","qos":"2","datatype":"utf8","broker":"378c0403.8cda04","x":430,"y":3270,"wires":[["4360889e.602bf"]]},{"id":"4360889e.602bf","type":"function","z":"d103f472.19f3c8","name":"","func":"var newMsg = msg.payload; \nreturn newMsg;","outputs":1,"noerr":0,"x":560,"y":3270,"wires":[["e8c8383b.82e6f"]]},{"id":"378c0403.8cda04","type":"mqtt-broker","z":"","name":"MQTT HOST","broker":"192.168.0.99","port":"1883","clientid":"","usetls":false,"compatmode":false,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthRetain":"true","birthPayload":"","closeTopic":"","closeQos":"0","closePayload":"","willTopic":"","willQos":"0","willPayload":""}]
Now, it doesn't work at all.
I've tried a couple of settings in the MQTT IN
node but I get Function tried to send a message of type string
in the function
node.
So, sorry, just to establish a working version.....
Could you check what I have done wrong?
I can't look at your flow as I'm away from my computer this week.
But my guess is you have not configured the MQTT In node to parse the message as JSON.
Thanks Nick.
Yes, it is set to parse JSON
I did stuff up what I posted as the code though.
Small-ish detail. Now corrected, but still not working.
This is the best code to now.
[{"id":"27f0730a.da3ecc","type":"inject","z":"d103f472.19f3c8","name":"","topic":"","payload":"this is the message payload","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":120,"y":3270,"wires":[["13c865eb.e3f5ba"]]},{"id":"e8c8383b.82e6f","type":"debug","z":"d103f472.19f3c8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":560,"y":3310,"wires":[]},{"id":"13c865eb.e3f5ba","type":"change","z":"d103f472.19f3c8","name":"","rules":[{"t":"set","p":"payload.payload","pt":"msg","to":"payload","tot":"msg"},{"t":"set","p":"payload.part1","pt":"msg","to":"part 1","tot":"str"},{"t":"set","p":"payload.part2","pt":"msg","to":"this is part 2","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":150,"y":3320,"wires":[["db3b98d3.d2c5a8","9b6b1e2b.5fa24"]]},{"id":"db3b98d3.d2c5a8","type":"mqtt out","z":"d103f472.19f3c8","name":"","topic":"temp","qos":"","retain":"","broker":"378c0403.8cda04","x":320,"y":3320,"wires":[]},{"id":"3877c78e.e86728","type":"mqtt in","z":"d103f472.19f3c8","name":"","topic":"temp","qos":"2","datatype":"json","broker":"378c0403.8cda04","x":430,"y":3270,"wires":[["4360889e.602bf"]]},{"id":"4360889e.602bf","type":"function","z":"d103f472.19f3c8","name":"","func":"var newMsg = msg.payload; \nreturn newMsg;","outputs":1,"noerr":0,"x":560,"y":3270,"wires":[["e8c8383b.82e6f"]]},{"id":"9b6b1e2b.5fa24","type":"debug","z":"d103f472.19f3c8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":320,"y":3380,"wires":[]},{"id":"378c0403.8cda04","type":"mqtt-broker","z":"","name":"MQTT HOST","broker":"192.168.0.99","port":"1883","clientid":"","usetls":false,"compatmode":false,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthRetain":"true","birthPayload":"","closeTopic":"","closeQos":"0","closePayload":"","willTopic":"","willQos":"0","willPayload":""}]
You are aware that the mqtt node will only send what is in the msg.payload
?
As Nick already stated here:
So you're msg.part1
and msg.part2
will never be send and as you msg.payload
is a string the function after the mqtt in will give you this error as it will only work on objects.
You have to move any msg object properties that you want to be send over mqtt to the msg.payload
.
Yeah, sorry. Please look at the latest code I posted.
Still isn't working.
Yes because you cant use a change node like this. You are trying to set the property msg.payload.payload
but you cant do that because msg.payload
is a string and you cant set properties of a string so again only your initial string that is in msg.payload
gets send and causes the error in the function.
Ok.
I'll try setting the parts all in the node.
I set all parts like this:
[{"id":"13c865eb.e3f5ba","type":"change","z":"d103f472.19f3c8","name":"","rules":[{"t":"set","p":"payload.payload","pt":"msg","to":"This is the payload","tot":"str"},{"t":"set","p":"payload.part1","pt":"msg","to":"part 1","tot":"str"},{"t":"set","p":"payload.part2","pt":"msg","to":"this is part 2","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":150,"y":3320,"wires":[["db3b98d3.d2c5a8","9b6b1e2b.5fa24"]]}]
I'm now trying with a function
node.
[{"id":"467f1710.509fc","type":"inject","z":"447386dd.6b884","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"payload\":\"this is a payload\",\"part1\":\"this is part 1\",\"part2\":\"this is part 2\"}","payloadType":"json","x":370,"y":580,"wires":[["dc3f0cc5.c7a74","d9e9a6a1.a61c6"]]},{"id":"94b1c130.dc32e8","type":"debug","z":"447386dd.6b884","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":810,"y":580,"wires":[]},{"id":"dc3f0cc5.c7a74","type":"mqtt out","z":"447386dd.6b884","name":"","topic":"temp","qos":"","retain":"","broker":"4b4790c.bc5cf7","x":570,"y":590,"wires":[]},{"id":"9d9242b2.d81f3","type":"mqtt in","z":"447386dd.6b884","name":"","topic":"temp","qos":"2","datatype":"json","broker":"4b4790c.bc5cf7","x":680,"y":540,"wires":[["a0f89673.98d73"]]},{"id":"a0f89673.98d73","type":"function","z":"447386dd.6b884","name":"","func":"var newMsg = msg.payload; \nreturn newMsg;","outputs":1,"noerr":0,"x":810,"y":540,"wires":[["94b1c130.dc32e8"]]},{"id":"d9e9a6a1.a61c6","type":"debug","z":"447386dd.6b884","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":570,"y":650,"wires":[]},{"id":"4b4790c.bc5cf7","type":"mqtt-broker","z":"","name":"","broker":"pipspi1","port":"1883","clientid":"","usetls":false,"compatmode":false,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthRetain":"false","birthPayload":"","closeTopic":"","closeQos":"0","closeRetain":"false","closePayload":"","willTopic":"","willQos":"0","willRetain":"false","willPayload":""}]
Look at this
Edit
Yes a function node would have to be used if you wanted to manipulate a message with existing properties that you want to move to msg.payload
. Pretty much the reverse of what the function after the mqtt in node does. Something like this before an mqtt out node:
var newMsg = {payload:msg};
return newMsg;
would send the whole msg object over mqtt.
Ok. I was doing it with a function
node.
Sorry I missed the magic solution:
var newMsg = msg.payload;
return newMsg;
Shall document it for future use.
This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.