Alternative Howto for using Whatsapp notifications sent from a linux host

The following provides instructions to allow HA to send notifications via whatsapp. I use this approach on a supervised install of HA where I have direct access to the host OS command prompt. I’ve seen a couple other ways people access whatsapp for notification. Based on my background I felt this was the easiest way for me to get the notifications going. It also make it easy to switch from whatsapp to some other notification method like textbelt should I choose to do so. In response to the typical question as to why not just use HA integrated notification my answer is this provides a disconnected way to get notification. If I’m not using Naba Casa or a VPN I can still get real time notification from a disconnected HA system.

The first thing you need to do is install the latest version of node js at the host OS level command prompt. The following instruction should do the job on a Debian 11 system.

sudo apt-get update && sudo apt-get install -y ca-certificates curl gnupg
sudo mkdir /etc/apt/keyrings
curl -fsSL https://deb.nodesource.com/gpgkey/nodesource-repo.gpg.key | sudo gpg --dearmor -o /etc/apt/keyrings/nodesource.gpg
NODE_MAJOR=20
echo "deb [signed-by=/etc/apt/keyrings/nodesource.gpg] https://deb.nodesource.com/node_$NODE_MAJOR.x nodistro main" | sudo tee /etc/apt/sources.list.d/nodesource.list
sudo apt-get update && sudo apt-get install nodejs -y

Next you want to link your linux system command tool npx mudslide" line with your whasapp account. At the host OS level command prompt run the following:

npx mudslide login

This generates a QRC code in the command prompt window.
You need to bring up the whatsapp app on your phone.
Click the three dot stack in up right hand side, click settings.
On line with your name, to the right hand side you’ll see a QRC icon, click on it.
Click on the Scan code tab.
Scan the QRC code with the camera. You’ll get a popup asking about linking the device. Then you have to scan the QRC code again. At which point you should have things linked up.

Back in the OS terminal window you should get a success Logged in” message. You can then hit any key in the terminal to exit npx.

Try to send a text in the OS command prompt.

npx mudslide send me 'A test text message from my linked account to my phone’

This should show up in whatsapp on your phone as coming from yourself. If that works send a message to another phone with whatsapp.

npx mudslide send PhoneNumber "A friendly test message"

You replace PhoneNumber with the phone number the message is being sent to. In the US this would be +1 followed by your 10 digit phone number.

Once you have the OS level command line stuff working with whatsapp you need to set things up so HA can access it.
First add the file /usr/share/hassio/homeassistant/os-support-apps/local_whatsapp_ctrl on your HA with the following contents


#!/bin/bash
#exec 0<&- 1> >(logger -t $(basename $0) >/dev/null 2>&1) 2>&1
#echo Received: $@
if [ $2 == "send_whatsapp_msg" ]; then
  text="${@:4}"
  npx mudslide send +1$3 \'"$text"\' >> tmp.out
else
  echo Request $2 is not known
fi

This file is the host based shell command that will handle HA ssh request to send a whatsapp message.
You need to make this file executable via:

sudo chmod 755 /usr/share/hassio/homeassistant/os-support-apps/local_whatsapp_ctrl

You need to create a second shell command that is directly accessed via HA in the container. Create the file /usr/share/hassio/homeassistant/shell_cmds/send_whatsapp_msg with the following contents:

#!/bin/bash
text="${@:2}"
ssh -i /config/.ssh/id_rsa -o StrictHostKeyChecking=no USER@IPADDRESS /usr/share/hassio/homeassistant/os-support-apps/local_whatsapp_ctrl send_whatsapp_msg $1 $text

USER is the host level user id you just added npx mudslide login for. IPADDRESS is the external ip address of your host machine running HA.

This command script uses shared keys to allow the HA containerized application to run the command within the host level user account. If you haven’t already created a key pair within HA you’ll need create the key pair. The easiest way to create the key pair is to add the HA “Terminal & SSH” addon from the addon store. Once added you click on the addon and select the “OPEN WEB UI” link, which gives a command prompt. In the command prompt do the following:

cd /config
mkdir .ssh
cd .ssh
ssh-keygen -t rsa
ls -l
scp id_rsa.pub [USER@IPADDRESS](mailto:USER@IPADDRESS)

The ls -l is used to verify the files where created and are available. In the SCP command USER is the host level user you just linked to whatsapp with the “npx mudslide login” command. IPADDRESS is the external ip address of your host machine hosting the whatsapp access via ssh. The scp command places a copy of the public key in the home directory of your host user account.

Back at the host command prompt for USER@IPADDRESS you’ll need to add the id_rsa.pub key to your authorized key file ~/.ssh/authorized_keys. You’ll also need to make sure the permission of these files are set correctly. Do the following:

mkdir ~/.ssh
cat ~/id_rsa.pub >> ~/.ssh/authorized_keys
chmod 700 ~/.ssh
chmod 644 ~/.ssh/authorized_keys

You should modify the key we just added to the authorized_keys file by editing ~/.ssh/authorized_keys and modify the last line by adding the following to the start of the line. There will be a space after this text and the ssh-rsa that currently starts the line.

command="/usr/share/hassio/homeassistant/os-support-apps/local_whatsapp_ctrl $SSH_ORIGINAL_COMMAND",no-port-forwarding,no-x11-forwarding,no-agent-forwarding

This will only allow any one logging in with this key pair (HA) to execute the local_whatsapp_ctrl command we created above.

You need to make the shell command accessible within the HA. You do this by editing the HA configuration file /usr/share/hassio/homeassistant/configuration.yaml. You add a send_whatsapp_msg line to the shell_command section of the file. If you don’t already have shell_command then add it at the end of the file. It should look like this:

shell_command:
  send_whatsapp_msg: '/config/shell_cmds/send_whatsapp_msg {{ number }} {{ text }}'

At which point you need to restart HA so that it read’s this configuration file change.
In my my automations when I what to send notification I add something simular to the following as an automation action:

if:
  - condition: state
    entity_id: person.brian
    state: home
then:
  - service: notify.mobile_app_brian_pixel_5a
    data:
      message: Garage Door Closed
else:
  - service: shell_command.send_whatsapp_msg
    data_template:
       number: PHONENUMBER
       text: "{{ \"Garage Door Closed\" }}"

Here the notification goes directly to my pixel via HA notifications if I’m at home. When I’m away from home the notification is sent via whatsapp to my cell phone. PHONENUMBER is the 10 digit US phone number that will receive the alert.

At my house I have cell phone connected to HA via USB tethering that provide backup communications should my cable network go down. It’s also provides home phone service. Finally it provides a wall mounted screen to control HA. I linked this house phone to whatsapp. This way the whatsapp notification comes in from a different user to my personal cell phone with an associated audio alert.

A few links that I reference to get this going:

Nice tutorial.
I would suggest this be moved to Community Guides…
Then if things change over time Regular members can fix things.
It’s also the topic you look for detailed tutorials for things.

I change it to community guide as suggested.

1 Like