Pick just one value from a MQTT message

maybee you got this already figured out but the quesyion seemed simple.

you do not need change nodes, just a function

{"id":"405c9277.fcf69c","type":"inject","z":"ff5d1e85.d8ebe","name":"","topic":"","payload":"{"Time":"2019-12-10T15:30:16","TSL2561":{"Illuminance":56.190}}","payloadType":"json","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":370,"y":240,"wires":[["85d55443.88abc8"]]},{"id":"e40ed4b6.3a00c8","type":"debug","z":"ff5d1e85.d8ebe","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":730,"y":240,"wires":},{"id":"85d55443.88abc8","type":"function","z":"ff5d1e85.d8ebe","name":"","func":"var Illuminance = msg.payload.TSL2561.Illuminance\nmsg.payload = Illuminance\nreturn msg;","outputs":1,"noerr":0,"x":510,"y":240,"wires":[["e40ed4b6.3a00c8"]]}

1 Like

Or, you do not need a function node, if you can use a change node...
Why write code if you don't have to?

Because sometimes the difference between a function node and a change node is the difference between writing JavaScript and writing JSONata :slightly_smiling_face:

1 Like

agreed if the choice is between function node versus change node
most of the times you still need something more and you can do that in a function node instead of creating another node.

e.g. in this example we were talking about one variable.
what if you had 100, you can still do it with one function node, give it multiple outputs.

second example: function nodes can work with variables while change nodes can not

eg. in a change node set variable to flow.device.{{variable}}

the idea of Node-red is to keep things simple so creating lot's of nodes is not the way to go but creating 1 function node which can do it all neither

i am still a beginner when it comes to node-red but figured out a lot of smart functionality, just by trying out things in the code(it is Javascript and you can find a lot on google)

2 Likes

Tried to import that code, didnt work. Not a json array...

[{"id":"405c9277.fcf69c","type":"inject","z":"ff5d1e85.d8ebe","name":"","topic":"","payload":"{"Time":"2019-12-10T15:30:16","TSL2561":{"Illuminance":56.190}}","payloadType":"json","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":370,"y":240,"wires":[["85d55443.88abc8"]]},{"id":"e40ed4b6.3a00c8","type":"debug","z":"ff5d1e85.d8ebe","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":730,"y":240,"wires":},{"id":"85d55443.88abc8","type":"function","z":"ff5d1e85.d8ebe","name":"","func":"var Illuminance = msg.payload.TSL2561.Illuminance\nmsg.payload = Illuminance\nreturn msg;","outputs":1,"noerr":0,"x":510,"y":240,"wires":[["e40ed4b6.3a00c8"]]}]

to give you an idea how to get variables from json strings