Using data directly from the JSON output of MQTT

I have a working flow below, but I want to understand how to simplify it so my flows are cleaner (and learn something new). I was thinking a function node could do all of it, and would appreciate any pointers.

The input is a zigbee door sensor which output the JSON string over MQTT.
When the door is open (contact = false) I want the message to be passed onto a trigger node.
When the door is closed (contact = true) I want the message blocked.

Current flow and debugs:

Change node:
02%20change%20node

Switch node:
03%20switch%20node

My basic thoughts of a function node would be:

var doorstate = msg.payload.contact;        //read contact state into a variable

if (doorstate = false)       // false means door is open
msg.payload = 1;            //any value works when sent to the trigger node

return msg;

I'm not sure how to read the payload from the MQTT node.
Also I 'think' a payload may still get passed through to the trigger node like this.

Thanks

You can set the mqtt node to ouput json directly, saves 1 node.

In the change node example, you change false to "false", could you explain why this is needed ?

The easiest would be to use a switch node and configure it like:

1 Like

Hi @bakman2

It's because the switch node does not have a "== boolean true" option, so it did not work.
So I am changing it from a "booleon true" to a string which is "true".

Probably totally wrong logic on my part :slight_smile:

The is true and is false is the boolean test in the switch node.

1 Like

Of course! That makes sense thanks.
I believe I still currently need the change node to set the payload to "payload.contact".

Any tips how to read the JSON message from the MQTT node to get the contact state?

You don't need the change node - if the mqtt node outputs the example above.

mqtt (set the output to a parsed JSON object) -> switch node (configure it as shown in the reply above) -> trigger node

1 Like

Excellent - that all works with only the MQTT node > switch node > trigger node.

I really ought to look at all the node options in the future.

Thanks very much @bakman2 :+1:

1 Like