Mqtt with conditions taking into account previous payload

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 (4.1 KB)
t

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.

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

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

It should go after you set on or off.

Copying the json

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

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.

[{"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.

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

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

Unfortunately they do not work.
Tried both.

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.

1 Like

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