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
Configuring:
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.
#!/bin/bash
# Consume all variables sent by Asterisk
while read VAR && [ -n ${VAR} ] ; do : ; done
# Answer the call.
echo "ANSWER"
read RESPONSE
url=http://192.168.0.10:8123/api/services/$1
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
[internal]
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()
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.
#!/bin/bash
[[ $# < 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 ;;
esac
case $2 in
0) service="light/turn_off" ;;
[1-8]) let lightlevel="($2 * 32) - 1";
service="light/turn_on";
data=',"brightness":"'"$lightlevel"'"' ;;
9) let lightlevel="(8 * 32) - 1";
service="light/turn_on";
data=',"brightness":"'"$lightlevel"'"' ;;
*) echo "Enter the light range from 0 to 8. 9 will be the same as 8." ;;
esac
json='{"entity_id":"'"$entity_id"'"'"$data"'}'
/usr/bin/curl -H "Content-Type: application/json" \
--cacert /etc/ssl/certs/GraysonPeddieCA.crt \
-X POST -d $json https://172.21.0.1:8123/api/services/$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.
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?
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