How to send calls to different trunks at different times of day

Some users have a situation where a particular trunk may offer least-cost calling during the daytime hours, while another trunk may offer better rates at night. Here's a method to select trunks based on time of day. Note that you cannot use a Time Condition for this because Time Conditions are currently designed for use with incoming calls only.

Step 1: Create a new context in extensions_custom.conf - it should look something like this:

[custom-timeselect]
exten => _[*0-9]!,1,GotoIfTime(08:00-05:00|*|*|*?dayselect)
exten => _[*0-9]!,n,Dial(SIP/nighttrunk/${EXTEN})
exten => _[*0-9]!,n,Goto(app-blackhole,congestion,1)
exten => _[*0-9]!,n(dayselect),Dial(SIP/daytrunk/${EXTEN})
exten => _[*0-9]!,n,Goto(app-blackhole,congestion,1)
exten => h,1,Hangup()

See this page at voip-info.org for an explanation of how to set the time in the GotoIfTime statement (and note that if you are using Asterisk 1.6 or later, you must replace the | separators with commas) - or if you get stuck, you can always create a dummy Time Condition in FreePBX (so you can use the GUI to specify your time condition settings), save it and save changes, and then open extensions_additional.conf with a text viewer or editor and search for the [timeconditions] context header - you will see how FreePBX would construct the GotoIfTime statement according to the parameters you've entered, and you can copy that into your custom-timeselect context (then you can delete the dummy Time Condition).

Replace daytrunk and nighttrunk with the actual names of trunks you want to use for day and night calls, as shown in your trunks list. Replace SIP with whatever technology is actually used by the trunk (such as IAX2, etc.), if it's not SIP.

Step 2: Create a CUSTOM trunk. In the Dial text box, enter this:

Local/$OUTNUM$@custom-timeselect

Step 3: Set up your Outbound Route to use the custom trunk rather than calling either of the two trunks directly.

That's all there is to it. Maybe someday, someone will create an "Time Condition Trunk" module, which would create a pseudo-trunk that appears in the Outbound Route list of selectable trunks, that would let you set a time condition and based on that condition would go to either (actual) Trunk A or (actual) Trunk B. But until then, just use the above method to accomplish the same thing.