Select Page

While building a dial plan you will always run in scenario where you have to choose the action based on a if statement. In this example we can use a counter variable and based on the value of the variable we can make another decision.  Lets start with normal counter variable and use that in a conditional statement in asterisk.

[Counter] exten => start,1,Verbose(2,Increment the counter variable)
; Set the initial value of the variable
same => n,Set(localCounter=1)
same => n,Verbose(2,Current value of localCounter is: \${ localCounter })
; Now lets increment the value of localCounter
same => n,Set(localCounter =\$[\${ localCounter } + 1])
same => n,Verbose(2,New counter is: \${localCounter })
same => n,Hangup()

You can also use int() aka increment function

[Counter] exten => start,1,Verbose(2,Increment the counter variable)
; Set the inital value of the variable
same => n,Set(localCounter =1)
same => n,Verbose(2,Current value of localCounter is: \${ localCounter })
; Now we can increment the value of localCounter
same => n,Set(localCounter =\${INC(localCounter)})
same => n,Verbose(2,New counter is: \${localCounter })
same => n,Hangup()

Now we have a variable, lets us a if condition

[Counter]

exten => start,1,Verbose(2,Increment the counter variable)
; Set the inital value of the variable
same => n,Set(localCounter=1)
same => n,Verbose(2,Current value of localCounter is: \${ localCounter })
; Lets Here we use the RAND() function to randomly decide whether we should increment
; the localCounter. Use 0 as false and 1 as true
same => n,Set(IncrementValue=\${RAND(0,1)})
; Now we can increment the value of localCounter if IncrementValue returns 1
same => n,Set(localCounter=\${IF(\$[\${IncrementValue} = 1]?
\${INC(localCounter)}:\${localCounter})
same => n,Verbose(2,Our IncrementValue returned: \${IncrementValue})
same => n,Verbose(2,Our new value of localCounter is: \${ localCounter })
same => n,Hangup()

When you start building a complex dial plan, you will find incrementing and decrementing a variable in asterisk is very useful tool. Int() or dec() for decrementing.
Usage is

INC(variablename)
DEC(variablename)

If() syntax
\${IF(\$[…conditional statement…]?true_return_value:false_return_value)}