How to use msg and global variables in expression

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 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!

Do you mean, or perhaps

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.

it's in, and I've tried the following:

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.

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.

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


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

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?

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