Send to mqtt a json only if certain condition

I have the following json that is exported from a function.
"{"HeatingUpstairs":{"RSSI":-57,"LQI":49,"Battery":2650,"State":1,"Flags":1,"Measured":21.12,"Required":21.50}}"

I want the following to be sent to an mqtt only and only if the values of Battery, State and flag simultaneously are not zero all three off them.

If they are all three zero, nothing will be sent.
Any thoughts?

These two lines seem to contradict each other.

You should be able to use a JSONata expression in the property field of the switch node.
e.g. ( A guess as confusion noted)

0 in [$$.payload.forecasts.HeatingUpstairs.Battery,
    $$.payload.forecasts.HeatingUpstairs.State,
    $$.payload.forecasts.HeatingUpstairs.Flags]

then check for true or false

Example flow

[{"id":"24197d27a440a79f","type":"inject","z":"452103ea51141731","name":"","props":[{"p":"payload.forecasts","v":"{\"HeatingUpstairs\":{\"RSSI\":-57,\"LQI\":49,\"Battery\":2650,\"State\":1,\"Flags\":0,\"Measured\":21.12,\"Required\":21.5}}","vt":"json"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":110,"y":4780,"wires":[["2b7ba29eeeaeba45"]]},{"id":"2b7ba29eeeaeba45","type":"switch","z":"452103ea51141731","name":"","property":"0 in [$$.payload.forecasts.HeatingUpstairs.Battery,\t    $$.payload.forecasts.HeatingUpstairs.State,\t    $$.payload.forecasts.HeatingUpstairs.Flags]","propertyType":"jsonata","rules":[{"t":"false"}],"checkall":"true","repair":false,"outputs":1,"x":270,"y":4720,"wires":[["79a6e7d13e9387bf"]]},{"id":"989e880ce2cf99af","type":"inject","z":"452103ea51141731","name":"","props":[{"p":"payload.forecasts","v":"{\"HeatingUpstairs\":{\"RSSI\":-57,\"LQI\":49,\"Battery\":2650,\"State\":1,\"Flags\":1,\"Measured\":21.12,\"Required\":21.50}}","vt":"json"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":110,"y":4720,"wires":[["2b7ba29eeeaeba45"]]},{"id":"79a6e7d13e9387bf","type":"debug","z":"452103ea51141731","name":"debug 234","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":510,"y":4740,"wires":[]}]

Let me rephrase again

If Battery == 0 && State == 0 && Flags ==0 do not sent to mqtt
else sent to mqtt the whole json

A function to do it, as an alternative to Jsonata, and very similar to your rephrased requirement:

if (msg.payload.HeatingUpstairs.Battery == 0 && 
    msg.payload.HeatingUpstairs.State == 0 && 
    msg.payload.HeatingUpstairs.Flags == 0) {
        return null
    }
return msg;

Then this should work.
In switch node check for true.

true in [$$.payload.HeatingUpstairs.Battery,
    $$.payload.HeatingUpstairs.State,
    $$.payload.HeatingUpstairs.Flags].($!=0)

[edit] fixed wrong properties

I am getting this during testing
2/24/2023, 12:07:26 AMnode: debug 234
msg.payload : Object
{ forecasts: object }
2/24/2023, 12:07:39 AMnode: debug 234
msg.payload : Object
{ forecasts: object }

My fault, had an extra property in the inject nodes, through everything off.

true in [$$.payload.HeatingUpstairs.Battery,
    $$.payload.HeatingUpstairs.State,
    $$.payload.HeatingUpstairs.Flags].($!=0)

test flow

[{"id":"989e880ce2cf99af","type":"inject","z":"452103ea51141731","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"HeatingUpstairs\":{\"RSSI\":-57,\"LQI\":49,\"Battery\":2650,\"State\":1,\"Flags\":0,\"Measured\":21.12,\"Required\":21.5}}","payloadType":"json","x":110,"y":4720,"wires":[["2b7ba29eeeaeba45"]]},{"id":"2b7ba29eeeaeba45","type":"switch","z":"452103ea51141731","name":"","property":"true in [$$.payload.HeatingUpstairs.Battery,\t    $$.payload.HeatingUpstairs.State,\t    $$.payload.HeatingUpstairs.Flags].($!=0)","propertyType":"jsonata","rules":[{"t":"true"}],"checkall":"true","repair":false,"outputs":1,"x":270,"y":4720,"wires":[["79a6e7d13e9387bf"]]},{"id":"24197d27a440a79f","type":"inject","z":"452103ea51141731","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"HeatingUpstairs\":{\"RSSI\":-57,\"LQI\":49,\"Battery\":0,\"State\":0,\"Flags\":0,\"Measured\":21.12,\"Required\":21.5}}","payloadType":"json","x":110,"y":4780,"wires":[["2b7ba29eeeaeba45"]]},{"id":"79a6e7d13e9387bf","type":"debug","z":"452103ea51141731","name":"debug 234","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":510,"y":4740,"wires":[]}]

OR

false in [$$.payload.HeatingUpstairs.Battery,
    $$.payload.HeatingUpstairs.State,
    $$.payload.HeatingUpstairs.Flags].($=0)

This is a returned as an object. Shouldn't this be in a json format like it was before passing though the switch

In addition can you please parameterize the switch since the json will not be the same.

Please supply a flow with inject nodes with different payloads. Its hard giving answers when not given all information.

Add a json node before , it will turn your json into object. The mqtt node will accept a object and will convert it.
e.g.

[{"id":"6b54e3a77601e9cd","type":"inject","z":"452103ea51141731","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"HeatingUpstairs\":{\"RSSI\":-57,\"LQI\":49,\"Battery\":2650,\"State\":1,\"Flags\":0,\"Measured\":21.12,\"Required\":21.50}}","payloadType":"str","x":170,"y":4880,"wires":[["863a8ee7cebd3eae"]]},{"id":"863a8ee7cebd3eae","type":"json","z":"452103ea51141731","name":"","property":"payload","action":"","pretty":false,"x":330,"y":4820,"wires":[["a59d84a3ff440f70"]]},{"id":"065f5aa0712a3e9f","type":"inject","z":"452103ea51141731","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"HeatingUpstairs\":{\"RSSI\":-57,\"LQI\":49,\"Battery\":0,\"State\":0,\"Flags\":0,\"Measured\":21.12,\"Required\":21.5}}","payloadType":"str","x":170,"y":4940,"wires":[["863a8ee7cebd3eae"]]},{"id":"a59d84a3ff440f70","type":"switch","z":"452103ea51141731","name":"","property":"true in [$$.payload.HeatingUpstairs.Battery,     $$.payload.HeatingUpstairs.State,     $$.payload.HeatingUpstairs.Flags].($!=0)","propertyType":"jsonata","rules":[{"t":"true"}],"checkall":"true","repair":false,"outputs":1,"x":470,"y":4820,"wires":[["fc8bbccf27983eb1"]]},{"id":"fc8bbccf27983eb1","type":"debug","z":"452103ea51141731","name":"debug 234","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":570,"y":4900,"wires":[]}]

Why not export an object from the function?

Why do you need it as a JSON string instead of a javascript object? The MQTT node can handle objects.

Let me try to give you more information. The json file is HUGE so it will make things more complicated.
There is a function that among others export this
Owl/HeatingUpstairs : msg.payload : string[110]

"{"HeatingUpstairs":{"RSSI":-56,"LQI":49,"Battery":2650,"State":0,"Flags":0,"Measured":21.05,"Required":15.00}}"

For the output of this function I connect to an mqtt out node.
I have put the switch between the output of the function and the mqtt out node. The paylod does not go though.

I have tested your logic and works correctly by ingesting the data.

It was my mistake. It is not needed. You are correct.

Then send a section of it showing the issue. add injects to start showing msg's at that point.

Make the function output a object, as objects are easier to work with.

Is the logic working in the switch node, your post leaves confusion.
Outputting a object from the function should fix this issue.

2 json did the trick.
One before the switch, converted to object and one after.
Thanks guys.

If the one after is feeding an mqtt node then, as I said before, you don't need it.

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