Pick just one value from a MQTT message

Probaly simple for others but I got stuck.
I have som esp8266 with tasmota 7.1 and lux meter.
I want just the value to be sent to dashboard and MYSQL.
The message looks like this:
{"Time":"2019-12-10T15:30:16","TSL2561":{"Illuminance":56.190}}

How can I get Node-Red just to send the Illuminance value to next node thats a Dashboard or "insert into mysql" node?

Hi @Quart666

This page in the docs should give you some tips on how to work with messages, understand their structure and access the different parts of it: https://nodered.org/docs/user-guide/messages

I have read that page but still dont get it :frowning:
I now do this but still miss the target
image

@Quart666 so the property you are interested in is msg.payload.TSL2561.Illuminance

You could add a Change node to set msg.payload to the value of msg.payload.TSL2561.Illuminance. Most Dashboard nodes will use msg.payload by default.

1 Like

Humm
I am a newbie.
Is this right?

[{"id":"8dbf67eb.fd5df8","type":"mqtt in","z":"76eae6e0.698b98","name":"","topic":"tele/tasmotatest/SENSOR","qos":"2","datatype":"json","broker":"fbf559eb.beb7d8","x":150,"y":1980,"wires":[["d54d210c.d2c2e"]]},{"id":"729bf046.a8b45","type":"debug","z":"76eae6e0.698b98","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":950,"y":1980,"wires":[]},{"id":"d54d210c.d2c2e","type":"change","z":"76eae6e0.698b98","name":"","rules":[{"t":"set","p":"payload.TSL2561.Illuminance","pt":"msg","to":"","tot":"num"}],"action":"","property":"","from":"","to":"","reg":false,"x":530,"y":1980,"wires":[["729bf046.a8b45"]]},{"id":"fbf559eb.beb7d8","type":"mqtt-broker","z":"","name":"Mosquitto MQTT Server","broker":"192.168.1.12","port":"1883","clientid":"node-red","usetls":false,"compatmode":true,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","closeTopic":"","closeQos":"0","closePayload":"","willTopic":"","willQos":"0","willPayload":""}]

The first entry here is what you have got. The second shows what I suggest you should try.

You want to set the value of msg.payload to the value of msg.payload.TSL.......

THanks for your input.
But now I got this
tele/tasmotatest/SENSOR : msg.payload : string[62]

"{"Time":"2019-12-11T08:24:56","TSL2561":{"Illuminance":6.524}}"

Did you change anything?

Earlier you had a properly parsed object. You now have a JSON string. The link I sent in my first reply explains the difference and how to convert one to the other.

Probaly do this to complex but now it works..

@Quart666 you don't need to do that.

The MQTT node is passing on a JSON String. Use a JSON node to convert it to a JavaScript Object where you can access its properties as you did in the earlier replies.

All of this is explained in the docs I linked to.

So first the json and then the change node?

At this point, I suggest you just try it and use Debug nodes along the flow to understand what you have at each stage.

Make sure your MQTT node produces JSON before feeding it into the change node

image

Finally..

As Simon pointed out, you don't need the {json} node, in your flow, if you select the 'parsed JSON object' option in the MQTT-In node.

Set the mqtt node output to "a parsed JSON object",
Remove the json node,
Remove the change node
Enter in the gauge node: msg.payload.TSL2561.Illuminance

The beauty of node-red.

1 Like

Thanks, I must be missing something
image

In your Change node, make sure you have msg selected in the input where you have entered payload.TSL2.....

From your screenshot, it suggests you currently have that set to String rather than msg - so you are setting it to the literal string payload.TLS2... rather than looking up the value from the message object.

Look back at the screenshot of the Change node I provided and compare it to what you have got.

@Quart666 wants to get the value into an SQL database as well as a gauge, so I think its best to try and stick with @knolleary method and extract the value as early on as possible

Final version

2 Likes