How to assign a value from msg.payload

4,726 views
Skip to first unread message

Ziomek Ziomkowski

unread,
Dec 30, 2017, 2:42:34 PM12/30/17
to Node-RED
I am downloading data from Domoticz via MQTT How to assign a value from msg.payload eg svalue to a variable and use it in another function

David Caparrós

unread,
Dec 30, 2017, 2:45:15 PM12/30/17
to Node-RED
You can write on a function node to capture the msg.payload to a global variable:

global.set("variable-name",msg.payload)
return msg;


Afterward you can use another function to pull the value as msg.payload with:

var obtaindata = global.get ("variable-name")
msg.payload = obtaindata;
return msg;


Or use a the change node from the palette.

Regards

Ziomek Ziomkowski

unread,
Dec 30, 2017, 2:53:54 PM12/30/17
to Node-RED

and how to draw only one value, e.g. temperature

Zenofmud

unread,
Dec 30, 2017, 3:20:46 PM12/30/17
to node...@googlegroups.com
Without knowing what the MQTT payload is no one can tell you. Attach a debug node to the MQTT in and display the complete message, that should tell you what part you need 

Paul
--
http://nodered.org
 
Join us on Slack to continue the conversation: http://nodered.org/slack
---
You received this message because you are subscribed to the Google Groups "Node-RED" group.
To unsubscribe from this group and stop receiving emails from it, send an email to node-red+u...@googlegroups.com.
To post to this group, send email to node...@googlegroups.com.
Visit this group at https://groups.google.com/group/node-red.
To view this discussion on the web, visit https://groups.google.com/d/msgid/node-red/5c1cc0c6-bfbf-4566-a10a-96bf6fc74946%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Ziomek Ziomkowski

unread,
Dec 30, 2017, 3:56:30 PM12/30/17
to Node-RED
{
   "Battery" : 255,
   "RSSI" : 12,
   "description" : "",
   "dtype" : "Temp",
   "id" : "82002",
   "idx" : 2,
   "meterType" : "Energy",
   "name" : "Piec CO",
   "nvalue" : 0,
   "stype" : "LaCrosse TX3",
   "svalue1" : "48.13",
   "unit" : 1
}
All values have names such as idx, svalue1 etc.
And I want a number from svalue, i.e., 48.13, write to a variable.

Mark Setrem

unread,
Dec 30, 2017, 4:05:27 PM12/30/17
to Node-RED
In the latest versions of node-red you can click on a value in a debug window and it will display the path to the value.

See here:  https://nodered.org/blog/2017/06/28/version-0-17-released  under "Even better Debug panel"

Ziomek Ziomkowski

unread,
Dec 30, 2017, 4:11:14 PM12/30/17
to Node-RED

ok but how to do it to assign the selected value to a variable


W dniu sobota, 30 grudnia 2017 20:42:34 UTC+1 użytkownik Ziomek Ziomkowski napisał:

Ziomek Ziomkowski

unread,
Dec 30, 2017, 4:17:10 PM12/30/17
to Node-RED
var a = 0;
a = msg.svalue1;
return msg;

is correct ?

W dniu sobota, 30 grudnia 2017 20:42:34 UTC+1 użytkownik Ziomek Ziomkowski napisał:

Mark Setrem

unread,
Dec 30, 2017, 4:21:48 PM12/30/17
to Node-RED
That would set a to what you want but then you send on the msg object which doesn't include variable a

Best bet would be to take a look at one of the online javascript tutorials and look at variables objects and arrays.
As to get the most out of node-red you really need to understand how all 3 are used in javascript.


But you can either set the value with the change node or within a function node

Ziomek Ziomkowski

unread,
Dec 30, 2017, 4:54:13 PM12/30/17
to Node-RED
trying with arrays but still something does not work

var a = 0;
a = msg.payload[11][0];

return msg,a;

Debug:
"Function tried to send a message of type string"

W dniu sobota, 30 grudnia 2017 20:42:34 UTC+1 użytkownik Ziomek Ziomkowski napisał:

Colin Law

unread,
Dec 30, 2017, 5:00:07 PM12/30/17
to node...@googlegroups.com
msg.payload = msg.payload[11][0]
return msg

You must always return an object, usually with a value in the payload. You cannot return a simple value like a string, as the error message says.

Colin

--
http://nodered.org
 
Join us on Slack to continue the conversation: http://nodered.org/slack
---
You received this message because you are subscribed to the Google Groups "Node-RED" group.
To unsubscribe from this group and stop receiving emails from it, send an email to node-red+unsubscribe@googlegroups.com.

To post to this group, send email to node...@googlegroups.com.
Visit this group at https://groups.google.com/group/node-red.

