# Can anyone help me?

**URL:** <https://discourse.nodered.org/t/can-anyone-help-me/67829>\
**Category:** General\
**Tags:** function-node, mqtt\
**Created:** [17 September 2022 00:15 UTC](https://discourse.nodered.org/t/can-anyone-help-me/67829 "2022-09-17T00:15:20Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![PowerZone21](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/powerzone21/32/67873_2.png) [@PowerZone21](https://discourse.nodered.org/u/PowerZone21)\
**Post date:** [17 September 2022 00:15 UTC](https://discourse.nodered.org/t/can-anyone-help-me/67829/1 "2022-09-17T00:15:20Z")

</div>

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

{  
"serv": "kind-owl",  
"type": "evt.notification.report",  
"val\_t": "object",  
"val": {  
"EventName": "custom",  
"MessageContent": "global.get('lux\_avg\_level')",  
"SiteId": "XXXXXXXXXXXXXXXXXXX"  
},  
"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

---

<div class="post-metadata">

**Author:** ![michaelblight](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/michaelblight/32/445_2.png) [@michaelblight](https://discourse.nodered.org/u/michaelblight)\
**Post date:** [17 September 2022 06:23 UTC](https://discourse.nodered.org/t/can-anyone-help-me/67829/2 "2022-09-17T06:23:38Z")

</div>

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).

---

<div class="post-metadata">

**Author:** ![PowerZone21](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/powerzone21/32/67873_2.png) [@PowerZone21](https://discourse.nodered.org/u/PowerZone21)\
**Post date:** [17 September 2022 07:58 UTC](https://discourse.nodered.org/t/can-anyone-help-me/67829/3 "2022-09-17T07:58:03Z")

</div>

> [@michaelblight](#):
>
> $globalContext('lux\_avg\_level')

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  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/3/4/347f3d785f826d8e8d66c997bdc251929ca049ed.png)  
You can see the changenode and the mqtt node here

---

<div class="post-metadata">

**Author:** ![PowerZone21](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/powerzone21/32/67873_2.png) [@PowerZone21](https://discourse.nodered.org/u/PowerZone21)\
**Post date:** [17 September 2022 07:58 UTC](https://discourse.nodered.org/t/can-anyone-help-me/67829/4 "2022-09-17T07:58:41Z")

</div>

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/a/4/a42c31b2e84a639fa92c7a7e1e3b6581ffc89a4c.png)  
This is the change node with payload and topic  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/2/f/2f3a45bd080a194a704a337c6bd79c33a15cb93c.png)  
This is the JSON message with your suggestion, and the errormessage

---

<div class="post-metadata">

**Author:** ![smanjunath211](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/smanjunath211/32/95742_2.png) [@smanjunath211](https://discourse.nodered.org/u/smanjunath211)\
**Post date:** [17 September 2022 08:26 UTC](https://discourse.nodered.org/t/can-anyone-help-me/67829/5 "2022-09-17T08:26:44Z")

</div>

> [@michaelblight](#):
>
> should be set as `JSONata`

> [@PowerZone21](#):
>
> 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

---

<div class="post-metadata">

**Author:** ![Frida](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/frida/32/68250_2.png) [@Frida](https://discourse.nodered.org/u/Frida)\
**Post date:** [17 September 2022 17:52 UTC](https://discourse.nodered.org/t/can-anyone-help-me/67829/6 "2022-09-17T17:52:19Z")

</div>

```auto
[{"id":"b0ad0d72ad087faa","type":"change","z":"829f16a2994adbca","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"{ \"serv\": \"kind-owl\", \"type\": \"evt.notification.report\", \"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.

---

<div class="post-metadata">

**Author:** ![PowerZone21](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/powerzone21/32/67873_2.png) [@PowerZone21](https://discourse.nodered.org/u/PowerZone21)\
**Post date:** [17 September 2022 19:12 UTC](https://discourse.nodered.org/t/can-anyone-help-me/67829/7 "2022-09-17T19:12:09Z")

</div>

Sorry, didn't work, but no errormessage either

---

<div class="post-metadata">

**Author:** ![smanjunath211](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/smanjunath211/32/95742_2.png) [@smanjunath211](https://discourse.nodered.org/u/smanjunath211)\
**Post date:** [18 September 2022 01:59 UTC](https://discourse.nodered.org/t/can-anyone-help-me/67829/8 "2022-09-18T01:59:25Z")

</div>

> [@Frida](#):
>
> Try this.

Works for me.

> [@PowerZone21](#):
>
> Sorry, didn't work

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

---

<div class="post-metadata">

**Author:** ![michaelblight](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/michaelblight/32/445_2.png) [@michaelblight](https://discourse.nodered.org/u/michaelblight)\
**Post date:** [18 September 2022 03:23 UTC](https://discourse.nodered.org/t/can-anyone-help-me/67829/9 "2022-09-18T03:23:29Z")

</div>

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

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/1/6/16e91421284da8077432fc91f17adf34719add11.png)

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

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/9/0/90030ba972c3c8c8a7cd6a8da36f17c380ed49ae.png)

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.

---

<div class="post-metadata">

**Author:** ![PowerZone21](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/powerzone21/32/67873_2.png) [@PowerZone21](https://discourse.nodered.org/u/PowerZone21)\
**Post date:** [18 September 2022 10:51 UTC](https://discourse.nodered.org/t/can-anyone-help-me/67829/10 "2022-09-18T10:51:28Z")

</div>

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":"evt.notification.report","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"]]}]

---

<div class="post-metadata">

**Author:** ![PowerZone21](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/powerzone21/32/67873_2.png) [@PowerZone21](https://discourse.nodered.org/u/PowerZone21)\
**Post date:** [18 September 2022 10:56 UTC](https://discourse.nodered.org/t/can-anyone-help-me/67829/11 "2022-09-18T10:56:38Z")

</div>

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:  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/9/e/9eb2fecfb7c1cecd5447262ff8550bb8b69c8108.png)  
Debug from @Frida example:

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/1/e/1ed11413380647db8900ee576d3eaa262cab4df4.png)

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [17 November 2022 10:57 UTC](https://discourse.nodered.org/t/can-anyone-help-me/67829/12 "2022-11-17T10:57:21Z")

</div>

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