Insert Dynamic Variable?

So this is what I have from running examples, but I can’t figure out how to insert a “Dynamic Variable”

{
“metrics”: [
{
“name”: “Temp”,
“value”: 68.56 (I want to replace the value with a dynamic variable (payload.main.temp)
},
{
“name”: “Humidity”,
“value”: 78 (The same for here (payload.main.humidity)
}
]
}

If you get that into a javascript object (defined as ‘o’ below), you would change the values like this:

o.metrics[0].value = payload.main.temp;
o.metrics[1].value = payload.main.humidity;

I’m a beginner for javascript? Can you provide a more detailed example?

I still can’t figure this out, what am I missing? I have tried everything that I have read.

The inject node is the start of a flow - it creates an initial msg object and you can initialize it like you are doing for testing other nodes. It doesn’t have access to any variables that were created in other flows. (There is no input to an inject node). Node-Red works on passing (and copying) a msg object from node to node. That is a fundamental part of Node-Red, so please focus on learning more about that.

You have given us very little details of what you are trying to do, so I am going to make some assumptions.

From your http request, I assume you are getting a value from that - it looks like the temperature. From there you need to connect the output of your http request to a function node and connect the output of the function node to your TagData node.

The function node is where you will work on converting your data into the object structure that your TagData node is expecting.

The function node would look something like this (this is untested):

var o = {
  "metrics": [
    {
      "name": "Temp",
      "value": msg.payload.main.temp
    }
  ]
};

msg.payload = o;

return msg;

I have to agree with @philip here, and will also make some assumptions.

You can make your JSON payload as Philip has shown, or more compact like this:

msg.payload = {
            Temp: msg.payload.main.temp,
            Humidity: msg.payload.main.humidity,
            Pressure: msg.payload.main.pressure,
            Condition: msg.payload.weather.id,
            WindSp: msg.payload.wind.speed,
            WindGust: msg.payload.wind.gust,
            WindDeg: msg.payload.wind.deg
}
return msg;```

That won’t match the object format that he has in his inject node though. I am assuming that the TagData node is ridgid in what it expects.

Assumptions were made.
I agree with you, but we don’t know for sure what the rest of the flow is doing / looking like.

I want to thank all for your reply. After getting help from Opto-22 I was able to get my first flow working as I needed.

I am a beginner and I have been watching videos and reading what I can.

I will work toward the suggestions and once again thanks for your help.

2 Likes