Getting the name of variable as value

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

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

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 :slight_smile:

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

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 :slight_smile:

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

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.

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

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

3 Likes

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

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.

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

2 Likes

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

Thanks for the help!

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

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