Send SMS with USB GSM modem when alarm triggered

I wanted home assistant to be able to send SMS alarm messages from a USB stick in case the internet was off-line. I bought a Huawei E169 gsm stick, installed a program called chat with: “sudo apt-get install ppp”.

A simple bash script in /home/homeassistant/.homeassistant:

#!/bin/sh
chat TIMEOUT 1 “” "AT+CMGS=“0123456789"” “OK” > /dev/ttyUSB0
chat TIMEOUT 1 “” “$1” “OK” > /dev/ttyUSB0
chat TIMEOUT 1 “” “^Z” “OK” > /dev/ttyUSB0

Where the 0123456789 is the phone number the message needs to be sent to.

In configuration.yaml:
shell_command:
send_sms: ~/.homeassistant/sendSMS.sh "Alarm triggered"
send_panicsms: ~/.homeassistant/sendSMS.sh “PANIC button pressed”

Can send sms from automation.yaml with the command:
- service: shell_command.send_sms
or:
- service: shell_command.send_panicsms

8 Likes

How to do that in Hass.io??

Sorry, I am not familiar with hasso. I just installed homeassistant on a rpi 3 in a virtualenv.

Ok, thank you!

You all never cease to amaze me. Really cool

It turned out that I had made a mistake with my script. It would only work if I had first established a screen terminal session with the usb GSM stick before sending SMS from home assistant. Apparently the screen program sets the baud serial port settings needed for the GSM stick.

Here is the correct version for the sendSMS.sh bash script:

#!/bin/bash

read count < smsCounter.txt

stty -F /dev/ttyUSB0 9600 min 100 time 2 -hupcl brkint ignpar -opost -onlcr -isig -icanon -echo

chat TIMEOUT 1 "" "AT+CMGF=1" "OK" > /dev/ttyUSB0
chat TIMEOUT 1 "" "AT+CMGS=\"0123456789\"" "OK" > /dev/ttyUSB0
chat TIMEOUT 1 "" "$1" "OK" > /dev/ttyUSB0
chat TIMEOUT 1 "" "^Z" "OK" > /dev/ttyUSB0

sleep 2

chat TIMEOUT 1 "" "AT+CMGS=\"0987654321\"" "OK" > /dev/ttyUSB0
chat TIMEOUT 1 "" "$1" "OK" > /dev/ttyUSB0
chat TIMEOUT 1 "" "^Z" "OK" > /dev/ttyUSB0

let 'count++'
echo $count > smsCounter.txt

You can test the script from the command line as follows:
/sendSMS.sh “Hello world from GSM stick”

1 Like

Not so perfect after all: if I start home assistant from a command line with: sudo -u homeassistant -H /srv/homeassistant/bin/hass, the sms messages will be sent.

But if I start home assistant with sudo systemctl start [email protected], no message gets sent.

It turns out that the command I use (chat) is not suitable for this type of script. I have now a new script that works fine, using a program called “gammu”.

Here is the new version:

#!/bin/bash

read count < /home/homeassistant/.homeassistant/smsCounter.txt

gammu sendsms TEXT 0123456789 -text "$1"

let 'count++'
echo $count > /home/homeassistant/.homeassistant/smsCounter.

For some reason, the script is best placed in /usr/bin, because (at least my) home assistant has trouble calling bash scripts from other directories.

Hi,

Sorry to raise an old thread, but I was wondering if you had considered making an official Hassio Add-on for Gammu?

I am trying to receive sms notifications into HA to trigger various automation. I am looking to use GAMMU on a separate RPI, but was thinking if anyone knew how to create an add=-on that would be awesome.

1 Like

Hi,

As an alternative, have a look at https://github.com/1technophile/OpenMQTTGateway with an A6/A7 GSM module and Arduino Uno/Mega or ESP32/8266.

It is able to both receive and transmit SMS and pass them over MQTT.

While that looks awesome, I wanted to use my existing spare RPI3… for those of you that want to know how to do this, I managed to fire a webhook into HA (and therefore trigger my automations) on receipt of an SMS using this method:

I have successfully recreated the script on Rasppian.

I am using this stick from amazon, but I had to unlock it using this guide.

I am going to build a notification component for hassio that can talk directly to the serial port so it integrates naturally with Home Assistant.

I have no idea how to make a Hassio Add-on. I use home assistant, installed in a python virtual environment. But using the script is fairly easy and straightforward in a normal linux environment. Not sure about Hassio though.

Gammu seems overkill just to send notifications (and possibly react to notifications)

would you mind posting code or some idiot proof how to’s?

Not at all, I am at work currently, I will try on hassio to make sure it works.
In hassbian you may need to add homeassistant to the dialout group:

sudo adduser homeassistant dialout

stty is available in hass.io but chat is not available in hass.io, it comes from the ppp package.
Do you know how to modify hass.io to install the ppp package?

I suspect that the best way to support hassio is to replace the script and chat with a home assistant notification component built on top of pyserial

This should not be hard for an expert in python (which I am not, but willing to learn)

You can install chat in hass.io doing:

apk add ppp

So I figure out how to fix the error “Can’t get terminal parameters: Inappropriate ioctl for device” discussed here

Turns out that chat is trying to read the response from STDIN, but in the context of Home Assistant this is incorrect as STDIN contains the message we want to send. It is also incorrect in that chat wont be able to verify if we got the correct response from the modem (e.g.: “OK” )

Here is a working script that can be executed from home-assistant:

#!/bin/bash

DEST=$1
# Modify the MD variable to match your hardware configuration
MD="/dev/ttyUSB2"

if [ -z "$2" ]
then
    read message
else
    message="$2"
fi

echo "Sending $message to $DEST"

stty -F $MD 9600 min 100 time 2 -hupcl brkint ignpar -opost -onlcr -isig -icanon -echo

chat TIMEOUT 10 "" "AT+CMGF=1" "OK" > $MD < $MD
chat TIMEOUT 10 "" "AT+CMGS=\"$DEST\"" "OK" > $MD < $MD
chat TIMEOUT 10 "" "$message^Z" "OK" > $MD < $MD

In home assistant you need to configure your yaml notification as:

notify:
  - platform: command_line
    name: sms_person1
    command: !secret notify_sms_person1
  - platform: command_line
    name: sms_person2
    command: !secret notify_sms_person2

And the screts yaml:

notify_sms_person1: "/home/homeassistant/.homeassistant/send_sms.sh +1NNNNNNNNNN"
notify_sms_person2: "/home/homeassistant/.homeassistant/send_sms.sh +1NNNNNNNNNN"

Replace the NNN for the actual phone numbers

Here is my commit in my config.

1 Like

this wont work for me because australia doesnt have a 2G network :frowning: its so hard to find affordable 3G / 4G modules or gps trackers…