Little stuck with JSON syntax using node.send in a function node

From a ZigBee extension lead I get states for each of the sockets like msg.payload.state_l1. I'm using node.send to filter out the rest so the button node will work.

msg.payload.state_l1 = "ON";
node.send([{payload:msg.payload.state_l1}, null, null]);

This works, but it outputs as msg.payload and I can't get the JSON correct to output msg.payload.state_l1. So I want to change the payload: bit to something like payload.state_l1: but so it works.

Any help much appreciated!

You may need to use the JSON node.

Not sure, but it may help.

Maybe
node.send([{payload:msg.payload},null,null]);

Not exactly answering your question, but....
Wouldn't this be an opportunity to simplify the format of the zigbee msg, and instead of passing another JSON object (where you would then need to extract the value again later), move the value to payload, and add a msg.topic such as 'zigbee3' or something clearer like 'lounge_lighting', so the it can be easily identified as it runs through the flow.

node.send([{ payload: msg.payload.state_l1, topic:'zigbee3' }, null, null]);

Output -

{"payload":56,"topic":"zigbee3"}

Hi, thanks for the replies!

This does work for the switch on the dashboard but it doesn't update if the device is physically pressed.

The switch node is set to JSON with {"state_l1":"ON"} and {"state_l1":"OFF"} for on and off, and it seems like it has to only have that message to trigger. If state_l2 and l3 is present in the object it doesn't work.

Took me awhile to figure this out as the zigbee2mqtt sends out a single state message if done from Node RED but not if the button physically pressed or triggered from another device, so it worked some of the time.

I've fixed it with a change node to move payload to payload.state_l1 for each of the outputs.

Thanks for the reply.

I was using the original message as I had to send it back to a MQTT node the same way for it to work. Just wish the dashboard stuff allowed me use something other than payload.

Most ui nodes do allow that. You can put in the Value Format field something like {{msg.payload.state_l1}}

Hey Colin.

I've used this to display values before, but can I use this in the on/off fields in a switch node?

Thanks!

No, I don't believe so.

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.