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

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:

Use a function node...

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

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

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

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

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

What does the info say?

Hmm that one doesn't seem to work

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

{{flow.MinCool}}

Right, I mean I assumed it would be

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

But that just tells me Bad String

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.

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

No. That is JSONata expression. Not JSON.

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

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 who sounds like he's done what I need with a template, but I still don't quite get it...

Looking at your screen shot,

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

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)

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

Try sending a prepared JSON string instead...

Function node...

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

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

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

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