JSON: how to generate random number?

I need to inject random a single value in a json. I am trying


msg.payload = {
    "temperature" : jsonata("Math.random() * 20 + 10").evaluate(),
}
msg.topic = "device/ht";
return msg;

but temperature is empty.
What is the right syntax?

I am using node-red 3.1.7

Try this in your function node...

msg.payload = {
    "temperature": Math.random() * 20 + 10,
};
msg.topic = "device/ht";
return msg;

temp_random

1 Like

Alternative using jsonata and $random() inside an inject node

[{"id":"59fd02ccc1d8efb4","type":"inject","z":"d73d76db3df96ba2","name":"","props":[{"p":"temperature","v":"$random() * 20 + 10","vt":"jsonata"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"\"device/ht\"","x":2821,"y":1736,"wires":[[]]}]
1 Like

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