# Is it possible to use a flow variable as a value in a JSON string?

**URL:** <https://discourse.nodered.org/t/is-it-possible-to-use-a-flow-variable-as-a-value-in-a-json-string/10377>\
**Category:** General\
**Created:** [20 April 2019 09:59 UTC](https://discourse.nodered.org/t/is-it-possible-to-use-a-flow-variable-as-a-value-in-a-json-string/10377 "2019-04-20T09:59:05Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![simonk83](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/simonk83/32/7651_2.png) [@simonk83](https://discourse.nodered.org/u/simonk83)\
**Post date:** [20 April 2019 09:59 UTC](https://discourse.nodered.org/t/is-it-possible-to-use-a-flow-variable-as-a-value-in-a-json-string/10377/1 "2019-04-20T09:59:05Z")

</div>

Essentially I have a number stored in a flow, called flow.get("MinCool") which is dynamic but currently set to 20 for examples sake. I'm trying to pass this number back to HomeAssistant using a Call Service node, which requires that number in the following JSON format:

{"value": 20}

So what I need to do is format that somehow to basically be:

{"value": flow.get("MinCool"}

That doesn't work, obviously, and I've been googling for a good hour now but can't quite get it right (if it's even possible)?

Here's the dialog in question:

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

---

<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 April 2019 10:03 UTC](https://discourse.nodered.org/t/is-it-possible-to-use-a-flow-variable-as-a-value-in-a-json-string/10377/2 "2019-04-20T10:03:19Z")

</div>

Use a function node...

```auto
msg.payload = {
    "value": flow.get("MinCool") || 0
}
return msg;

```

---

<div class="post-metadata">

**Author:** ![simonk83](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/simonk83/32/7651_2.png) [@simonk83](https://discourse.nodered.org/u/simonk83)\
**Post date:** [20 April 2019 10:04 UTC](https://discourse.nodered.org/t/is-it-possible-to-use-a-flow-variable-as-a-value-in-a-json-string/10377/3 "2019-04-20T10:04:08Z")

</div>

I would but I need to add the value in this specific Call Service node as far as I know...

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/7/7c9d7095e16eb5161016f6ac07aeb9c44083e652.png)

---

<div class="post-metadata">

**Author:** ![simonk83](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/simonk83/32/7651_2.png) [@simonk83](https://discourse.nodered.org/u/simonk83)\
**Post date:** [20 April 2019 10:08 UTC](https://discourse.nodered.org/t/is-it-possible-to-use-a-flow-variable-as-a-value-in-a-json-string/10377/4 "2019-04-20T10:08:19Z")

</div>

Ah hang on, reading the Info for it, it says I can use a template, so maybe I can pass along what I need using a function node before it afterall... I'll give that a try

---

<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 April 2019 10:10 UTC](https://discourse.nodered.org/t/is-it-possible-to-use-a-flow-variable-as-a-value-in-a-json-string/10377/5 "2019-04-20T10:10:08Z")

</div>

Perhaps it lets you send it if you leave Data blank? ...

```auto
msg.Data = {
    "value": flow.get("MinCool") || 0
}
return msg;

```

What does the info say?

---

<div class="post-metadata">

**Author:** ![simonk83](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/simonk83/32/7651_2.png) [@simonk83](https://discourse.nodered.org/u/simonk83)\
**Post date:** [20 April 2019 10:20 UTC](https://discourse.nodered.org/t/is-it-possible-to-use-a-flow-variable-as-a-value-in-a-json-string/10377/6 "2019-04-20T10:20:21Z")

</div>

Hmm that one doesn't seem to work

---

<div class="post-metadata">

**Author:** ![simonk83](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/simonk83/32/7651_2.png) [@simonk83](https://discourse.nodered.org/u/simonk83)\
**Post date:** [20 April 2019 10:22 UTC](https://discourse.nodered.org/t/is-it-possible-to-use-a-flow-variable-as-a-value-in-a-json-string/10377/7 "2019-04-20T10:22:53Z")

</div>

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/2/2b83f9418ba087a4df58c63f4f9ee060f6741373.png)

I'm assuming the answer lies there but I can't quite work it out ...

---

<div class="post-metadata">

**Author:** ![ukmoose](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ukmoose/32/13_2.png) [@ukmoose](https://discourse.nodered.org/u/ukmoose)\
**Post date:** [20 April 2019 10:52 UTC](https://discourse.nodered.org/t/is-it-possible-to-use-a-flow-variable-as-a-value-in-a-json-string/10377/8 "2019-04-20T10:52:45Z")

</div>

{{flow.MinCool}}

---

<div class="post-metadata">

**Author:** ![simonk83](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/simonk83/32/7651_2.png) [@simonk83](https://discourse.nodered.org/u/simonk83)\
**Post date:** [20 April 2019 11:02 UTC](https://discourse.nodered.org/t/is-it-possible-to-use-a-flow-variable-as-a-value-in-a-json-string/10377/9 "2019-04-20T11:02:16Z")

</div>

Right, I mean I assumed it would be

{"value": {{flow.MinCool}}}

But that just tells me Bad String

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [20 April 2019 11:23 UTC](https://discourse.nodered.org/t/is-it-possible-to-use-a-flow-variable-as-a-value-in-a-json-string/10377/10 "2019-04-20T11:23:25Z")

</div>

My guess is you have to set the field to the `string` type of you want to use the template syntax. The JSON input certainly doesn't support it natively.

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [20 April 2019 11:23 UTC](https://discourse.nodered.org/t/is-it-possible-to-use-a-flow-variable-as-a-value-in-a-json-string/10377/11 "2019-04-20T11:23:38Z")

</div>

In the JSON editor use `$flowContext("MinCool")` same goes for globals use $globalContex

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [20 April 2019 11:25 UTC](https://discourse.nodered.org/t/is-it-possible-to-use-a-flow-variable-as-a-value-in-a-json-string/10377/12 "2019-04-20T11:25:40Z")

</div>

> [@zenofmud](#):
>
> In the JSON editor use...

No. That is JSONata expression. Not JSON.

---

<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 April 2019 11:29 UTC](https://discourse.nodered.org/t/is-it-possible-to-use-a-flow-variable-as-a-value-in-a-json-string/10377/13 "2019-04-20T11:29:44Z")

</div>

If the above don't work then I would guess...

1. Add a change node before it to set msg.payload to flow.MinCool
2. use `{ "value" : {{payload}} }` as the template for Data

---

<div class="post-metadata">

**Author:** ![simonk83](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/simonk83/32/7651_2.png) [@simonk83](https://discourse.nodered.org/u/simonk83)\
**Post date:** [20 April 2019 11:32 UTC](https://discourse.nodered.org/t/is-it-possible-to-use-a-flow-variable-as-a-value-in-a-json-string/10377/14 "2019-04-20T11:32:58Z")

</div>

Hmm, unfortunately I can't change it to string in the node, it has to be JSON as that's all homeassistant will accept for that value.

There's this in the info which makes it sound like I should be able to force this to work somehow, I just can't quite grasp it:

> If the incoming message has a `payload` property with `domain` , `service` set it will override any config values if set.
> 
> If the incoming message has a `payload.data` that is an object or parseable into an object these properties will be **merged** with any config values set.

There's also this guy: [Reddit - Dive into anything](https://www.reddit.com/r/nodered/comments/8tyfqd/how_to_use_the_template_node_in_nodered/) who sounds like he's done what I need with a template, but I still don't quite get it...

---

<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 April 2019 11:35 UTC](https://discourse.nodered.org/t/is-it-possible-to-use-a-flow-variable-as-a-value-in-a-json-string/10377/15 "2019-04-20T11:35:59Z")

</div>

Looking at your screen shot,

`{ "value" : {{flow.MinCool}} }` should work (assuming you need an object with 1 property named value)

---

<div class="post-metadata">

**Author:** ![simonk83](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/simonk83/32/7651_2.png) [@simonk83](https://discourse.nodered.org/u/simonk83)\
**Post date:** [20 April 2019 11:40 UTC](https://discourse.nodered.org/t/is-it-possible-to-use-a-flow-variable-as-a-value-in-a-json-string/10377/16 "2019-04-20T11:40:47Z")

</div>

> [@Steve-Mcl](#):
>
> { "value" : {{flow.MinCool}} }

I think I see the problem. The above is accepted by the node as valid IF I include quotes, so { "value" : "{{flow.MinCool}}" }. The problem then though, is it's sending a string, and I get an API error from HomeAssistant as it's expecting a Float... (though I'm not sure why it's saying Float, as a "normal" whole number works fine)

---

<div class="post-metadata">

**Author:** ![simonk83](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/simonk83/32/7651_2.png) [@simonk83](https://discourse.nodered.org/u/simonk83)\
**Post date:** [20 April 2019 11:45 UTC](https://discourse.nodered.org/t/is-it-possible-to-use-a-flow-variable-as-a-value-in-a-json-string/10377/17 "2019-04-20T11:45:46Z")

</div>

Stupid question (another stupid question), do I need a template node in the chain to be able to call {{flow.MinCool}}?

---

<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 April 2019 11:47 UTC](https://discourse.nodered.org/t/is-it-possible-to-use-a-flow-variable-as-a-value-in-a-json-string/10377/18 "2019-04-20T11:47:50Z")

</div>

Try sending a prepared JSON string instead...

Function node...

```auto
msg.payload = '{ "value": ' + flow.get("MinCool") || 0 + ' }'
return msg;

```

Then use `{{payload}}` as the Data template.

---

<div class="post-metadata">

**Author:** ![simonk83](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/simonk83/32/7651_2.png) [@simonk83](https://discourse.nodered.org/u/simonk83)\
**Post date:** [20 April 2019 11:57 UTC](https://discourse.nodered.org/t/is-it-possible-to-use-a-flow-variable-as-a-value-in-a-json-string/10377/19 "2019-04-20T11:57:01Z")

</div>

> [@Steve-Mcl](#):
>
> ```auto
> msg.payload = '{ "value": ' + flow.get("MinCool") || 0 + ' }'
> return msg;
> 
> ```

Unfortunately no good. The weird part is that it just hates {{payload}} in the data field (says Bad String) despite it mentioning it in the Info...

---

<div class="post-metadata">

**Author:** ![simonk83](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/simonk83/32/7651_2.png) [@simonk83](https://discourse.nodered.org/u/simonk83)\
**Post date:** [20 April 2019 12:00 UTC](https://discourse.nodered.org/t/is-it-possible-to-use-a-flow-variable-as-a-value-in-a-json-string/10377/20 "2019-04-20T12:00:35Z")

</div>

See if I were trying to send a string I'd be fine: [https://community.home-assistant.io/t/call-service-node-data-inject-msg-payload/85573](https://community.home-assistant.io/t/call-service-node-data-inject-msg-payload/85573)

[Next page](https://discourse.nodered.org/t/is-it-possible-to-use-a-flow-variable-as-a-value-in-a-json-string/10377.md?page=2)
