# Mqtt with conditions taking into account previous payload

**URL:** <https://discourse.nodered.org/t/mqtt-with-conditions-taking-into-account-previous-payload/75732>\
**Category:** General\
**Created:** [25 February 2023 16:03 UTC](https://discourse.nodered.org/t/mqtt-with-conditions-taking-into-account-previous-payload/75732 "2023-02-25T16:03:35Z")\
**Posts on this page:** 10\
**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:** [25 February 2023 16:03 UTC](https://discourse.nodered.org/t/mqtt-with-conditions-taking-into-account-previous-payload/75732/1 "2023-02-25T16:03:35Z")

</div>

I have the attached flow. This listen to a topic and when an "on" or "off" comes it executes two tasks.  
I want to modify this and execute the two tasks with these if conditions:  
It will execute the "off" if the previous payload was "on" and It will execute the "on" if only the previous payload was "off". Else, nothing will be executed.  
[flows.json](https://discourse.nodered.org/uploads/short-url/fQQxO8srPfu29Rm2HQlXWyvEFeC.json) (4.1 KB)  
t

---

<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:** [25 February 2023 16:07 UTC](https://discourse.nodered.org/t/mqtt-with-conditions-taking-into-account-previous-payload/75732/2 "2023-02-25T16:07:39Z")

</div>

I think you may only need to put a filter node in place, it will only allow the payload to be forwarded if it is different from last payload.

---

<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:** [25 February 2023 16:12 UTC](https://discourse.nodered.org/t/mqtt-with-conditions-taking-into-account-previous-payload/75732/3 "2023-02-25T16:12:10Z")

</div>

Can you please show me using the json I have attached?

---

<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:** [25 February 2023 16:13 UTC](https://discourse.nodered.org/t/mqtt-with-conditions-taking-into-account-previous-payload/75732/4 "2023-02-25T16:13:29Z")

</div>

If you paste an example flow, as i do not download files.

It should go after you set on or off.

---

<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:** [25 February 2023 16:15 UTC](https://discourse.nodered.org/t/mqtt-with-conditions-taking-into-account-previous-payload/75732/5 "2023-02-25T16:15:30Z")

</div>

Copying the json

```auto
[
    {
        "id": "817c804cf7d2c6cd",
        "type": "mqtt in",
        "z": "b1d941b8829a44a4",
        "name": "Boost Heating Downstairs MQTT",
        "topic": "Owl/HeatingDownstairsBoost/set",
        "qos": "0",
        "datatype": "auto-detect",
        "broker": "91b9efe8.4a598",
        "nl": false,
        "rap": true,
        "rh": 0,
        "inputs": 0,
        "x": 750,
        "y": 440,
        "wires": [
            [
                "6ed85933320eda89"
            ]
        ]
    },
    {
        "id": "6ed85933320eda89",
        "type": "switch",
        "z": "b1d941b8829a44a4",
        "name": "MQTT Payload",
        "property": "payload",
        "propertyType": "msg",
        "rules": [
            {
                "t": "eq",
                "v": "ON",
                "vt": "str"
            },
            {
                "t": "eq",
                "v": "OFF",
                "vt": "str"
            }
        ],
        "checkall": "false",
        "repair": false,
        "outputs": 2,
        "x": 900,
        "y": 500,
        "wires": [
            [
                "27b4a75281a80ad9"
            ],
            [
                "c0c0d24a4efcee8e"
            ]
        ]
    },
    {
        "id": "27b4a75281a80ad9",
        "type": "ui_button",
        "z": "b1d941b8829a44a4",
        "name": "",
        "group": "6b03e370646d518f",
        "order": 0,
        "width": 0,
        "height": 0,
        "passthru": true,
        "label": "Boost Heating Downstairs On MQTT",
        "tooltip": "",
        "color": "",
        "bgcolor": "",
        "className": "",
        "icon": "",
        "payload": "BOOST,ON,20020B0",
        "payloadType": "str",
        "topic": "",
        "topicType": "str",
        "x": 1210,
        "y": 500,
        "wires": [
            [
                "b845f487468b553f"
            ]
        ]
    },
    {
        "id": "c0c0d24a4efcee8e",
        "type": "ui_button",
        "z": "b1d941b8829a44a4",
        "name": "",
        "group": "6b03e370646d518f",
        "order": 0,
        "width": 0,
        "height": 0,
        "passthru": true,
        "label": "Boost Heating Downstairs Off MQTT",
        "tooltip": "",
        "color": "",
        "bgcolor": "",
        "className": "",
        "icon": "",
        "payload": "BOOST,OFF,20020B0",
        "payloadType": "str",
        "topic": "",
        "topicType": "str",
        "x": 1210,
        "y": 540,
        "wires": [
            [
                "b845f487468b553f"
            ]
        ]
    },
    {
        "id": "b845f487468b553f",
        "type": "function",
        "z": "b1d941b8829a44a4",
        "name": "Amend to include your UDP Code",
        "func": "msg.payload += \",3C5E3EA8\" // Add UDP code (don't delete the comma)\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 2860,
        "y": 180,
        "wires": [
            []
        ]
    },
    {
        "id": "91b9efe8.4a598",
        "type": "mqtt-broker",
        "name": "MQTT",
        "broker": "192.168.10.104",
        "port": "1883",
        "clientid": "",
        "autoConnect": true,
        "usetls": false,
        "compatmode": false,
        "protocolVersion": "4",
        "keepalive": "60",
        "cleansession": true,
        "birthTopic": "",
        "birthQos": "0",
        "birthPayload": "",
        "birthMsg": {},
        "closeTopic": "",
        "closeQos": "0",
        "closePayload": "",
        "closeMsg": {},
        "willTopic": "",
        "willQos": "0",
        "willPayload": "",
        "willMsg": {},
        "userProps": "",
        "sessionExpiry": "",
        "credentials": {}
    },
    {
        "id": "6b03e370646d518f",
        "type": "ui_group",
        "name": "MQTT Hidden",
        "tab": "f3240690e392b62a",
        "order": 3,
        "disp": true,
        "width": "6",
        "collapse": false,
        "className": ""
    },
    {
        "id": "f3240690e392b62a",
        "type": "ui_tab",
        "name": "Hidden",
        "icon": "dashboard",
        "disabled": false,
        "hidden": true
    }
]
```

---

<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:** [25 February 2023 16:27 UTC](https://discourse.nodered.org/t/mqtt-with-conditions-taking-into-account-previous-payload/75732/6 "2023-02-25T16:27:18Z")

</div>

I would think right after the mqtt node.

P.S.  
We help for free, best to make life easy for us.  
Paste code/json between triple backticks, that way we are presented with a copy icon.

[Edit] Example flow set your own mqtt config.

```auto
[{"id":"817c804cf7d2c6cd","type":"mqtt in","z":"65617ffeb779f51c","name":"Boost Heating Downstairs MQTT","topic":"Owl/HeatingDownstairsBoost/set","qos":"0","datatype":"auto-detect","broker":"e8ba3ef5.22f4a8","nl":false,"rap":true,"rh":0,"inputs":0,"x":150,"y":220,"wires":[["a73909a1cf817ebe"]]},{"id":"a73909a1cf817ebe","type":"rbe","z":"65617ffeb779f51c","name":"","func":"rbe","gap":"","start":"","inout":"out","septopics":true,"property":"payload","topi":"topic","x":150,"y":300,"wires":[["6ed85933320eda89"]]},{"id":"6ed85933320eda89","type":"switch","z":"65617ffeb779f51c","name":"MQTT Payload","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"ON","vt":"str"},{"t":"eq","v":"OFF","vt":"str"}],"checkall":"false","repair":false,"outputs":2,"x":320,"y":340,"wires":[["27b4a75281a80ad9"],["c0c0d24a4efcee8e"]]},{"id":"27b4a75281a80ad9","type":"ui_button","z":"65617ffeb779f51c","name":"","group":"6b03e370646d518f","order":0,"width":0,"height":0,"passthru":true,"label":"Boost Heating Downstairs On MQTT","tooltip":"","color":"","bgcolor":"","className":"","icon":"","payload":"BOOST,ON,20020B0","payloadType":"str","topic":"","topicType":"str","x":630,"y":340,"wires":[["b845f487468b553f"]]},{"id":"c0c0d24a4efcee8e","type":"ui_button","z":"65617ffeb779f51c","name":"","group":"6b03e370646d518f","order":0,"width":0,"height":0,"passthru":true,"label":"Boost Heating Downstairs Off MQTT","tooltip":"","color":"","bgcolor":"","className":"","icon":"","payload":"BOOST,OFF,20020B0","payloadType":"str","topic":"","topicType":"str","x":630,"y":380,"wires":[["b845f487468b553f"]]},{"id":"b845f487468b553f","type":"function","z":"65617ffeb779f51c","name":"Amend to include your UDP Code","func":"msg.payload += \",3C5E3EA8\" // Add UDP code (don't delete the comma)\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":960,"y":360,"wires":[["8088a2303d354bb1"]]},{"id":"8088a2303d354bb1","type":"debug","z":"65617ffeb779f51c","name":"debug 234","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":1210,"y":360,"wires":[]},{"id":"adcde34b3bef2d58","type":"inject","z":"65617ffeb779f51c","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"OFF","payloadType":"str","x":170,"y":120,"wires":[["073a64ceda4b3c34"]]},{"id":"fa5c415b46e2ae96","type":"inject","z":"65617ffeb779f51c","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"ON","payloadType":"str","x":170,"y":60,"wires":[["073a64ceda4b3c34"]]},{"id":"073a64ceda4b3c34","type":"mqtt out","z":"65617ffeb779f51c","name":"","topic":"Owl/HeatingDownstairsBoost/set","qos":"","retain":"","respTopic":"","contentType":"","userProps":"","correl":"","expiry":"","broker":"e8ba3ef5.22f4a8","x":480,"y":80,"wires":[]},{"id":"e8ba3ef5.22f4a8","type":"mqtt-broker","name":"testb","broker":"192.168.1.25","port":"1883","clientid":"node-red-test","autoConnect":true,"usetls":false,"compatmode":false,"protocolVersion":"4","keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthRetain":"false","birthPayload":"","birthMsg":{},"closeTopic":"","closeQos":"0","closePayload":"","closeMsg":{},"willTopic":"","willQos":"0","willPayload":"","willMsg":{},"userProps":"","sessionExpiry":""},{"id":"6b03e370646d518f","type":"ui_group","name":"MQTT Hidden","tab":"f3240690e392b62a","order":3,"disp":true,"width":"6","collapse":false,"className":""},{"id":"f3240690e392b62a","type":"ui_tab","name":"Hidden","icon":"dashboard","disabled":false,"hidden":true}]

```

Send on mulitple times and only one may pass until an off is received, and vice versa.

Or after ui-switch nodes if you want them to only send message once until different output.

```auto
[{"id":"817c804cf7d2c6cd","type":"mqtt in","z":"65617ffeb779f51c","name":"Boost Heating Downstairs MQTT","topic":"Owl/HeatingDownstairsBoost/set","qos":"0","datatype":"auto-detect","broker":"e8ba3ef5.22f4a8","nl":false,"rap":true,"rh":0,"inputs":0,"x":150,"y":220,"wires":[["6ed85933320eda89"]]},{"id":"6ed85933320eda89","type":"switch","z":"65617ffeb779f51c","name":"MQTT Payload","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"ON","vt":"str"},{"t":"eq","v":"OFF","vt":"str"}],"checkall":"false","repair":false,"outputs":2,"x":160,"y":300,"wires":[["27b4a75281a80ad9"],["c0c0d24a4efcee8e"]]},{"id":"27b4a75281a80ad9","type":"ui_button","z":"65617ffeb779f51c","name":"","group":"6b03e370646d518f","order":0,"width":0,"height":0,"passthru":true,"label":"Boost Heating Downstairs On MQTT","tooltip":"","color":"","bgcolor":"","className":"","icon":"","payload":"BOOST,ON,20020B0","payloadType":"str","topic":"","topicType":"str","x":410,"y":280,"wires":[["a73909a1cf817ebe"]]},{"id":"c0c0d24a4efcee8e","type":"ui_button","z":"65617ffeb779f51c","name":"","group":"6b03e370646d518f","order":0,"width":0,"height":0,"passthru":true,"label":"Boost Heating Downstairs Off MQTT","tooltip":"","color":"","bgcolor":"","className":"","icon":"","payload":"BOOST,OFF,20020B0","payloadType":"str","topic":"","topicType":"str","x":410,"y":320,"wires":[["a73909a1cf817ebe"]]},{"id":"a73909a1cf817ebe","type":"rbe","z":"65617ffeb779f51c","name":"","func":"rbe","gap":"","start":"","inout":"out","septopics":true,"property":"payload","topi":"topic","x":810,"y":300,"wires":[["b845f487468b553f"]]},{"id":"b845f487468b553f","type":"function","z":"65617ffeb779f51c","name":"Amend to include your UDP Code","func":"msg.payload += \",3C5E3EA8\" // Add UDP code (don't delete the comma)\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":960,"y":360,"wires":[["8088a2303d354bb1"]]},{"id":"8088a2303d354bb1","type":"debug","z":"65617ffeb779f51c","name":"debug 234","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":1210,"y":360,"wires":[]},{"id":"e8ba3ef5.22f4a8","type":"mqtt-broker","name":"testb","broker":"192.168.1.25","port":"1883","clientid":"node-red-test","autoConnect":true,"usetls":false,"compatmode":false,"protocolVersion":"4","keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthRetain":"false","birthPayload":"","birthMsg":{},"closeTopic":"","closeQos":"0","closePayload":"","closeMsg":{},"willTopic":"","willQos":"0","willPayload":"","willMsg":{},"userProps":"","sessionExpiry":""},{"id":"6b03e370646d518f","type":"ui_group","name":"MQTT Hidden","tab":"f3240690e392b62a","order":3,"disp":true,"width":"6","collapse":false,"className":""},{"id":"f3240690e392b62a","type":"ui_tab","name":"Hidden","icon":"dashboard","disabled":false,"hidden":true}]

```

---

<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:** [25 February 2023 16:31 UTC](https://discourse.nodered.org/t/mqtt-with-conditions-taking-into-account-previous-payload/75732/7 "2023-02-25T16:31:00Z")

</div>

Corrected the json. Can you please give more input on this since I failed so far whatever I have tried

---

<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:** [26 February 2023 21:40 UTC](https://discourse.nodered.org/t/mqtt-with-conditions-taking-into-account-previous-payload/75732/8 "2023-02-26T21:40:07Z")

</div>

Unfortunately they do not work.  
Tried both.

---

<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:** [26 February 2023 21:52 UTC](https://discourse.nodered.org/t/mqtt-with-conditions-taking-into-account-previous-payload/75732/9 "2023-02-26T21:52:20Z")

</div>

If something doesn't work it is no good just saying it doesn't work. Add debug nodes so you can work out what is happening and tell us which node is not doing what it should.

---

<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:** [27 April 2023 21:53 UTC](https://discourse.nodered.org/t/mqtt-with-conditions-taking-into-account-previous-payload/75732/10 "2023-04-27T21:53:11Z")

</div>

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