How to get boolean from JSON node to LED node

Just started learning Node Red and I'm having great difficulty trying to turn an LED on/off based on a value in a JSON block.

I put the following in an INJECT node:
{"voltage": 30,"heateron":true}

The INJECT node connects to a JSON node which then connects to a GAUGE node.
My gauge works fine when I use msg.payload.voltage
However, I cannot do the same with an LED node, example msg.payload.heateron

So how do I get the boolean true/false value from the JSON node to turn on the LED ?

Welcome to the Forum.

The json node isn't necessary here, as msg.payload.voltage and msg.payload.heateron will handle the relevant values directly from the JavaScript object.

[{"id":"dcadfbbcf13f3955","type":"tab","label":"Flow 5","disabled":false,"info":"","env":[]},{"id":"10610ae81f65d607","type":"inject","z":"dcadfbbcf13f3955","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"voltage\":30,\"heateron\":true}","payloadType":"json","x":190,"y":240,"wires":[["e7d4dcf5245de1bf","66ed6e4c48f4aab8"]]},{"id":"e7d4dcf5245de1bf","type":"debug","z":"dcadfbbcf13f3955","name":"Isolate voltage","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload.voltage","targetType":"msg","statusVal":"","statusType":"auto","x":380,"y":200,"wires":[]},{"id":"66ed6e4c48f4aab8","type":"debug","z":"dcadfbbcf13f3955","name":"Isolate heateron","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload.heateron","targetType":"msg","statusVal":"","statusType":"auto","x":380,"y":260,"wires":[]}]

Thanks Dave, but I'm afraid I'm just left more confused. There does not appear to be anywhere in the LED node to use msg.payload.heateron so not sure how I can use it.
From what I've read, the LED node expects either a true or false input so somehow I need to read the heateron boolean value and get that "injected" into the LED node.

I'm not wanting to push out @dynamicdave and his help.

(No, I haven't looked at the code he posted)

Open the LED node and change it from just payload to payload.heateron

I think that may be a reason it isn't working.

(But as I haven't seen your code either, I can't be sure.)

The payload is a rather generic term. (Nothing wrong with using it though)

But usually the payload is a simple thing like true/false or stuff like that.
But here it is a json message which means payload doesn't exist in the usual format/strucutre.

It is now msg.payload.heateron and msg.payload.voltage

So you need to look at the more specific payload.heateron part

Use a change node to move msg.payload.heaton to msg.payload.

As a beginner you may benefit form watching the essentials videos, they have a great deal of information that will help you as a beginner with node-red.

By the way welcome to the forum.

If you look at the LED node you'll see it expects the true/false to be in msg.payload, so you need to move the property to the correct place - with a change node.

Note: This is the LED node for Dashboard-1 (D2 may be different).



Here's the above demo flow - just IMPORT it and have a play.

[{"id":"dcadfbbcf13f3955","type":"tab","label":"Flow 5","disabled":false,"info":"","env":[]},{"id":"10610ae81f65d607","type":"inject","z":"dcadfbbcf13f3955","name":"voltage = 3, heateron = true","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"voltage\":30,\"heateron\":true}","payloadType":"json","x":160,"y":200,"wires":[["4685871be5d4e500"]]},{"id":"4685871be5d4e500","type":"change","z":"dcadfbbcf13f3955","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"payload.heateron","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":420,"y":240,"wires":[["35c18b511d14be8e","8afcc9e196feecfe"]]},{"id":"8afcc9e196feecfe","type":"ui_led","z":"dcadfbbcf13f3955","order":2,"group":"29bb735723a8be77","width":0,"height":0,"label":"","labelPlacement":"left","labelAlignment":"left","colorForValue":[{"color":"#ff0000","value":"false","valueType":"bool"},{"color":"#008000","value":"true","valueType":"bool"}],"allowColorForValueInMessage":false,"shape":"circle","showGlow":true,"name":"","x":590,"y":240,"wires":[]},{"id":"35c18b511d14be8e","type":"debug","z":"dcadfbbcf13f3955","name":"LED state","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":600,"y":300,"wires":[]},{"id":"b95c753607250817","type":"inject","z":"dcadfbbcf13f3955","name":"voltage = 3, heateron = false","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"voltage\":30,\"heateron\":false}","payloadType":"json","x":160,"y":280,"wires":[["4685871be5d4e500"]]},{"id":"29bb735723a8be77","type":"ui_group","name":"dashboard_group","tab":"246a538c.3d84b4","order":null,"disp":true,"width":6},{"id":"246a538c.3d84b4","type":"ui_tab","name":"SMART Home - lighting","icon":"home","order":1,"disabled":false,"hidden":false}]

The problem is like I mentioned in my opening post:
"However, I cannot do the same with an LED node, example msg.payload.heateron"

There is no value box in the LED node where you can alter what the message is. I therefore cannot use msg.payload.heateron directly in the LED node.
The gauge node is different and no problem, as there is a value box and I change it from "msg.payload" to "msg.payload.voltage" and the gauge works as expected. Just can't do the same thing in the LED node.

Aha, that's it Dave, that worked a treat.
It's very sneaky in that change node that I don't change the value to a boolean but instead it gets changed to msg.payload.heateron.
Yes, looks like I've got a lot to learn.

Thanks to all of you for the kind help, and for the welcomes. Nice to be here.

Keith

Glad you have got it working.

The change node is changing the position of the property in the payload so it matches the LED node's requirements of where it expects the property to be. Happens a lot in Node-RED.

As @E1cid suggested - have a look at the Essential Videos - well worth the time and energy.

Thanks for the extra info Dave. Already started watching the videos.

Keith

I've just asked ChatGPT to create an illustration of how a JavaScript object is used in Node-RED.

That isn't a JSON object, it is a JavaScript object. JSON is always a string.

OK - point taken.
Here's another go with a clearer distinction between a JavaScript Object and a JSON string.

Perfect :slight_smile:

… maybe worth noting that some nodes can do the conversion for you, eg mqtt nodes.

Well spotted.
I just use MQTT-In and MQTT-Out nodes and accept they do the conversion for me.

Just to expand on @dceejay comments a bit further...

MQTT-OUT node
monday_mqtt-out
msg.payload is used as the payload of the published message. If it contains an Object it will be converted to a JSON string before being sent. If it contains a binary Buffer the message will be published as-is.

MQTT-IN node
Can be set to output the incoming message in different formats.


In my NR flows, I usually leave it as Auto Detect (parse JSON object, string or buffer).

It's great to see a Javascript object and a JSON object side by side. Not a great deal of difference.

Also good to know about the MQTT node being able to automatically detect a JSON object. I'm learning all this for monitoring/controlling ESP32 microcontrollers over the web, and I'll be using MQTT. Initially I thought I'd have to use a JSON node to convert the JSON object I'd be sending from a microcontroller, but that looks un-necessary now.

That is exactly like I do with my IoT students - using ESP32-S2-Mini and ESP32-C3-Super-Mini.

The number of times I've been caught out by that despite doing this as a career is wild. Definitely not just a you thing!

There is a big difference between them. In fact this

Is not actually a javascript object, it is a visualisation of a js object. The object itself is a complex arrangement of data in the computer, arranged in a way that is easy for the software to understand. The visualisation above is a representation of that data in a way that is easy for humans to understand.

Similarly this

Is not a string, it is a visualisation of a string. The string itself is a sequence of bytes in memory containing the text shown. The main purpose of a JSON string is to provide a simple version of the complex js object that can easily be moved about from one piece of s/w to another (via MQTT for example) or for storing, in a file for example.