Ziomek Ziomkowski

unread,
Dec 30, 2017, 5:15:09 PM12/30/17
to Node-RED
ok works but only displays 1 letter.

30/12/2017, 23: 12: 26node: 83ef7132.55963
domoticz / out: msg.payload: string [1] "y"

This is the table from which I want to get the temperature out
{
    "Battery": 255,
    "RSSI": 12,
    "description": "",
    "dtype": "Temp",
    "id": "82002",
    "idx": 2,
    "meterType": "Energy",
    "name": "CH furnace",
    "nvalue": 0,
    "stype": "LaCrosse TX3",
    "svalue1": "48.13",
    "unit": 1
}

how to get the whole value, e.g., 48.13?

W dniu sobota, 30 grudnia 2017 20:42:34 UTC+1 użytkownik Ziomek Ziomkowski napisał:

Colin Law

unread,
Dec 30, 2017, 5:22:21 PM12/30/17
to node...@googlegroups.com
You said you had an array.  *Please* follow the advice you have been given to work through some javascript tutorials. However, to keep you going, If that object is in msg.payload then you want
msg.payload = msg.payload.sValue1
if the object you have shown is the complete message then you want
msg.payload = msg.sValue1

If you don't understand what I have written then head for the tutorials. You will save yourself and us considerable time in the long run.

Colin


--
http://nodered.org
 
Join us on Slack to continue the conversation: http://nodered.org/slack
---
You received this message because you are subscribed to the Google Groups "Node-RED" group.
To unsubscribe from this group and stop receiving emails from it, send an email to node-red+unsubscribe@googlegroups.com.
To post to this group, send email to node...@googlegroups.com.
Visit this group at https://groups.google.com/group/node-red.

Nick O'Leary

unread,
Dec 30, 2017, 5:22:30 PM12/30/17
to Node-RED Mailing List
Hi,

lets take a couple of steps back.

The MQTT node is emitting msg.payload containing a String - specifically, the JSON string you've shared.

Before you can start referencing elements within that JSON you need to parse it to a JavaScript Object.

So add a JSON node between your MQTT node and Function node.

You can then access each element of the object as normal javascript properties. For example:

    msg.payload = msg.payload.svalue1;
    return msg;



Nick



On 30 December 2017 at 22:15, Ziomek Ziomkowski <kwas...@gmail.com> wrote:

--
http://nodered.org
 
Join us on Slack to continue the conversation: http://nodered.org/slack
---
You received this message because you are subscribed to the Google Groups "Node-RED" group.
To unsubscribe from this group and stop receiving emails from it, send an email to node-red+unsubscribe@googlegroups.com.
To post to this group, send email to node...@googlegroups.com.
Visit this group at https://groups.google.com/group/node-red.

Ziomek Ziomkowski

unread,
Dec 30, 2017, 5:32:53 PM12/30/17
to Node-RED
It works, thank you very much. I have one more question is the ability to filter to display only svalue1 or temperature only from the device idx 2?


W dniu sobota, 30 grudnia 2017 20:42:34 UTC+1 użytkownik Ziomek Ziomkowski napisał:

Nick O'Leary

unread,
Dec 30, 2017, 5:39:30 PM12/30/17
to Node-RED Mailing List
the ability to filter to display only svalue1 or temperature only from the device idx 2?

Yes, you can add some logic in your flow so that only messages with msg.payload.idx === 2 pass through.

For example, the Switch node lets you set rules that direct messages to different outputs based on the value of a message property.

Or you could add a standard if statement to your Function - and 'return null' if msg.payload.idx is not 2.

Nick



--
http://nodered.org
 
Join us on Slack to continue the conversation: http://nodered.org/slack
---
You received this message because you are subscribed to the Google Groups "Node-RED" group.
To unsubscribe from this group and stop receiving emails from it, send an email to node-red+unsubscribe@googlegroups.com.
To post to this group, send email to node...@googlegroups.com.
Visit this group at https://groups.google.com/group/node-red.

Ziomek Ziomkowski

unread,
Dec 30, 2017, 6:20:51 PM12/30/17
to Node-RED
Almost everything works, thank you :)
One more question I have an OLED display on esp8266 from esp_Easy and I want to display on it the temperature that I extracted, for example, into a variable, how to do it?
http: // <ESP IP address> / control? cmd = oled, <row>, <col>, <text>
this is a template how to display something on the display but I do not know how to adopt it in node red.

W dniu sobota, 30 grudnia 2017 20:42:34 UTC+1 użytkownik Ziomek Ziomkowski napisał:
Reply all
Reply to author
Forward
0 new messages