Can anyone help me?

I got this message wich is a Notification message I use to push into Futurehome via MQTT

"serv": "kind-owl",
"type": "",
"val_t": "object",
"val": {
"EventName": "custom",
"MessageContent": "global.get('lux_avg_level')",
"props": {},
"tags": null,
"ver": "1",
"uid": "",
"topic": "pt:j1/mt:evt/rt:app/rn:kind_owl/ad:1"

I want to put a global variable into Message Content, but any way I try, I just get errormessage

You've not posted what error you're getting, or any context around this JSON.

Firstly, you should not have global.get() in quotes. With it in quotes it is interpreted as a string. Secondly, what node is this in? If it's in a function node, post the whole function. If it's in a template node it should be using mustache format - {{ global.get('lux_avg_level') }}. If it's in a change node it should be set as JSONata and that format used - $globalContext('lux_avg_level').

Also, I would have expected you would want to set msg.topic for the MQTT topic, whereas the above is (probably) setting msg.payload.topic (depending on what you're actually doing with this JSON).

I will try to explain.
My main smart hub are Futurehome, but i'm not satisfied with the role engine they are using, so i run Node-Red on a Respberry Pi to use as a role engine, communicating to Futurehome via MQTT, Wich is working very good.
But now i want to push a message from Node-Red to the userinterface for Fututrehome.
This also works, but only with static text within quotes. This is where I want to put in a Global variable
You can see the changenode and the mqtt node here

This is the change node with payload and topic

This is the JSON message with your suggestion, and the errormessage

i think JSONata and JSON is the difference here, i am no expert, but i had similar issue which i got resolved from this forum

[{"id":"b0ad0d72ad087faa","type":"change","z":"829f16a2994adbca","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"{ \"serv\": \"kind-owl\", \"type\": \"\", \"val_t\": \"object\", \"val\": { \"EventName\": \"custom\", \"MessageContent\": $globalContext('lux_avg_level'), \"SiteId\": \"XXXXXXXXXXXXXXXXXXX\" }, \"props\": {}, \"tags\": null, \"ver\": \"1\", \"uid\": \"\", \"topic\": \"pt:j1/mt:evt/rt:app/rn:kind_owl/ad:1\" }","tot":"jsonata"},{"t":"set","p":"topic","pt":"msg","to":"","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":380,"y":420,"wires":[["b3d141c20aaf2233"]]}]

Try this.

Sorry, didn't work, but no errormessage either

Works for me.

it would help if you posted your change node with ctrl+e

You currently have your change node set to JSON like this...


But it needs to be JSONata (expression) like this...


JSON does not allow you to include code, only strings, numbers, etc. JSONata allows you to include code, and the function reference inside its editor shows you what things you can do. There is also a handy 'test' tab in there that lets you simulate an incoming message and see what you would get with the code you have written.

You should also put a debug node after the change node so you can see you're getting what you expect to get.

Here are a copy of the hole changenode that are functional against futurehome, the output goes directly to a MQTT node

[{"id":"414352161b571b1f","type":"change","z":"8e1dc754b3299609","name":"Functional Futurehome Notification","rules":[{"t":"set","p":"payload","pt":"msg","to":"{"serv":"kind-owl","type":"","val_t":"object","val":{"EventName":"custom","MessageContent":"Testmelding","SiteId":"XXXXX"},"props":{},"tags":null,"ver":"1","uid":"XXXXX","topic":"pt:j1/mt:evt/rt:app/rn:kind_owl/ad:1"}","tot":"json"},{"t":"set","p":"topic","pt":"msg","to":"pt:j1/mt:evt/rt:app/rn:kind_owl/ad:1","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":600,"y":440,"wires":[["1e371ba830605c72"]]}]

Thanks for your tip, and I tried, and I can see the debug message for both my functional node, and the node i tried from @Frida

Debug from the fuctional node:
Debug from @Frida example: