# How do i insert a variable in a function - in the string

**URL:** <https://discourse.nodered.org/t/how-do-i-insert-a-variable-in-a-function-in-the-string/89949>\
**Category:** General\
**Created:** [2 August 2024 09:49 UTC](https://discourse.nodered.org/t/how-do-i-insert-a-variable-in-a-function-in-the-string/89949 "2024-08-02T09:49:42Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![PhilipR](https://avatars.discourse-cdn.com/v4/letter/p/f6c823/32.png) [@PhilipR](https://discourse.nodered.org/u/PhilipR)\
**Post date:** [2 August 2024 09:49 UTC](https://discourse.nodered.org/t/how-do-i-insert-a-variable-in-a-function-in-the-string/89949/1 "2024-08-02T09:49:42Z")

</div>

In the function node I have a line that looks like this:

```auto
msg.payload.Rad1W = msg.payload.data.devices_status.ec62608de1cc["switch:0"].apower

```

Now... I would like to use the variables from the sub flow to replace the places 'ec62608de1cc' & '["switch:0"].'  
How do insert the variables into those places so It can look something like this:  
msg.payload.Rad1W = msg.payload.data.devices\_status. **Variable1Variable2**.apower

---

<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:** [2 August 2024 10:04 UTC](https://discourse.nodered.org/t/how-do-i-insert-a-variable-in-a-function-in-the-string/89949/2 "2024-08-02T10:04:39Z")

</div>

> [@PhilipR](#):
>
> I would like to use the variables from the sub flow to replace the places 'ec62608de1cc' & '["switch:0"].'

```auto
const id = env.get('id') // e.g. ec62608de1cc
const prop = env.get('prop') // e.g. switch:0
msg.payload.Rad1W = msg.payload.data.devices_status[id][prop].apower
return msg

```

NOTE: I would recommend you break this down and add null/undefined guards e.g

```auto
const id = env.get('id') // e.g. ec62608de1cc
if (!id) {
    node.error('id is not set')
    return null
}
const prop = env.get('prop') // e.g. switch:0
if (!prop) {
    node.error('prop is not set')
    return null
}
const stat = msg.payload?.data?.devices_status?.[id]
if (!stat) {
    node.error('device status for id "' + id + '" was not found in the msg')
    return null
}
const statProp = stat[prop]
if (!statProp) {
    node.error('device status property "' + prop + '" for id "' + id + '" was not found in the msg')
    return null
}

msg.payload.Rad1W = statProp.apower
return msg

```

↑ 100% untested - but should get you close ↑

---

<div class="post-metadata">

**Author:** ![PhilipR](https://avatars.discourse-cdn.com/v4/letter/p/f6c823/32.png) [@PhilipR](https://discourse.nodered.org/u/PhilipR)\
**Post date:** [2 August 2024 22:25 UTC](https://discourse.nodered.org/t/how-do-i-insert-a-variable-in-a-function-in-the-string/89949/3 "2024-08-02T22:25:35Z")

</div>

Awesome, with some modifications it did exactly what I needed!

---

<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:** [16 August 2024 22:25 UTC](https://discourse.nodered.org/t/how-do-i-insert-a-variable-in-a-function-in-the-string/89949/4 "2024-08-16T22:25:38Z")

</div>

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