MQTT feed as trigger (mosquito) [GOT CREATED]

Hi

Is there any “easy” possibility to subscribe to a MQTT feed (mosquito) and use that as a trigger?

Kind regards
Mattias

Currently probably not. We have only the “AMQP Trigger” Node that is using the rhea npm library which supports “AMQP” but not MQTT. To support it, a special node would be required.

@Mattias_Larsson Just created the MQTT trigger. However I have no experience with it, it’s very simple. Can you please check it out and provide feedback?

Thanks! I will try as soon as i get connection to server:)

MQTT Trigger Node got released with [email protected]

1 Like

Hi, I am just experimenting with this trigger and I am actually completely new to n8n and mqtt. I have an iobroker checkbox that sends the mqtt message to the trigger and basically it works. But it works on true and false and I’d like it to trigger only on false. I’d appreciate a hint how to facilitate this. Cheers

There is no way to do that directly in the Trigger-Node. It will start the workflow no matter which data it receives. What you can do though is to have an IF-Node which filters out the events you do not want to have (and so the workflow stops) and so only continues on the ones you want.

thanks … will look into it :slight_smile:

If you have any problems simply get back to me. Here also the documentation of the IF-Node:

In fact I do … I read the documentation and I tried to copy from another workflow … but I fail.
Could you get me started with this please … the workflow is only mqtt-trigger -> if -> two pushover notifications … one for true the other for false.

Hey @daniello!

I have created a workflow that might help you. My workflow listens for two MQTT topics - /n8n and /n8n_io. The IF node checks the topic that triggered the workflow. If the message is coming from the /n8n topic, the IF node returns true, and the Pushover node is executed. If the message is coming from anyother topic, the IF node will return false and the Pushover1 node will be executed. I hope this helps. :slightly_smiling_face:

