# Getting the name of variable as value

**URL:** https://discourse.nodered.org/t/getting-the-name-of-variable-as-value/29830
**Category:** Dashboard
**Created:** [10 July 2020 07:53 UTC](https://discourse.nodered.org/t/getting-the-name-of-variable-as-value/29830 "2020-07-10T07:53:38Z")
**Posts on this page:** 14
**Page:** 1

<div class="post-metadata">

### Author: ![Chorum](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/chorum/32/47644_2.png) [@Chorum](https://discourse.nodered.org/u/Chorum)
#### Post date: [10 July 2020 07:53 UTC](https://discourse.nodered.org/t/getting-the-name-of-variable-as-value/29830/1 "2020-07-10T07:53:38Z")

</div>

Hello There,

i want to extract the Name of a variable and use it as a string.

For instance i have multiple sets of temperatures. Every set has different names for the temperatures and the names could Change.  
So i would like to get the name from the payload to title switches and for names in Charts.

How could i do that?

Greetings  
Chorum

---

<div class="post-metadata">

### Author: ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)
#### Post date: [10 July 2020 08:13 UTC](https://discourse.nodered.org/t/getting-the-name-of-variable-as-value/29830/2 "2020-07-10T08:13:05Z")

</div>

If you select the chart node, click the "i" or book button, you will find information on how to set the name of the chart dynamically using the mustache syntax.

> The label can also be set by a message property by setting the field to the name of the property, for example `{{msg.topic}}` .

---

<div class="post-metadata">

### Author: ![Chorum](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/chorum/32/47644_2.png) [@Chorum](https://discourse.nodered.org/u/Chorum)
#### Post date: [10 July 2020 08:16 UTC](https://discourse.nodered.org/t/getting-the-name-of-variable-as-value/29830/3 "2020-07-10T08:16:44Z")

</div>

Oh thats a missunderstanding.  
i would like to know how i get the Name of the variable.  
before i can set the names in the Chart, i Need to get them first 🙂

---

<div class="post-metadata">

### Author: ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)
#### Post date: [10 July 2020 08:22 UTC](https://discourse.nodered.org/t/getting-the-name-of-variable-as-value/29830/4 "2020-07-10T08:22:38Z")

</div>

> [@Chorum](#):
>
> i would like to know how i get the Name of the variable.

What variable ? Perhaps you should post an example flow and/or debug output.

---

<div class="post-metadata">

### Author: ![Chorum](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/chorum/32/47644_2.png) [@Chorum](https://discourse.nodered.org/u/Chorum)
#### Post date: [10 July 2020 08:26 UTC](https://discourse.nodered.org/t/getting-the-name-of-variable-as-value/29830/5 "2020-07-10T08:26:34Z")

</div>

I want the Name of the variable where the value is stored.

if i have temp1= 23 in the payload i want to get the Name "temp1" and put it in the payload as a new value temp1name = "temp1"

i think now its understandable 🙂

---

<div class="post-metadata">

### Author: ![molesworth](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/molesworth/32/11748_2.png) [@molesworth](https://discourse.nodered.org/u/molesworth)
#### Post date: [10 July 2020 08:28 UTC](https://discourse.nodered.org/t/getting-the-name-of-variable-as-value/29830/6 "2020-07-10T08:28:58Z")

</div>

> [@Chorum](#):
>
> if i have temp1= 23 in the payload i want to get the Name "temp1" and put it in the payload as a new value temp1name = "temp1"

When creating the message, would it be possible to add a field e.g. "varname", and set it to the name of the field you want? You can then extract it as "msg.varname".

---

<div class="post-metadata">

### Author: ![Chorum](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/chorum/32/47644_2.png) [@Chorum](https://discourse.nodered.org/u/Chorum)
#### Post date: [10 July 2020 08:34 UTC](https://discourse.nodered.org/t/getting-the-name-of-variable-as-value/29830/7 "2020-07-10T08:34:50Z")

</div>

Yea, good idea.  
that would be possible and will my solution if i dont find an other way.  
The messagesetup will be made by different persons and i want to make it as simple and as less work as possible for them.

There will be around 50 values that have to be changed every some weeks.

If i could make it possible in node red it would be better.

---

<div class="post-metadata">

### Author: ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)
#### Post date: [10 July 2020 08:37 UTC](https://discourse.nodered.org/t/getting-the-name-of-variable-as-value/29830/8 "2020-07-10T08:37:17Z")

</div>

With a function node and `Object.keys` you can get the key value and then set it to a new one

```auto
// input = {payload:{temp1:23}}

m = msg.payload
key = Object.keys(m)[0]
val = m[key]

return {payload:{[key+"_name"]:val, name:key}};

// output = {"payload":{temp1_name":23,"name":"temp1"}}

```

So now there are 2 ways - creating a new key name and the name itself in a new property, whatever suits your needs.

---

<div class="post-metadata">

### Author: ![Chorum](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/chorum/32/47644_2.png) [@Chorum](https://discourse.nodered.org/u/Chorum)
#### Post date: [10 July 2020 08:44 UTC](https://discourse.nodered.org/t/getting-the-name-of-variable-as-value/29830/9 "2020-07-10T08:44:04Z")

</div>

Oh that Looks good!  
i will work on that in around a hour.  
I hope i understand that 🙂

---

<div class="post-metadata">

### Author: ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)
#### Post date: [11 July 2020 07:37 UTC](https://discourse.nodered.org/t/getting-the-name-of-variable-as-value/29830/10 "2020-07-11T07:37:55Z")

</div>

> [@Chorum](#):
>
> i have temp1= 23

Do you have control over where that string is generated? If so then it would be better to encode it as a json string. `{temp: 23}` then in node red feed that through a JSON node to convert it to a js object. If there is more than just temp1 then the whole set could be json encoded.

---

<div class="post-metadata">

### Author: ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)
#### Post date: [11 July 2020 07:49 UTC](https://discourse.nodered.org/t/getting-the-name-of-variable-as-value/29830/11 "2020-07-11T07:49:20Z")

</div>

It would need to be {“temp”:23} to be valid Json.

---

<div class="post-metadata">

### Author: ![Chorum](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/chorum/32/47644_2.png) [@Chorum](https://discourse.nodered.org/u/Chorum)
#### Post date: [11 July 2020 11:24 UTC](https://discourse.nodered.org/t/getting-the-name-of-variable-as-value/29830/12 "2020-07-11T11:24:51Z")

</div>

Next time i can work on that is on monday.  
i will test and report.

Thanks for the help!

---

<div class="post-metadata">

### Author: ![Chorum](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/chorum/32/47644_2.png) [@Chorum](https://discourse.nodered.org/u/Chorum)
#### Post date: [15 July 2020 06:03 UTC](https://discourse.nodered.org/t/getting-the-name-of-variable-as-value/29830/13 "2020-07-15T06:03:47Z")

</div>

For the Moment i will send the Name of the value with the value .  
I will rethink that later.

Thank you for your help!  
Chorum

---

<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: [14 August 2020 06:18 UTC](https://discourse.nodered.org/t/getting-the-name-of-variable-as-value/29830/14 "2020-08-14T06:18:22Z")

</div>

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