using and parsing JSON objects msg.payload

750 views
Skip to first unread message

Don Biederman

unread,
Mar 26, 2017, 10:38:23 AM3/26/17
to Node-RED
Hi
I'm using a NodeMcu to monitor a few sump pumps at my home sending this using MQTT. The I'm using the topic of "home/sump_pump" I'm  publishing the following JSON data to the broker as:

{"level":"normal","dcpwr":"ok","acpwr":"ok","run":"stop"} . 

My question how do I parse the msg.payload to retrieve the values of the 4 sensor in Node-Red and display the status and send and email once if level = "high"? 
Thank you
Don Biederman 

Dave C-J

unread,
Mar 26, 2017, 11:01:16 AM3/26/17
to node...@googlegroups.com
Data received from an MQTT node is typically in String format - wheras we need JSON to be able to parse it easily.. - so feed the output of teh MQTT node into a JSON node... then you can get to your properties in either a function node or switch node or... whatever.
They should be then available as   msg.payload.level   msg.payload.run  etc...

Brian Orpin

unread,
Mar 26, 2017, 12:05:08 PM3/26/17
to Node-RED
Try this;

[{"id":"bc3f9e17.11af4","type":"inject","z":"4590d4a5.b2b604","name":"","topic":"","payload":"{\"level\":\"normal\",\"dcpwr\":\"ok\",\"acpwr\":\"ok\",\"run\":\"stop\"}","payloadType":"str","repeat":"","crontab":"","once":false,"x":218,"y":133,"wires":[["85ffbdca.b8f52"]]},{"id":"85ffbdca.b8f52","type":"json","z":"4590d4a5.b2b604","name":"","x":366,"y":133,"wires":[["8ee5ac04.0282b","c5706cf6.067f2"]]},{"id":"8ee5ac04.0282b","type":"debug","z":"4590d4a5.b2b604","name":"","active":true,"console":"false","complete":"false","x":516,"y":132,"wires":[]},{"id":"c5706cf6.067f2","type":"debug","z":"4590d4a5.b2b604","name":"","active":true,"console":"false","complete":"payload.level","x":528,"y":183,"wires":[]}]

Don Biederman

unread,
Mar 26, 2017, 12:20:26 PM3/26/17
to Node-RED
thanks so much that helps out so much! I didnt have a JSON node and was getting undefined error in debug.  I'm learning about this love how Node-Red works I want to make a UI and have it email me. Can you give a lead on how to keep it from emailing me each time it receives a message. Say email me when "level" = "high" and email me once again when it returns back to "normal".  

Don Biederman

unread,
Mar 26, 2017, 12:21:18 PM3/26/17
to Node-RED
thank I see msg.payload.acpwr will give me the value.

Dave C-J

unread,
Mar 26, 2017, 12:40:46 PM3/26/17
to node...@googlegroups.com
look at the rbe node...

Brian Orpin

unread,
Mar 27, 2017, 2:49:16 AM3/27/17
to Node-RED
And now I learn something as well. Excellent.

On Sunday, 26 March 2017 17:40:46 UTC+1, Dave C-J wrote:
look at the rbe node...
Reply all
Reply to author
Forward
0 new messages