Skip to content

Asterisk Integration

corb555 edited this page Jan 24, 2015 · 1 revision


In some cases it is very useful to make call routing decisions in Asterisk based on openHAB Items states. As an example, if nobody is home (away mode is on) route my doorphone calls to mobile, in other case route them to local phones inside the house. To do that AGI (Asterisk application gateway interface) can be used to obtain Item state value into an Asterisk variable and then a routing decision can be performed based on this variable value. Here is a small python script which, when called from Asterisk AGI makes an http request to openHAB REST API, gets specific item state and puts it into specified Asterisk variable:

    import sys,os,datetime
    import httplib
    import base64
    def send(data):
            sys.stdout.write("%s \n"%data)
    env = ""
    while(env != "\n"):
            env = sys.stdin.readline()
            envdata =  env.split(":")
            if len(envdata)==2:
    username = AGIENV['agi_arg_1']
    password = AGIENV['agi_arg_2']
    item = AGIENV['agi_arg_3']
    varname = AGIENV['agi_arg_4']
    auth = base64.encodestring('%s:%s' % (username, password)).replace('\n', '')
    headers = {"Authorization" : "Basic %s" % auth}
    conn = httplib.HTTPConnection("localhost", 8080)
    conn.request('GET', "/rest/items/%s/state"%item, "", headers)
    response = conn.getresponse()
    item_state =
    send("SET VARIABLE %s %s"%(varname, item_state))

In Asterisk dialplan (extensions.conf) this AGI script is used in the following way:

exten => 1000,1,Answer()
exten => 1000,n,AGI(openhabitem.agi, "asterisk", "password", "Presence", "atHome")
exten => 1000,n,GotoIf($["${atHome}" == "ON"]?athome:away)
exten => 1000,n(athome),Playback(hello-world) ; do whatever you need if Presence is ON
exten => 1000,n,Hangup()
exten => 1000,n(away),Playback(beep) ; do whatever you need if Presence is OFF
exten => 1000,n,Hangup()

In AGI call arguments are:

  • the script name itself
  • openhab username
  • openhab password
  • openhab Item name
  • Asterisk variable to put state to

feature overview


Setup intro

Linux and OS X





User Interfaces



(link to openHAB forum)



Application Integration



A good source of inspiration and tips from users gathered over the years. Be aware that things may have changed since they were written and some examples might not work correctly.

Please update the wiki if you do come across any out of date information.

Binding configurations

Use case examples

Item definition examples

Sitemap definition examples

Collections of Rules on a single page

Single Rules


Syntax highlighting for external editors

Release Notes

Clone this wiki locally
You can’t perform that action at this time.