Node Red function

298 views
Skip to first unread message

Sporty Morty

unread,
Feb 13, 2017, 1:15:03 PM2/13/17
to SonoffUsers
Hi
has any one have an example of a function in node red to parse DHT temperature and humidity info out of the JSON input and then display it in a graph 
 i do not quite have the programming skill to parse the JSON input

Jay S

unread,
Feb 13, 2017, 1:16:14 PM2/13/17
to Sporty Morty, SonoffUsers
Hi sporty,

I do. I'll try and post an example tonight.

Jay

--
You received this message because you are subscribed to the Google Groups "SonoffUsers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sonoffusers+unsubscribe@googlegroups.com.
Visit this group at https://groups.google.com/group/sonoffusers.
To view this discussion on the web, visit https://groups.google.com/d/msgid/sonoffusers/274ab3fd-7f00-4400-bef0-d7e83ed2f1c3%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Jay S

unread,
Feb 13, 2017, 1:37:38 PM2/13/17
to SonoffUsers
[{"id":"ed19c70c.15e8c8","type":"mqtt in","z":"83556bd8.575568","name":"","topic":"tele/xxxxx/TELEMETRY","qos":"2","broker":"","x":300,"y":500,"wires":[["a90891e6.fa465","cb2040ad.20c9c"]]},{"id":"a90891e6.fa465","type":"function","z":"83556bd8.575568","name":"","func":"msg.payload=JSON.parse(msg.payload);\nmsg.payload=msg.payload.Temperature;\nmsg.topic = \"Temperature\";\ntry {msg.payload;\nnode.send (msg);\nnode.status({fill:\"blue\",shape:\"dot\",text:\"Status: \"+ msg.payload + \" Degrees Centigrade\"});\n}\ncatch(e) {}\n","outputs":1,"noerr":0,"x":550,"y":500,"wires":[[]]},{"id":"cb2040ad.20c9c","type":"json","z":"83556bd8.575568","name":"","x":500,"y":580,"wires":[["a9080b88.21a258"]]},{"id":"a9080b88.21a258","type":"debug","z":"83556bd8.575568","name":"","active":true,"console":"false","complete":"false","x":660,"y":580,"wires":[]}]

If you give that a go, just setup your MQTT details, change xxxx to the sonoff name and then attach a graph at the output.
you can feed more than one temperature reading into the graph, just change the msg.topic.
If you want to use it for humidity, change msg.payload.Temperature to msg.payload,Humidity. Also change the topic.
You can also have multiple functions attached to the one MQTT node to split out all of the data

The json node and msg.debug are not needed for this to work, but you can view the message output to see the json structure. Make sure you are on node-red v0.16 or above so you can use the debug node to view the structure easily.

Sporty Morty

unread,
Feb 13, 2017, 1:59:19 PM2/13/17
to SonoffUsers
Thanx Jay I am going to give this a try

Sporty Morty

unread,
Feb 13, 2017, 2:57:33 PM2/13/17
to SonoffUsers
Hi 
i am getting an error with the function status undefined degrees centigrade under the function node.... i have should of mentioned that i am using Tasmota 3.9.14 i changed mqtt details to tele/sonoff/SENSOR   i am getting debugging info

Sporty Morty

unread,
Feb 13, 2017, 3:32:54 PM2/13/17
to SonoffUsers
ok i got it.... had to change msg.payload.Temperature to msg.payload.DHT.Temperature everything is working great now

Thanx Jay this would of taken me ages to figger out

Jay S

unread,
Feb 13, 2017, 4:29:25 PM2/13/17
to SonoffUsers
Glad you worked it out. Forgot it has the extra part to denote the type of sensor being used.
You'll find that JSON is actually very easy once you get into it. Even easier with the recent updates to node red.
Have you been using node red long?

Julian Knight

unread,
Feb 13, 2017, 6:02:32 PM2/13/17
to SonoffUsers
I recently did a 5-minute write up on accessing parts of the msg object in the Node-RED group - you might find it useful.


Just added a suggestion to the new cookbook as well as this kind of thing comes up a lot now that Node-RED has lots of new users, many of whom know little JavaScript (which is great because that is one of the reasons for Node-RED).

Sporty Morty

unread,
Feb 13, 2017, 7:16:20 PM2/13/17
to SonoffUsers
I have been using Node Red for about a month now and is working out great...it made my day with the function node and the JSON input
Reply all
Reply to author
Forward
0 new messages