Free SMS notifications via MQTT

I have found so much help and valuable information from other users posting in these forums! Thanks to all you contributors for that. Here’s my little effort to give something back to the community…

HA provides a lot of value when it can dynamically notify you of any important events happening in your home. There are many methods of remote notification available but I prefer SMS. There are a few SMS brokers that HA already integrates with like Twilio, ClickSend, MessageBird, etc but they all cost money to use and that can add up. Besides, I like free :wink:

I’ve put together a method for sending myself an SMS notification from HA through MQTT that’s free. I don’t know if my method is unique here or others have figured it out too, but I haven’t seen it on any forums here, so I hope it is of some value to others too. Here’s how I do it…

You will be publishing an mqtt message to a mqtt broker via your scripts/automatons that will ultimately end up being sent out via SMS. If you aren’t already using mqtt in your environment, you will need to set that up first. To use my notification method, you will need to use a local mosquitto mqtt broker. Here’s how to do that…

Once your local mosquitto mqtt broker is installed, configured, and tested create a topic on your local mosquitto mqtt server e.g. home/smsmessage. Test writing and reading the topic with a good mqtt client like MQTT.fx.

Head over to gmail .com and create a free gmail (google) account if you don’t have one. I suggest having a separate google account for these kinds of integrations anyway, instead of using your personal email one. Associate your mobile number with your gmail (google) account. This will happen during the new gmail account verification process.

Head over to https://accounts.adafruit.com/users/sign_up
Use your new gmail (google) account to create a free account on Adafruit. It takes a little reading to figure out the Adafruit platform if you’re not familiar with it…
https://learn.adafruit.com/adafruit-io-basics-dashboards/overview

Ultimately, Adafruit has a lot of capabilities, all for free, but we just need it to work as a public mqtt broker for us. So…

  • Create a topic (feed) on Adafruit called “smsmessage”
  • Make note of your adafruit user name and adafruit IO key.
  • Now the full topic on adafruit mqtt server is: ‘your user name’/feeds/smsmessage

If you didn’t use mqtt for anything else in your environment, you could skip setting up your own local mosquitto broker and we could just write our notification message directly to the Adafruit mqtt broker, however, there are so many uses for a local mqtt broker that you should have one anyway :wink:

As Home Assistant can only use one mqtt broker at a time, we want to configure our local mqtt broker to mirror published messages on a given topic to the Adafruit mqtt broker. This is called bridging in mqtt speak. MQTT bridging is very powerful in that it creates a persistent two way communication channel from your internal environment to the outside world without having to open any firewall ports, or do any goofy port forwarding schemes, etc.

To bridge your local mqtt server topic home/smsmessage that you created earlier, to the adafruit topic /feeds/smsmessage that you created on adafruit we are going to modify your mosquitto mqtt broker configuration file in /etc/mosquitto/conf.d/mosquitto.conf

Add the following to your mosquitto conf file and restart the mosquitto service.

connection adafruit
address io.adafruit.com:1883
remote_username <your adafruit.io username>
remote_password <your adafruit.io IO key>
start_type automatic
bridge_protocol_version mqttv311
notifications false
try_private false
topic "" both 0 home/smsmessage <your adafruit.io user name>/feeds/smsmessage

I’m not getting into all the nuts and bolts of mosquitto configuration here. I will just tell you that this configuration works and creates a two way channel between your local mosquitto broker topic and the public adafruit broker topic. If anything is published to either the local mosquitto broker topic, or the public adafruit topic, it is immediately reflected in the other. That’s very powerful if you think about all the possibilities of that capability!

Ok, so how does the SMS happen you ask? Getting there…

Head over to ifttt .com and create a new account using your new gmail account you created earlier. IFTTT .com is an amazing free automation platform that allows you to create a million different
“if this happens, then do that” scenarios that are very useful indeed. If you’re not familiar with
the platform, go here:

Spend some time exploring all the services on ifttt…it has a huge number of services that are great for all kinds of home automation integrations.

Once you learn how to create an applet on ifttt, which is not difficult at all, then you’re going to create an applet that uses the Adafruit service (trigger one) as the IF part of the applet and the SMS service as the THEN part of the applet.

Because you have used the same gmail account to create accounts on both adafruit and ifttt then the accounts are all linked and they share information between them. You will find that on ifttt when you create the applet, and select the Adafruit as the IF service, it already knows about your “smsmessage” mqtt topic on the adafruit platform that you created, and the SMS service already knows your mobile phone number. Very handy and cool.

Now, whenever you publish a message to the mqtt topic home/smsmessage on your local mqtt server, it gets bridged to the /feeds/smsmessage topic on the adafruit mqtt server, which triggers ifttt to send you an sms with the message. I realize there are a few steps here, and new accounts to set up, and new platforms to get familiar with, but it’s well worth the time to learn the capabilities of mqtt, the adafruit platform, and especially the ifttt platform. They
all open up a world of possibilities as you learn them! And, it’s all free, and I have found that from the time this script is run, it takes about 15 seconds for me to get the SMS message on my phone.

Note: The ifttt SMS service only allows for up to 100 SMS messages per month for free. That should be more than sufficient if only sending out important alerts, or a daily status, or something like that.

In your HA platform, you could now use a script to publish a mqtt message to your local mosquitto mqtt broker. I use something like this in my scripts.yaml file. Just set the payload to whatever you need the sms message to say.

mqtt_sms_notify:
    alias: MQTT SMS Alert
    sequence:
      - service: mqtt.publish
        data_template:
          topic: 'home/smsmessage'
          payload: 'Alert from Home Assistant.'

EDIT: If the number of SMS messages per month permitted by IFTTT is a problem, there is also a “Notify” service on IFTTT that you can use instead of the SMS service. The Notify service will send a push notification to your phone/mobile device if you install the IFTTT app on it from the app stores. I don’t believe there is any limit on the number of push notifications you can receive through that method.

I hope this setup example helps other HA users learn some new things and enjoy having some free SMS too!
Brian

3 Likes

Wouldn’t all traffic between the private and public MQTT brokers be transmitted in the clear?

I believe you’d need to secure the connection to ensure MQTT payloads aren’t (easily) readable.

PS
The cap of 100 messages/month is for USA and Canada. It’s 10/month elsewhere.

Yes, 123, the above example of the mqtt bridge is not encrypted for the sake of simplicity. Certainly, if users are concerned about the privacy of their notification message, then they should take the next steps and also configure security as per something like the video you linked.