MQTT into function cannot read message

I have a fairly simple flow where the mqtt node reads into a function. I can see the mqtt output in a debug node and yet when I try to read that value in a function it does not work. It's actually like the function doesn't even see the input into it. I have deleted the mqtt node and recreated it, deleted the function and recreated it. Still nothing. Other mqtt nodes work fine just this one wants to cause me grief.


3
2

Any thoughts appreciated

[EDIT] I get an output on the debug and yet I see nothing on the node.status of the function. I would expect to see something on the node.status

That's weird indeed. I suppose if you inject a payload: "off" manually into the limit switch function node it works?

Do you have the code in the On Message section of the Function node ?

Ah, didn't think of that one, will try.

Add a debug to the output of the function and see what comes out

First, did set up two inject nodes, one for off, one for on and that does work through the function. And a debug at the output of the function shows nothing when using the mqtt node. What I would expect with the inject nodes.

hi @gerry, I just tried with your code, and work well. Also I share other option, with switch and change node, to compare and replace the msg.payload. I hope it helps you!!

[{"id":"18fafe0f8ccf3ab4","type":"tab","label":"Flow 3","disabled":false,"info":""},{"id":"90536333734d8c40","type":"inject","z":"18fafe0f8ccf3ab4","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"door","payload":"on","payloadType":"str","x":90,"y":100,"wires":[["7f1067e4a112bc3c"]]},{"id":"332ff57eee6be44d","type":"debug","z":"18fafe0f8ccf3ab4","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":550,"y":140,"wires":[]},{"id":"3806434c0989a659","type":"inject","z":"18fafe0f8ccf3ab4","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"door","payload":"off","payloadType":"str","x":90,"y":160,"wires":[["7f1067e4a112bc3c"]]},{"id":"7f1067e4a112bc3c","type":"switch","z":"18fafe0f8ccf3ab4","name":"","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"on","vt":"str"},{"t":"eq","v":"off","vt":"str"}],"checkall":"true","repair":false,"outputs":2,"x":210,"y":140,"wires":[["8390f5af5b81366d"],["6cc5590c1f527468"]]},{"id":"8390f5af5b81366d","type":"change","z":"18fafe0f8ccf3ab4","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"door open","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":360,"y":120,"wires":[["332ff57eee6be44d"]]},{"id":"6cc5590c1f527468","type":"change","z":"18fafe0f8ccf3ab4","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"door closed","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":360,"y":160,"wires":[["332ff57eee6be44d"]]},{"id":"459dbee4dc6bbee0","type":"function","z":"18fafe0f8ccf3ab4","name":"","func":"var message = msg.payload;\nnode.status({fill:\"blue\",shape:\"dot\",text:msg.payload});\nif(message == \"on\"){\n    msg.payload = \"door open\";\n}\nif (message == \"off\") {\n    msg.payload = \"door closed\";\n}\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":310,"y":320,"wires":[["b4da4cc8b5d17176"]]},{"id":"d790ee364f314e12","type":"inject","z":"18fafe0f8ccf3ab4","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"door","payload":"on","payloadType":"str","x":90,"y":280,"wires":[["459dbee4dc6bbee0"]]},{"id":"7fee6cfaca7a15d2","type":"inject","z":"18fafe0f8ccf3ab4","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"door","payload":"off","payloadType":"str","x":90,"y":340,"wires":[["459dbee4dc6bbee0"]]},{"id":"b4da4cc8b5d17176","type":"debug","z":"18fafe0f8ccf3ab4","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":490,"y":320,"wires":[]}]

Captura de pantalla 2021-07-31 a la(s) 2.26.47 p. m.
with your (similar) code:
Captura de pantalla 2021-07-31 a la(s) 2.26.56 p. m.

and the other option with switch and change node:

Captura de pantalla 2021-07-31 a la(s) 2.26.40 p. m.

looks like:
Captura de pantalla 2021-07-31 a la(s) 2.27.13 p. m.

@MecatronicaMADE when I import your flow (comes up flow 3) it works. When I import your code into my tab, it doesn't work. I am leaning towards that tab has bad karma. I'm running beta 2 ver 1 on that machine and coming to the decision I will update to latest versions and see if that somehow auto magically fixes things. I have put far too much effort into this and since this is a holiday weekend here the manager (wife) has decided if I get on my laptop one more time this weekend instead of being with the kids I will regret it. I tend to believe her so this is going to get put away for a few days.
Thanks everyone

Please don't tell da manager that you are running beta 2 still :scream:

Just to make sure that the function node is connected to the mqtt-in, try moving it and see if the wires move with it.

I’ve been known to think I dropped a flow on a wire, but it didn’t connect.

Hid in the closet for a few minutes....
Checked and the wires are connected and the code is in on message

Export your flow and attach it to a reply so we can try it.

Interesting. It would be nice to visually indicate if there is an actual wire connected (change the color of the connection dot slightly, for example) . Maybe this has been discussed before?

Why do you destroy the original msg ? Is this something you often do? I recommend you break that habit.

Try changing all code in all functions in this :point_up: flow to reuse the original msg object e.g....

msg.payload = "door open";
1 Like

Maybe I'm missing something, but doesn't this function always return "door closed"? Maybe a few else's would clarify the logic?

From the screenshot you shared, of the Debug node .. what strikes me as unusual is that the msg doesn't have a Topic.

image

If you have a Debug node directly connected to any MQTT node and set it to Complete msg (as you are) .. it would always have a Topic.

are you sure that debug message is from that Mqtt node and not from some other part of your flows ?

2 Likes

I was wondering that myself.

@gerry please export your flow and attach it to a reply

@zenofmud and @molesworth you are correct about always returning the one msg. The issue was and still is, when that node gets a msg from the mqtt node I get no output. So I added the last piece to see if my logic was failing or what. Turns out it is not a logic issue, the node receives the msg but doesn't out put anything,
@Steve-Mcl I googled destroy object and while I am confident I can blow up small villages now I still don't understand the nuances of your statement.
Also, I'm not sure how you can test my flow since it works with the inject nodes but not with mqtt. Do you have someway to input into the mqtt node?
Here's the flow..

[{"id":"3202129f.965a9e","type":"tab","label":"Garage","disabled":false,"info":""},{"id":"2911c558.0e32ba","type":"mqtt in","z":"3202129f.965a9e","name":"temp c","topic":"Gout_tempC","qos":"2","datatype":"auto","broker":"6fbbc66a.737958","x":210,"y":160,"wires":[["be401294.d77ab8"]]},{"id":"be401294.d77ab8","type":"function","z":"3202129f.965a9e","name":"temp C","func":"let z = Number(msg.payload);\nz = z.toFixed(2);  \nmsg = {\n    payload: 'Deg C ' + z,\n    topic: 'tempC',\n}\n\nnode.status({fill:\"blue\",shape:\"dot\",text:msg.payload});\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":440,"y":160,"wires":[["10192b9d.7462ec"]]},{"id":"e9152864.b9f088","type":"mqtt in","z":"3202129f.965a9e","name":"temp F","topic":"Gout_tempF","qos":"2","datatype":"auto","broker":"6fbbc66a.737958","nl":false,"rap":false,"x":210,"y":240,"wires":[["d272cf84.0923e"]]},{"id":"d272cf84.0923e","type":"function","z":"3202129f.965a9e","name":"temp F","func":"let z = Number(msg.payload);\nz = z.toFixed(2);  \nmsg = {\n    payload: 'Deg F ' + z,\n    topic: 'tempF',\n}\n \n    \nnode.status({fill:\"blue\",shape:\"dot\",text:msg.payload});\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":440,"y":240,"wires":[["6accbfac.669eb"]]},{"id":"9669f95d.519588","type":"mqtt in","z":"3202129f.965a9e","name":"limit switch","topic":"OnLimitSwitch","qos":"2","datatype":"auto","broker":"6fbbc66a.737958","nl":false,"rap":false,"x":220,"y":300,"wires":[["80dec21e.e4b18","f3b571fd5059739f"]]},{"id":"80dec21e.e4b18","type":"function","z":"3202129f.965a9e","name":"limit switch","func":"var message = msg.payload;\nnode.status({fill:\"blue\",shape:\"dot\",text:msg.payload});\nif(message == \"on\"){\n    msg.payload = \"door open\";\n}\nif (message == \"off\") {\n    msg.payload = \"door closed\";\n}\n\nreturn msg;\n\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":450,"y":300,"wires":[["17f7d5f9.bbddf2"]]},{"id":"17f7d5f9.bbddf2","type":"ui_template","z":"3202129f.965a9e","group":"b2e45b14.e88998","name":"limit switch","order":1,"width":0,"height":0,"format":"<div>\n   <md-button class=\"md-button mine-button\"\n    data-topic=\"target\"\n    data-payload=\"1\">{{msg.payload}}\n   </md-button>\n</div>","storeOutMessages":false,"fwdInMessages":false,"resendOnRefresh":false,"templateScope":"local","x":930,"y":300,"wires":[[]]},{"id":"39de04a9.004564","type":"function","z":"3202129f.965a9e","name":"humid","func":"let z = Number(msg.payload);\nz = z.toFixed(2); \nmsg = {\n    payload: 'RH% ' + z,\n    topic: 'humid',\n}\n\nnode.status({fill:\"blue\",shape:\"dot\",text:msg.payload});\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":430,"y":100,"wires":[["cca0f12e.33553"]]},{"id":"14be87a5.d6e988","type":"mqtt in","z":"3202129f.965a9e","name":"humid","topic":"Gout_humid","qos":"2","datatype":"auto","broker":"6fbbc66a.737958","nl":false,"rap":false,"x":210,"y":100,"wires":[["39de04a9.004564"]]},{"id":"10192b9d.7462ec","type":"ui_template","z":"3202129f.965a9e","group":"b2e45b14.e88998","name":"temp C","order":3,"width":0,"height":0,"format":"<div>\n   <md-button class=\"md-button mine-button\"\n    data-topic=\"target\"\n    data-payload=\"1\">{{msg.payload}}\n   </md-button>\n</div>","storeOutMessages":false,"fwdInMessages":false,"resendOnRefresh":false,"templateScope":"local","x":920,"y":160,"wires":[[]]},{"id":"cca0f12e.33553","type":"ui_template","z":"3202129f.965a9e","group":"b2e45b14.e88998","name":"humid","order":2,"width":0,"height":0,"format":"<div>\n   <md-button class=\"md-button mine-button\"\n    data-topic=\"target\"\n    data-payload=\"1\">{{msg.payload}}\n   </md-button>\n</div>","storeOutMessages":false,"fwdInMessages":false,"resendOnRefresh":false,"templateScope":"local","x":910,"y":100,"wires":[[]]},{"id":"6accbfac.669eb","type":"ui_template","z":"3202129f.965a9e","group":"b2e45b14.e88998","name":"temp F","order":4,"width":0,"height":0,"format":"<div>\n   <md-button class=\"md-button mine-button\"\n    data-topic=\"target\"\n    data-payload=\"1\">{{msg.payload}}\n   </md-button>\n</div>","storeOutMessages":false,"fwdInMessages":false,"resendOnRefresh":false,"templateScope":"local","x":920,"y":240,"wires":[[]]},{"id":"f3b571fd5059739f","type":"function","z":"3202129f.965a9e","name":"test","func":"\n node.status({fill:\"blue\",shape:\"dot\",text:msg.payload});\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":530,"y":360,"wires":[[]]},{"id":"ea573274bafe4774","type":"inject","z":"3202129f.965a9e","name":"off","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"off","payloadType":"str","x":140,"y":400,"wires":[["80dec21e.e4b18"]]},{"id":"d839df954907737a","type":"inject","z":"3202129f.965a9e","name":"on","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"on","payloadType":"str","x":110,"y":360,"wires":[["80dec21e.e4b18"]]},{"id":"6fbbc66a.737958","type":"mqtt-broker","name":"127.0.0.1","broker":"127.0.0.1","port":"1883","clientid":"","usetls":false,"compatmode":false,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","closeTopic":"","closeQos":"0","closePayload":"","willTopic":"","willQos":"0","willPayload":""},{"id":"b2e45b14.e88998","type":"ui_group","name":"Door","tab":"16fd26cf.f8ed21","order":1,"disp":false,"width":6,"collapse":false},{"id":"16fd26cf.f8ed21","type":"ui_tab","name":"Garage","icon":"fa-heart","order":10,"disabled":false,"hidden":false}]

Thanks everyone

Please add a debug node (set to display the complete msg object) to the `mqtt-in' node and run a test. When you get the debug output please copy and paste it to a reply.

See first post please, last image