# Using a global variable in the data/json

**URL:** https://discourse.nodered.org/t/using-a-global-variable-in-the-data-json/91145
**Category:** General
**Tags:** home-assistant
**Created:** [20 September 2024 15:26 UTC](https://discourse.nodered.org/t/using-a-global-variable-in-the-data-json/91145 "2024-09-20T15:26:05Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![Crabsticks](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/crabsticks/32/94714_2.png) [@Crabsticks](https://discourse.nodered.org/u/Crabsticks)
#### Post date: [20 September 2024 15:26 UTC](https://discourse.nodered.org/t/using-a-global-variable-in-the-data-json/91145/1 "2024-09-20T15:26:05Z")

</div>

Hello,

I'm trying to create a flow to change the brightness of my light, I can set the brightness directly however I can't figure out how to use a global variable I've set elsewhere in the flow. I'm sure I'm just not using the right syntax.

currently my json looks like this

```auto
{
    "entity_id": "light.living_room_main_light_light",
    "brightness": 22
}

```

instead of the number 22 I want to use a variable global.brightness put I dom not know how to import the variable into the json correctly.

Thanks in advince

---

<div class="post-metadata">

### Author: ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)
#### Post date: [20 September 2024 15:33 UTC](https://discourse.nodered.org/t/using-a-global-variable-in-the-data-json/91145/2 "2024-09-20T15:33:03Z")

</div>

There are several ways.

The easiest is to pass the `msg` through a change node and set `msg.payload.brightness` to `global.brightness`

> [@Crabsticks](#):
>
> instead of the number 22 I want to use a variable global.brightness put I dom not know how to import the variable into the json correctly.

Assuming you mean a JS Object (JSON is a string), then the above will work.

---

<div class="post-metadata">

### Author: ![Crabsticks](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/crabsticks/32/94714_2.png) [@Crabsticks](https://discourse.nodered.org/u/Crabsticks)
#### Post date: [20 September 2024 15:50 UTC](https://discourse.nodered.org/t/using-a-global-variable-in-the-data-json/91145/3 "2024-09-20T15:50:12Z")

</div>

Sorry if JSON is not the correct term.

I'm using a service call node and editing the data field.

I need to be able to pull in the global variable.

I've created a payload message as you suggested, how do I use the payload instead of the 22 value?

---

<div class="post-metadata">

### Author: ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)
#### Post date: [20 September 2024 15:53 UTC](https://discourse.nodered.org/t/using-a-global-variable-in-the-data-json/91145/4 "2024-09-20T15:53:44Z")

</div>

> [@Crabsticks](#):
>
> I'm using a service call node

I have no idea what that is.

does the nodes field support JSONata? You could use JSONata to grab context values. Or perhaps there is a `msg` fired into this " service call node" in which case you can use the switch node BEFORE it to grab the global value and add it to the payload?

If this is a home assistant node, then please tag the post with #home-assistant so that others are not tripped up too.

---

<div class="post-metadata">

### Author: ![Crabsticks](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/crabsticks/32/94714_2.png) [@Crabsticks](https://discourse.nodered.org/u/Crabsticks)
#### Post date: [20 September 2024 15:59 UTC](https://discourse.nodered.org/t/using-a-global-variable-in-the-data-json/91145/5 "2024-09-20T15:59:14Z")

</div>

It is a home assistant node, sorry for the confusion.

I'll try and figure out how to tag the post

---

<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: [20 September 2024 16:17 UTC](https://discourse.nodered.org/t/using-a-global-variable-in-the-data-json/91145/6 "2024-09-20T16:17:10Z")

</div>

Select J: (jsonata expression) rather than JSON type input.  
Then

```auto
{
    "entity_id": "light.living_room_main_light_light",
    "brightness": globalContext("brightness")
}

```

---

<div class="post-metadata">

### Author: ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)
#### Post date: [20 September 2024 16:19 UTC](https://discourse.nodered.org/t/using-a-global-variable-in-the-data-json/91145/7 "2024-09-20T16:19:51Z")

</div>

> [@Crabsticks](#):
>
> how do I use the payload instead of the 22 value?

```auto
{
    "entity_id": "light.living_room_main_light_light",
    "brightness": msg.payload
}

```

---

<div class="post-metadata">

### Author: ![Crabsticks](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/crabsticks/32/94714_2.png) [@Crabsticks](https://discourse.nodered.org/u/Crabsticks)
#### Post date: [20 September 2024 17:11 UTC](https://discourse.nodered.org/t/using-a-global-variable-in-the-data-json/91145/8 "2024-09-20T17:11:25Z")

</div>

> [@E1cid](#):
>
> ```auto
> {
> "entity_id": "light.living_room_main_light_light",
> "brightness": globalContext("brightness")
> }
> 
> ```

many thinkas , this was what I needed , It's all working now 🙂

---

<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: [4 October 2024 17:11 UTC](https://discourse.nodered.org/t/using-a-global-variable-in-the-data-json/91145/9 "2024-10-04T17:11:26Z")

</div>

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