Turning off lights with a phone using asterisk to define extensions

Asterisk is software to act as a pbx.
By configuring the dial plan you can create extensions that call scripts.
The below is a quick example that just uses extensions.
Asterisk supports creating interactive menu’s but that is not covered by this example.

User experience:
Pick up phone
Dial 700
Living room lights turn on

Asterisk is a powerful system but getting a configuration working take a little effort.
The Asterisk server runs fine on a raspberry pi.
I would recommend getting hardware sip phones if you can.

We want to set up an extension so when you pick up your phone and dial something happens.
This is done in extensions.conf where we want to create a rule when 700 is dialled.
This will call an AGI based shell script to do the work.

First lets create the shell script home-assistant.sh to call the web service api using curl.
AGI scripts use the standard ouptut to send commands to asterisk and standard in for numbers pressed on the phone.
Asterisk will first send some environment variables before processing standard in and standard out.
Our script is ignoring this but the power is there if you want to build your own menu’s.
The script takes 2 arguments, the service you want to call and a json string where you would put the entity id.

# Consume all variables sent by Asterisk
while read VAR && [ -n ${VAR} ] ; do : ; done

# Answer the call.
echo "ANSWER"
curl -X POST -H "X-HA-Access: your_api_key" -H "Content-Type: application/json" --data "$2"  $url 1>/dev/null 2> /dev/null

echo "HANGUP"

exit 0

In extensions.conf and add the following to call the shell script.
The arguments to the script are the service (light/turn_on) and the json blob containing the entity to toggle.
Note we need to escape the " characters so we have to use a slash

exten => 700,1,AGI(/etc/asterisk/home-assistant.sh,"light/turn_on","{\"entity_id\":\"light.living_room\"}")
same => n, Hangup()
exten => 701,1,AGI(/etc/asterisk/home-assistant.sh,"light/turn_off","{\"entity_id\":\"light.living_room\"}")
same => n, Hangup()

Very clever, thank you.

Thanks for this. What else do you do with your HA/Asterisk setup?

Currently I’m mainly doing this to switch off lights from one place.
I have a sip phone next to my bed and have phone book entries to switch on and off lights in each room.

Here are some idea’s that I have not implemented.
You can do text to speech with asterisk so it should be straight forward to get the status of sensors.

You could also expose this to external phone calls.
Ring up your home phone number.
If you are on a white list of phone numbers go to a menu system.
Press 1 to ring phone.
Press 2 to go to the lights menu
Press 3 to get status of sensors.

1 Like

I have done a similar project but with a different route that sends the two numbers to a script.

Watch the video:

(I realized the video is not that clear as I didn’t take the time to set the focus in my smartphone. Oh, well. At least I can get my point across.)


exten => _4277XX,1,Set(light=${EXTEN:4:1})
exten => _4277XX,n,Set(level=${EXTEN:5:1})
exten => _4277XX,n,System(/usr/local/bin/hassjson ${light} ${level})


[[ $# < 2 ]] && echo "Specify the light number and intensity (0-9)" && exit 2
case $1 in
    1) entity_id="light.lr_flamps" ;;
    2) entity_id="light.br_tlamps" ;;
    *) echo "Invalid light number"; exit 2 ;;
case $2 in
    0) service="light/turn_off" ;;
    [1-8]) let lightlevel="($2 * 32) - 1";
           data=',"brightness":"'"$lightlevel"'"' ;;
    9) let lightlevel="(8 * 32) - 1";
           data=',"brightness":"'"$lightlevel"'"' ;;
    *) echo "Enter the light range from 0 to 8. 9 will be the same as 8." ;;
/usr/bin/curl -H "Content-Type: application/json" \
    --cacert /etc/ssl/certs/GraysonPeddieCA.crt \
    -X POST -d $json$service > /dev/null

And that’s that!

Update as of 2022/07/15 14:36 Eastern Time: I wanted to replace the YouTube video with an Odysee video. I only want to upload new videos to Odysee only. I could delete my YouTube channel anytime without notice. Thanks for the understanding.


This is awesome!
Thanks for the info, it took a bit of re-configuring to get it to work, but I wound up getting the hang of it after a few hours.

Hello. I’m still very new to this topic, so could someone explain in detail how to turn on my xiaomi robot vacuum cleaner with asterisk and home assistant?

Hi, scroll up, an example script is posted, it’s for a light , but it’s the same logic

Does not work! Do you need to put something in place of “your_api_key”?

In home-assistant.sh file?

can someone help me please? I’m still a very beginner!:sob:

You need to change the command, API key doesn’t work anymore , use a bearer token

And how? I do not understand😩

Do a search on forum, should be plenty examples where bearer token is used in a curl

1 Like

In asterisk, there is also a feature mode, you can define custom commands there, that can be used as DTMF during a call… You can turn something on by pressing a number , it’s unlimited in use

Okay, Thanks!:slightly_smiling_face: