Easily Send Home-Assistant Notifications using Whatsapp

Home-Assistant Whatsapp

I have been using Home-Assistant for a long time, and was looking for the best way to send Notification based in Event Triggers. I know, you’ll probably Thinking, Hey… you have Pushbulet, Facebook Messenger or even telegram. well, you partially right. we do have this options but my goal was to get notifications using the apps i’m already using.

In this article i’ll explain how to use your whatsapp to get notification from Home-Assistant without have to install Parallel or even get a new number.
actualy it will be very easy thing to do.

Let’s start…

Step 1 – Opening Twilio Account:

in order to open Twilio account go to this address:
https://www.twilio.com/ and click on Sign up

רישום ל Twilio

Enter your personal details and click Get Started

Twilio Signup account details

now, you will be asked to enter your Phone number for verification.
enter it and click on Verify

Twilio Verify Phone Number

After a few moment you will receive an sms with confirmation code,
enter it and hit submit.

Step 2 – Create Twilio app for our notifications

So now, after we created the Twilio app we need to create an Application that will be use for sending the notification. select SMS chatbot to continue.

Twilio Chatbot

Now, give your project a name (‘Home Assistant’ etc.)

In the next step you will be asked to invite team members, no need to do that. just hit “skip this Step”.

Twilio Invite Team Members

Next, you’ll get a FlowChart, ignore it and click on “Programmable SMS” on the left side of the page.

Twilio Programmable SMS

And then hit the Whatsapp Application.

Now you’ll get a message that explains about this service and will be asked to confirm whatrsapp Terms of service. check the I agree box and hit Confirm To continue.

In the next step you’ll get a Whatsapp number to send message to. in order to activate the Sendbox you have to send the Bolded text to this number.

you can see that Twilio got your message:

and will also get a confirmation message back in your Whatsapp application.

Part 3 – Impimenting Twilio in Home Assistant

So now, the only thing left to do is integraing Twilio Whatsapp and using it to get Whatsapp notification from home assistant.

for doing that we need to get the Account SID and Auth token from Twilio account. go to user setting in the Upper Right corner of the page.

Now click on “Console” on the menu, then you will be able to see your sid and token, write it down.

Wilio Auth and sid

Now, go to your Home Assistant configuration file and add the following code:

twilio:
  account_sid: [your account sid]
  auth_token: [your account auth token]

and Create a new folder and name it”custom_components” and under it crerate another one named”notify”.

create new file within notify and call it ”
twilio_whatsapp.py”

"""
Twilio Whatsapp platform for notify component.

For more details about this platform, please refer to the documentation at

"""
import logging

import voluptuous as vol

from homeassistant.components.twilio import DATA_TWILIO
import homeassistant.helpers.config_validation as cv
from homeassistant.components.notify import (
    ATTR_TARGET, PLATFORM_SCHEMA, BaseNotificationService)

_LOGGER = logging.getLogger(__name__)
DEPENDENCIES = ["twilio"]


CONF_FROM_NUMBER = "from_number"

PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
    vol.Required(CONF_FROM_NUMBER):
        vol.All(cv.string),
    
})


def get_service(hass, config, discovery_info=None):
    """Get the Twilio Whatsapp notification service."""
    return TwilioWhatsappNotificationService(
        hass.data[DATA_TWILIO], config[CONF_FROM_NUMBER])


class TwilioWhatsappNotificationService(BaseNotificationService):
    """Implement the notification service for the Twilio Whatsapp service."""

    def __init__(self, twilio_client, from_number):
        """Initialize the service."""
        self.client = twilio_client
        self.from_number = from_number

    def send_message(self, message="", **kwargs):
        """Send Whatsapp to specified target user cell."""
        targets = kwargs.get(ATTR_TARGET)

        if not targets:
            _LOGGER.info("At least 1 target is required")
            return

        for target in targets:
            self.client.messages.create(
                to='whatsapp:'+target, body=message, from_=self.from_number)
                
            

and the for final configuration step add the following lines to your Configuration.yam file:

notify:
  - name: Home-Assistant
    platform: twilio_whatsapp
    from_number: whatsapp:+[the number you sent the initial message to]

Save it and restart Home Assistant.

Part 4 – Configuration test

Now, browse to Home Assistant and under Devloper tools hit Services

You should see a service calld “notify.home_assistant”

Under Data field add the following text (be sure to replace the target with your phone number including country code (“+1-77555-66” etc).

{"message":"Home Assistant Test Notification","target":"[your phone number include country code]"}

if you did everything correctly you should recive Whatsapp message with your message.

3 Comments

  1. Great find, will follow along this weekend :). Could this be set-up to send a message to more than one phone number? Thank you so much!

Leave a Reply

Your email address will not be published.


*