MQTT: Working Mosquitto / CloudMQTT Bridge

For anyone that’s interested this is what I did after following Bruh’s’s excellent Owntracks / CloudMQTT guide to get my local Mosquitto instance talking to CloudMQTT.

Create a new mosquitto config in the /etc/mosquitto/conf.d/ directory, I used cloudmqtt.conf (it can be any name as long as it ends with .conf so mosquitto will read it) with the following info…

connection cloudmqtt
address <Instance Server>:<Instance Port>
remote_username <Instance User>
remote_password <Instance Password>
clientid <A cloudmqtt user with read access>
try_private false
start_type automatic
topic # in

Where the items between the <> brackets are from the CloudMQTT Console “Instance Info” page.

After restarting mosquitto topics from cloudmqtt should show up. For example the user I setup on cloudmqtt and owntracks nexus6p shows up as topic owntracks/<Instance User>/nexus6p

If you need to write values back out to CloudMQTT you will need to change the last line as described in the mosquitto.conf man page

15 Likes

Was just discussing this in another thread - thanks for sharing this info with the group!

my CLoudMQTT is using mqtts with an SSL port of 2xxxx.
What do I have to do to get Mosquito talking with encryption to cloudMQTT?
I got it working with using a 1xxxx port number but I would prefer SSL.

HA works fine talking to directly to cloudMQTT with a 2xxxx port without requiring a cert path.

–mqtt n00b–

Thank you for sharing, sffjunkie. This is what I am looking for. How do you specify mqtt: section in the configuration.yaml. Would I have to point to Cloudmqtt or Mosquitto? Thanks a lot.

so your saying based on this picture here

I should have

connection cloudmqtt
address m13.cloudmqtt.com:18003
remote_username abcdefg
remote_password 12345
clientid home-assistant
try_private false
start_type automatic
topic # in

if I wanted my local MQTT to read from the CloudMQTT server?

After a lot of messing about I managed to get bridging working over ssl.

I added the line to the config

 bridge_cafile /etc/ssl/certs/ca-certificates.crt

Thats a standard cert file that contains many of the root certificates needed to talk to many sites especially the top level ones.

HA when talking to cloudmqtt knows about these root cert files and automatically uses it, but mosquitto will not, you have to tell it.

5 Likes

if i dont have a username and password of my mosquitto broker,how can i bridge it via cloudmqtt?

how could i set uri?

I configured the bridge in your way, but the following error:
17-02-08 10:58:42 ERROR (MainThread) [homeassistant.bootstrap] component mqtt failed to initialize
17-02-08 10:58:42 ERROR (MainThread) [homeassistant.bootstrap] Component mqtt failed to setup
17-02-08 10:58:42 ERROR (MainThread) [homeassistant.bootstrap] Unable to prepare setup for platform switch.mqtt because dependency mqtt could not be initialized
17-02-08 10:58:42 ERROR (Thread-3) [homeassistant.components.mqtt] Can’t connect to the broker. Please check your settings and the broker itself
Traceback (most recent call last):
File “/srv/homeassistant/lib/python3.4/site-packages/homeassistant/components/mqtt/init.py”, line 262, in setup
birth_message)
File “/srv/homeassistant/lib/python3.4/site-packages/homeassistant/components/mqtt/init.py”, line 351, in init
self._mqttc.connect(broker, port, keepalive)
File “/home/homeassistant/.homeassistant/deps/paho/mqtt/client.py”, line 686, in connect
return self.reconnect()
File “/home/homeassistant/.homeassistant/deps/paho/mqtt/client.py”, line 808, in reconnect
sock = socket.create_connection((self._host, self._port), source_address=(self._bind_address, 0))
File “/usr/lib/python3.4/socket.py”, line 509, in create_connection
raise err
File “/usr/lib/python3.4/socket.py”, line 500, in create_connection
sock.connect(sa)
ConnectionRefusedError: [Errno 111] Connection refused
17-02-08 10:58:42 ERROR (MainThread) [homeassistant.bootstrap] component mqtt failed to initialize

My configuration is as follows:
#conf.d/cloud.conf#
connection cloudmqtt
address m13.cloudmqtt.com:14443
remote_username xxx
remote_password xxxxxx
clientid home
try_private false
start_type automatic
topic # in

#yaml#

mqtt:
broker: 192.168.31.2
port: 1883
client_id: home-assistant-1
keepalive: 60
username: pi
password: raspberry
protocol: 3.1.1

switch 1:
platform: mqtt
name: “Sonoff_WoShi_1”
state_topic: “home/Bed_room/1/stat”
command_topic: “home/Bed_room/1”
qos: 1
payload_on: “on”
payload_off: “off”
retain: true
switch 2:
platform: mqtt
name: “Sonoff_WoShi_2”
state_topic: “home/Bed_room/2/stat”
command_topic: “home/Bed_room/2”
qos: 1
payload_on: “on”
payload_off: “off”
retain: true

Can you help me solve it?
Thank you

Ive actually tried this, I think I am doing something wrong because I am not getting any devices showing up which have been setup with cloudmqtt.
I still have my local mqtt devices working. It seems I cant get this to work at all, despite playing around with different conf files.
Getting frustrating now, it would be nice to have a step by step guide from start to finish on setting up a mosquitto bridge. I have searched many sites and tried so many different methods. They all say its simple, and list all kinds of different conf files.

My conf file as advised by this thread.

