Shelly integration & MQTT (via MacBook)

Hey Node RED community.

I'm on a beginners level using Node RED to monitor and control Shelly devices.
My setup at the moment is a Shelly Plus 1 which is connected to my network and I was setting up Node RED on my MacBook.

Now I'm a bit overwhelmed with all steps to do in order to control the Shelly (switching on and off)

Here's what I can say for sure.

  • Mosquitto v2.0.20 is installed with homebrew and the broker is running.
  • The IP's of MacBook ({MacBook-IP}.9) and Shelly device ({ShellyDevice-IP}.9) indicate that it's connected to the same network.
  • Shelly device runs with activated MQTT

Shelly device MQTT settings:

  • Connection type: No TLS
  • MQTT prefix: shellyplus1-{shellyID}
  • Server: {MacBook-IP}.9
  • Client ID: shellyplus1-{shellyID}
  • Username: {my-mqtt-user-name}
  • Password: {my-mqtt-user-password}

Node RED "mqtt out" node set to:

  • Topic: shellies/shellyplus1-{shellyID}/relay/0/command/
  • QoS: empty
  • Server: {MacBook-IP}.9 | Port: 1883 | Protocol: MQTT V3.1.1
  • Username: {my-mqtt-user-name}
  • Password: {my-mqtt-user-password}

Now when I'm deploying in Node RED, it shows "connecting" below the MQTT-node and I'm not able to trigger anything.

I would appreciate constructive hints for my issue as I assume setting up should be a straight forward thing but I'm lacking of background knowledge.

Thanks in advance :slight_smile:

Seems your not able to connect to the mqtt broker
mqtt2 has some security settings that are enabled by default, or your user and password are incorrect

have a look here for settings for mqtt config file.

Hey @E1cid.

Thanks for that.

I tried myself through. Found the config file, and uncommented
allow_anonymous true
and
listener 1883

I also found that bind_address 0.0.0.0 should be in there.

Unfortunately without success. Everything is still having the same outcome.

The node-red terminal message shows:
[info] [mqtt-broker:Switcher] Connection failed to broker: mqtt://172.20.10.9:1883

However, when I set localhost as Server in Node RED then I see

[info] [mqtt-broker:Switcher] Connected to broker: mqtt://localhost:1883

But that doesn't solve the Shelly issue.

Is this your Mac?
Does it have a static / reserved IP address? What address?
What is it's hostname and can you specify a hostname in the Shelly?
What is the IP address of the Shelly (which doesn't have to be static)?

Is this your Mac?

Yes

Does it have a static / reserved IP address? What address?

No, DHCP. The address is 172.20.10.9

What is it's hostname and can you specify a hostname in the Shelly?

What do you mean by hostname?

As to the Shelly. Here you have a screenshot how the MQTT setting looks (I just googled it):

What is the IP address of the Shelly (which doesn't have to be static)?

It's 172.20.10.2. I assume it gets assigned automatically. It is indeed possible to set a static IP address, Network mask, Gateway and DNS.

According to your shelly setting the server is 192.168.10.13
but your notebook is 172.20.10.9

The IP of the notebook(where mosquitto is installed) should match the IP of the server in the shelly settings. Which should be a local lan IP not your internet wan IP

You should be able to set static IP's in your router using the mac address of the device. The broker will need a static IP.

Please be aware that the image I posted above is googled and not a screenshot of my own screen. I just posted it for demonstration purposes.

A computer can be referred to on a network using either it's IP address or a hostname.
I don't know Macs but on my Linux computers the hostname is specified in /etc/hostname and can be shown with the hostname command.
For example:

pi@GlassPi:~ $ hostname
GlassPi

While the IP address may be set by the network DHCP server and might vary, the hostname is specified by the comouter itself and is thus fixed.

I asked about this because you may be able to specify the hostname rather than the IP address as the MQTT server (I have no idea if this will work for your Shelly).
It's more usual though for the MQTT broker to have a static IP address, or at least a reserved address in the router.

Screenshots can sometimes help us understand how a system is set up.
Not if they are a random photo from the internet though.

Hey @jbudd and @E1cid.

I understand your confusion about the content I provided as it always seems to be hard to think into an issue.
Thanks for your inputs. I went back to the beginning and started off new.

I made it work and can state the following:

  • there's no need to fix the IP
  • something like hostname is not needed

Here's what does the trick.

In the MacBook's Terminal:

  1. Properly setting up Mosquitto via brew on the Mac.
  2. Starting Mosquitto, listening to port 1883 (default)

In Shelly settings page

  1. Finding the Mac's IP address which (for now) is my (testing) server. Mine is 192.168.0.56
  2. Navigating to Shelly setup via browser: My Shelly IP is 192.168.0.31, so http://192.168.0.31/#/settings/mqtt will bring me to the settings page for MQTT.
  3. Tick the box for "Enable" at the top to start MQTT
  4. Tick the box for "Enable 'MQTT Control'
  5. Tick the box for "RPC over MQTT"
  6. Tick the box for "RPC status notifications over MQTT"
  7. Tick the box for "Generic status update over MQTT"
  8. Set the Server to: "192.168.0.56:1883"
  9. Set Client ID to: "{my-shelly-device_ID}"

In Node-RED

  1. Create an Inject-Node called "On"
  2. Set msg.payload to
{
    "id": 1,
    "src": "mqtt",
    "method": "Switch.Set",
    "params": {
        "id": 0,
        "on": true
    }
}
  1. Click "Done" to close the Inject node settings
  2. Create another Inject-Node called "Off"
  3. Set msg.payload to
{
    "id": 1,
    "src": "mqtt",
    "method": "Switch.Set",
    "params": {
        "id": 0,
        "on": false
    }
}
  1. Click "Done" to close the Inject node settings
  2. Create an mqtt-out-Node
  3. Connect the On and Off Inject Nodes to the mqtt-out-Node
  4. Open the mqtt-out-Node
  5. Set the Topic to {my-shelly-device_ID}/rpc
  6. Under Server edit or create the following settings:
  7. Server: 192.168.0.56 (that's my MacBooks IP address)
  8. Port: 1883
  9. Tick the box for "Connect automatically"
  10. Click "Update" and "Done"

Now "Deploy" the flow and run a test.

Suddenly it worked without any issue.

Summary:

{my-shelly-device_ID}/rpc

followed by

{
    "id": 1,
    "src": "mqtt",
    "method": "Switch.Set",
    "params": {
        "id": 0,
        "on": false OR true
    }
}

Did the trick.

I hope that might help someone in the future.

Without the fixed IP of the mqtt broker or hostname, if the IP of the Mac Book changes (as router may give the Mac Book a new IP in the near future) Then node-red and the Shelly devices will not know what the new IP may be. So again i would advise to fix the IP or set a Hostname.

This is very true but was not part of my problem :slight_smile: And I also referred to my setup with:

I will surely fix my IP address once I transfer it to production environment :slight_smile:

Your issue was you were using an incorrect IP (172.20.10.9) for the broker settings, if you had fixed the IP you would of been sure you where using the correct one.