{
  "nodes": [
    {
      "parameters": {
        "topics": "/n8n,/n8n_io",
        "options": {}
      },
      "name": "MQTT Trigger",
      "type": "n8n-nodes-base.mqttTrigger",
      "typeVersion": 1,
      "position": [
        320,
        361
      ],
      "credentials": {
        "mqtt": "mqtt"
      }
    },
    {
      "parameters": {
        "conditions": {
          "string": [
            {
              "value1": "={{$node[\"MQTT Trigger\"].json[\"topic\"]}}",
              "value2": "/n8n"
            }
          ]
        }
      },
      "name": "IF",
      "type": "n8n-nodes-base.if",
      "typeVersion": 1,
      "position": [
        520,
        361
      ]
    },
    {
      "parameters": {
        "userKey": "",
        "message": "={{$node[\"MQTT Trigger\"].json[\"message\"]}}",
        "priority": 0,
        "additionalFields": {}
      },
      "name": "Pushover",
      "type": "n8n-nodes-base.pushover",
      "typeVersion": 1,
      "position": [
        750,
        250
      ],
      "credentials": {
        "pushoverApi": "pushover"
      }
    },
    {
      "parameters": {
        "userKey": "",
        "message": "={{$node[\"MQTT Trigger\"].json[\"message\"]}}",
        "priority": 0,
        "additionalFields": {}
      },
      "name": "Pushover1",
      "type": "n8n-nodes-base.pushover",
      "typeVersion": 1,
      "position": [
        740,
        450
      ],
      "credentials": {
        "pushoverApi": "pushover"
      }
    }
  ],
  "connections": {
    "MQTT Trigger": {
      "main": [
        [
          {
            "node": "IF",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "IF": {
      "main": [
        [
          {
            "node": "Pushover",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Pushover1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}
1 Like

Thanks a lot @harshil1712!

@daniello and I forgot to mention. Please open in the future a new topic if you have a problem. This topic is a feature-request and not about a specific problem. If we do not make sure that people do that correctly the whole forum will become a huge mess and nobody finds the answers to questions that got already asked and answered before.Thanks a lot!

1 Like

@jan not sure if I should open an extra thread now … next time for sure
@harshil1712 … thanks … but not there yet :frowning:

Maybe this makes it quick to fix (don’t know how to make a code section):

{
  "nodes": [
    {
      "parameters": {
        "topics": "mqtt.0.n8n-trigger",
        "options": {
          "onlyMessage": true
        }
      },
      "name": "MQTT Trigger",
      "type": "n8n-nodes-base.mqttTrigger",
      "typeVersion": 1,
      "position": [
        580,
        300
      ],
      "credentials": {
        "mqtt": "ioBroker Mqtt"
      }
    },
    {
      "parameters": {
        "userKey": "..",
        "message": "mqtt trigger true",
        "additionalFields": {}
      },
      "name": "Pushover",
      "type": "n8n-nodes-base.pushover",
      "typeVersion": 1,
      "position": [
        1070,
        280
      ],
      "credentials": {
        "pushoverApi": "Pushover"
      }
    },
    {
      "parameters": {
        "conditions": {
          "string": [
            {
              "value2": "true"
            }
          ],
          "boolean": []
        }
      },
      "name": "IF1",
      "type": "n8n-nodes-base.if",
      "position": [
        790,
        300
      ],
      "typeVersion": 1
    },
    {
      "parameters": {
        "userKey": "..",
        "message": "mqtt trigger false",
        "additionalFields": {}
      },
      "name": "Pushover1",
      "type": "n8n-nodes-base.pushover",
      "typeVersion": 1,
      "position": [
        1070,
        430
      ],
      "credentials": {
        "pushoverApi": "Pushover"
      }
    }
  ],
  "connections": {
    "MQTT Trigger": {
      "main": [
        [
          {
            "node": "IF1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "IF1": {
      "main": [
        [
          {
            "node": "Pushover",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Pushover1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}

@daniello you have not set a proper condition in the IF node. The value 1 field is empty while the value 2 has the value true. Hence, the IF node is comparing an empty string with the string true. Since these values are not equal the IF node will return false and your Pushover1 node will execute. You need to set a proper condition in the IF node. I am not sure what the condition you want to check, but if you want to check from which topic the message was sent, you can use the workflow I shared earlier and in the IF node change the value for the value 2 field.

I thought the value1 was handed over by the trigger … my mistake obviously. So I have do “true equals true”?

I only have one topic … but the message can either be true or false … this I want to split with “if”

@daniello I have made some changes to the workflow based on your message. The IF node checks for the message, whether the message is true or not. Based on the output of the IF node the respective Pushover node is executed. I hope this helps. :slightly_smiling_face:

{
  "nodes": [
    {
      "parameters": {
        "topics": "n8n",
        "options": {}
      },
      "name": "MQTT Trigger",
      "type": "n8n-nodes-base.mqttTrigger",
      "typeVersion": 1,
      "position": [
        450,
        300
      ],
      "credentials": {
        "mqtt": "mqtt-test"
      }
    },
    {
      "parameters": {
        "conditions": {
          "string": [
            {
              "value1": "={{JSON.parse($node[\"MQTT Trigger\"].json[\"message\"])['message']}}",
              "value2": "true"
            }
          ],
          "boolean": []
        }
      },
      "name": "IF",
      "type": "n8n-nodes-base.if",
      "typeVersion": 1,
      "position": [
        680,
        300
      ]
    },
    {
      "parameters": {
        "userKey": "",
        "message": "Message for false",
        "priority": 0,
        "additionalFields": {}
      },
      "name": "Pushover",
      "type": "n8n-nodes-base.pushover",
      "typeVersion": 1,
      "position": [
        950,
        200
      ],
      "credentials": {
        "pushoverApi": "pushover"
      }
    },
    {
      "parameters": {
        "userKey": "",
        "message": "Message for False",
        "priority": 0,
        "additionalFields": {}
      },
      "name": "Pushover1",
      "type": "n8n-nodes-base.pushover",
      "typeVersion": 1,
      "position": [
        950,
        400
      ],
      "credentials": {
        "pushoverApi": "pushover"
      }
    }
  ],
  "connections": {
    "MQTT Trigger": {
      "main": [
        [
          {
            "node": "IF",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "IF": {
      "main": [
        [
          {
            "node": "Pushover",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Pushover1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}

@harshil1712

I assume this should work but I still only get false messages. I have the assumption that the message is different than expected … maybe 0/1 instead true/false … I’ll have to investigate.

Thanks for your patience.
@jan … thanks for tolerating this … it could be deleted if it disturbs.