connection cloudmqtt
address m20.cloudmqtt.com:12139
remote_username *****
remote_password ********
clientid **
try_private false
start_type automatic
topic # in

remote username and password is from the list on the cloud mqtt instance info.
clientid is username setup with the cloud mqtt for my pi

I assume this is correct as I tried it in different ways and I was getting error messages or mqtt wouldnt work at all.

When I type in the following at the command line i get the info relative to that topic.

mosquitto_sub -h m20.cloudmqtt.com -p 12139 -u ****** -P ******* -t owntracks/****/***

I assume I am nearly there, but I have no clue how to get home assistant to display the devices on the frontend.

Thanks! This worked like a charm, just copy paste :smile:

connection cloudmqtt
address :
remote_username
remote_password
clientid
try_private false
start_type automatic
topic # in

Hi,
tried it the same way and got stuck at some points - perhaps one of them is yours too:

mosquitto.conf

# Place your local configuration in /etc/mosquitto/conf.d/
#
# A full description of the configuration file is at
# /usr/share/doc/mosquitto/examples/mosquitto.conf.example

# For debugging
#log_type all

pid_file /var/run/mosquitto.pid

persistence true
persistence_location /var/lib/mosquitto/

log_dest file /var/log/mosquitto/mosquitto.log

user mosquitto

allow_anonymous true
password_file /etc/mosquitto/pwfile
listener 1883 localhost
listener 8883 localhost
listener 1883 192.168.178.61
listener 8883 192.168.178.61

#include_dir /etc/mosquitto/conf.d

connection cloudmqtt
address <cloudmqtt-host>:<cloudmqtt-port>
topic # in // fetches all incoming topics from cloudmqtt
clientid HAhost // User with read access configured in cloudmqtt
#cleansession false
start_type automatic
username <user from the "instance info", "**remote_username" is outdated**>
password <pw from the "instance info", "**remote_password" is outdated**>

Sorry for this suggestion, you installed the “owntracks”-component in Home Assistant too?
Regards!

Hi @ehsahog, “remote_username” and “remote_password” gave me some trouble, aren’t they outdated?

username <user from the "instance info", "**remote_username" is outdated**>
password <pw from the "instance info", "**remote_password" is outdated**>

worked for me.
Regards!

Were you able to show the stats on HA.Got the bridge working but stuck on how to show values

@Vayain, Sorry, but I have no idea if they are outdated, my HA is 0.40.1 installed with AIO installer and it worked there.

HI @vageesh79,
did you install owntracks in HA too?
After subscribing to all MQTT-topics via “topic # in” and restarting HA, my devices showed up.

I have owntracks in configured in device_tracker.yaml file.

- platform: owntracks
  max_gps_accuracy: 75
  waypoints: yes

and my cloudmqtt.conf look like this -

connection cloudmqtt
address m11.cloudmqtt.com:1111
remote_username xxx
remote_password xxx
clientid xxx
try_private false
start_type automatic
topic # in

and i am getting requests from owntrack from android with this command -

mosquitto_sub -h m11.cloudmqtt.com -p xxx -u xxx -P xxx -t ‘#’ -v

On HA i have configured sensor for battery as -

- platform: mqtt
  state_topic: 'owntracks/xxxh/Nx'
  name: Battery N4
  unit_of_measurement: "%"
  value_template: '{{ value_json.batt }}' 

but no value. I can see the value if run from ssh.

I have local broker also and is working fine… so i bridged cloudmqtt.conf. Am i missing something

I finally got this working after following this post and some others. There are a few things that need to be done for it to work end-to-end.

  1. In /etc/mosquitto/mosquitto.conf file add

include_dir /etc/mosquitto/conf.d

  1. in /etc/mosquitto/conf.d directory create a file as per the OP

connection cloudmqtt
address <host:port>
remote_username <>
remote_password <>
clientid <>
try_private false
start_type automatic
topic # in

  1. Your device_tracker config should look like his

device_tracker:

  • platform: mqtt
    devices:
    paulus_oneplus: /location/paulus
    annetherese_n4: /location/annetherese

from: MQTT device tracker - Home Assistant

What I also did was to create a bridge on cloudmqtt to my local mosquitto install.

First I created a new user in mosquitto by doing

sudo mosquitto_passwd /etc/mosquitto/pwfile username

Go to the Cloudmqtt console and create a bridge to your local mosquitto.

url: mqtt://USER:PASSWORD@host:port
Direction: out
Local Prefix: owntracks/#
Remote Prefix: owntracks/#

Obviously I had to portforward in the router to the local mosquitto port.

Hope this helps

Mukund

1 Like

You can create bridge on your local MQTT Server and you do bot need port forwarding for it. Connection initiator will be local MQTT server not CloudMQTT

1 Like

Didn’t work for me, so I did the other way. I’ll try disabling the bridge from cloudmqtt and see if the connections stay.

If you don’t configure bridge on local MQTT connection wil be lost.
Here is my config for brdging mosquitto with cloudmqtt

connection couldmqtt
  address <your cloudmqtt account>
  topic # in 1
  try_private true
  notifications false
  start_type automatic
  remote_clientid <your client id>
  remote_username <your user name>
  remote_password <your password>
  keepalive_interval 300
  cleansession true
  bridge_protocol_version mqttv311
  local_clientid hass
  bridge_cafile /etc/ssl/certs/ca-certificates.crt
  bridge_insecure false
3 Likes