# How to use msg and global variables in expression

**URL:** <https://discourse.nodered.org/t/how-to-use-msg-and-global-variables-in-expression/71497>\
**Category:** General\
**Created:** [30 November 2022 10:14 UTC](https://discourse.nodered.org/t/how-to-use-msg-and-global-variables-in-expression/71497 "2022-11-30T10:14:16Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![tamueller](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/tamueller/32/68690_2.png) [@tamueller](https://discourse.nodered.org/u/tamueller)\
**Post date:** [30 November 2022 10:14 UTC](https://discourse.nodered.org/t/how-to-use-msg-and-global-variables-in-expression/71497/1 "2022-11-30T10:14:16Z")

</div>

Hi,  
I'm trying to use a msg-variable and a global variable in a node that takes an "expression", but I somehow encounter strange problems.  
I previously used this syntax, when using only global variables, and it worked:  
`{ "brightness": "{{global.flux_brightness}}", "kelvin": "{{global.flux_temperature}}", "transition": 1 }`

Now I want to include a message-variable, and run into problems. With:  
`{ "brightness": "{{data.attributes.brightness}}", "kelvin": "{{global.flux_temperature}}", "transition": 1 }`  
I get "Call-service error. expected int for dictionary value @ data['brightness']" so the {{ }} for data.attributes.brightness is not evaluated correctly. I also tried msg.data.. or $$.data... or $.data..

When I do it like this:  
`{ "brightness": data.attributes.brightness, "kelvin": "{{global.flux_temperature}}", "transition": 1 }`  
I get an API-Error saying: "Call-service error. expected int for dictionary value @ data['kelvin']", so the {{ }} global flux is not evaluated correctly, even though the exact same syntax worked before.

Even stranger, when I use:  
`{ "brightness": data.attributes.brightness, "kelvin": global.flux_temperature, "transition": 1 }`,  
because then I don't get an error message, but the kelvin values are completly ignored, while the brightness value seems to work. So global.flux\_temperature is either evaluated incorrectly, or ignored.

Even after reading everything I found using googles about Mustache-Templates in NodeRed I don't really understand this behavior - why is there an interference between the brightness and the kelvin part? and how can I use both a msg variable and a global variable in one expression?

Thanks for your help!

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [30 November 2022 10:41 UTC](https://discourse.nodered.org/t/how-to-use-msg-and-global-variables-in-expression/71497/2 "2022-11-30T10:41:38Z")

</div>

> [@tamueller](#):
>
> {{data.attributes.brightness}}

Do you mean msg.data.attributes.brightness, or perhaps msg.payload.data...?

If you don't know then show us the message going into that node. I assume it is a Template node, though you have not told us that.

---

<div class="post-metadata">

**Author:** ![tamueller](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/tamueller/32/68690_2.png) [@tamueller](https://discourse.nodered.org/u/tamueller)\
**Post date:** [30 November 2022 14:05 UTC](https://discourse.nodered.org/t/how-to-use-msg-and-global-variables-in-expression/71497/3 "2022-11-30T14:05:52Z")

</div>

Hi,  
it's in msg.data.attributes.brightness, and I've tried the following:  
msg.data.attributes.brightness  
data.attributes.brightness  
$.data.attributes.brightness  
$$.data.attributes.brightness

And it's a "call service"-node from node-red-contrib-home-assistant-websocket, which accepts an J-expression. But since I guess evaluation of expression is not done by the plugin, but by NodeRed itself, I think it's a general problem I have.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [30 November 2022 14:14 UTC](https://discourse.nodered.org/t/how-to-use-msg-and-global-variables-in-expression/71497/4 "2022-11-30T14:14:46Z")

</div>

> [@tamueller](#):
>
> it's a "call service"-node from node-red-contrib-home-assistant-websocket, which accepts an J-expression.

Test it in a Template node to see if it works. We can't help with the call-service node as we don't use HA (at least very few here do).  
If it doesn't work in a template node then add show us what is going into and out of the template node and how you have configured it.

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [30 November 2022 17:00 UTC](https://discourse.nodered.org/t/how-to-use-msg-and-global-variables-in-expression/71497/5 "2022-11-30T17:00:31Z")

</div>

> [@tamueller](#):
>
> { "brightness": "{{global.flux\_brightness}}", "kelvin": "{{global.flux\_temperature}}", "transition": 1 }

If you set the type input to JSONata J: then you can use

```auto
{ 

  "brightness": $globalContext("flux_brightness"), 
  "kelvin": $globalContext("flux_temperature"), 
  "transition": 1 
}

```

---

<div class="post-metadata">

**Author:** ![tamueller](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/tamueller/32/68690_2.png) [@tamueller](https://discourse.nodered.org/u/tamueller)\
**Post date:** [30 November 2022 18:07 UTC](https://discourse.nodered.org/t/how-to-use-msg-and-global-variables-in-expression/71497/6 "2022-11-30T18:07:19Z")

</div>

Ah, I think I got confused between templates and JSONata. When using JSONata (and data.attributes.brightness, so without the msg prefix) I can combine everything I need, so thanks for the help!

What I don't understand: When a node offers a J-Expression field, will it also evaluate {{ }} templates or only JSONata? Because `{ "brightness": "{{global.flux_brightness}}", "kelvin": "{{global.flux_temperature}}", "transition": 1 } ` worked in the J-Expression field of the "call service" node as well, while it does not work in the J-Expression field of e.g. debug-nodes. Or is every node parsing expressions on their own?

---

<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:** [29 January 2023 18:07 UTC](https://discourse.nodered.org/t/how-to-use-msg-and-global-variables-in-expression/71497/7 "2023-01-29T18:07:48Z")

</div>

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