# Send to mqtt a json only if certain condition

**URL:** <https://discourse.nodered.org/t/send-to-mqtt-a-json-only-if-certain-condition/75655>\
**Category:** General\
**Tags:** mqtt\
**Created:** [23 February 2023 20:47 UTC](https://discourse.nodered.org/t/send-to-mqtt-a-json-only-if-certain-condition/75655 "2023-02-23T20:47:54Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![03397](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/03397/32/75347_2.png) [@03397](https://discourse.nodered.org/u/03397)\
**Post date:** [23 February 2023 20:47 UTC](https://discourse.nodered.org/t/send-to-mqtt-a-json-only-if-certain-condition/75655/1 "2023-02-23T20:47:54Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [23 February 2023 21:25 UTC](https://discourse.nodered.org/t/send-to-mqtt-a-json-only-if-certain-condition/75655/2 "2023-02-23T21:25:50Z")

</div>

> [@03397](#):
>
> 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.

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)

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

```

then check for true or false

Example flow

```auto
[{"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":[]}]

```

---

<div class="post-metadata">

**Author:** ![03397](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/03397/32/75347_2.png) [@03397](https://discourse.nodered.org/u/03397)\
**Post date:** [23 February 2023 21:32 UTC](https://discourse.nodered.org/t/send-to-mqtt-a-json-only-if-certain-condition/75655/3 "2023-02-23T21:32:06Z")

</div>

Let me rephrase again

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

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [23 February 2023 21:41 UTC](https://discourse.nodered.org/t/send-to-mqtt-a-json-only-if-certain-condition/75655/4 "2023-02-23T21:41:41Z")

</div>

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

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

```

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [23 February 2023 21:45 UTC](https://discourse.nodered.org/t/send-to-mqtt-a-json-only-if-certain-condition/75655/5 "2023-02-23T21:45:51Z")

</div>

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

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

```

[edit] fixed wrong properties

---

<div class="post-metadata">

**Author:** ![03397](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/03397/32/75347_2.png) [@03397](https://discourse.nodered.org/u/03397)\
**Post date:** [23 February 2023 22:09 UTC](https://discourse.nodered.org/t/send-to-mqtt-a-json-only-if-certain-condition/75655/6 "2023-02-23T22:09:04Z")

</div>

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 }

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [23 February 2023 22:22 UTC](https://discourse.nodered.org/t/send-to-mqtt-a-json-only-if-certain-condition/75655/7 "2023-02-23T22:22:08Z")

</div>

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

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

```

test flow

```auto
[{"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

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

```

---

<div class="post-metadata">

**Author:** ![03397](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/03397/32/75347_2.png) [@03397](https://discourse.nodered.org/u/03397)\
**Post date:** [24 February 2023 08:45 UTC](https://discourse.nodered.org/t/send-to-mqtt-a-json-only-if-certain-condition/75655/8 "2023-02-24T08:45:21Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![03397](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/03397/32/75347_2.png) [@03397](https://discourse.nodered.org/u/03397)\
**Post date:** [24 February 2023 08:55 UTC](https://discourse.nodered.org/t/send-to-mqtt-a-json-only-if-certain-condition/75655/9 "2023-02-24T08:55:03Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [24 February 2023 09:20 UTC](https://discourse.nodered.org/t/send-to-mqtt-a-json-only-if-certain-condition/75655/10 "2023-02-24T09:20:08Z")

</div>

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.

```auto
[{"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?

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [24 February 2023 13:40 UTC](https://discourse.nodered.org/t/send-to-mqtt-a-json-only-if-certain-condition/75655/11 "2023-02-24T13:40:02Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![03397](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/03397/32/75347_2.png) [@03397](https://discourse.nodered.org/u/03397)\
**Post date:** [24 February 2023 13:49 UTC](https://discourse.nodered.org/t/send-to-mqtt-a-json-only-if-certain-condition/75655/12 "2023-02-24T13:49:54Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![03397](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/03397/32/75347_2.png) [@03397](https://discourse.nodered.org/u/03397)\
**Post date:** [24 February 2023 13:50 UTC](https://discourse.nodered.org/t/send-to-mqtt-a-json-only-if-certain-condition/75655/13 "2023-02-24T13:50:22Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [24 February 2023 13:54 UTC](https://discourse.nodered.org/t/send-to-mqtt-a-json-only-if-certain-condition/75655/14 "2023-02-24T13:54:51Z")

</div>

> [@03397](#):
>
> The json file is HUGE so it will make things more complicated.

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

> [@03397](#):
>
> There is a function that among others export this  
> Owl/HeatingUpstairs : msg.payload : string[110]

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

> [@03397](#):
>
> 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.

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

---

<div class="post-metadata">

**Author:** ![03397](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/03397/32/75347_2.png) [@03397](https://discourse.nodered.org/u/03397)\
**Post date:** [24 February 2023 16:27 UTC](https://discourse.nodered.org/t/send-to-mqtt-a-json-only-if-certain-condition/75655/15 "2023-02-24T16:27:30Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [24 February 2023 16:56 UTC](https://discourse.nodered.org/t/send-to-mqtt-a-json-only-if-certain-condition/75655/16 "2023-02-24T16:56:14Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [25 April 2023 16:56 UTC](https://discourse.nodered.org/t/send-to-mqtt-a-json-only-if-certain-condition/75655/17 "2023-04-25T16:56:37Z")

</div>

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