Help With FUNC_MATH

Home » Asterisk Users » Help With FUNC_MATH
Asterisk Users 9 Comments

Hi,

I have some dialplan code that is trying to convert 12 hour time with AM/PM
to 24 hour format. The code has something like this:
Exten =>
2,1,ExecIf(${MATH(${HOUR_SELECTED}<12)}?Set(HOUR_SELECTED=${MATH(${HOUR_SELECTED}+12,int)})) Earlier on in the dialplan HOUR_SELECTED is set to 12. When they press option 2 they are selecting PM. If the time is from 1PM to 11PM then I want to add 12 to the number (so if it’s 1 make it 13 etc.). When I run the above the logs show the result as false yet if the user sets HOUR_SELECTED to 12 then after this line of dialplan code it gets switched to 24. What am I doing wrong here? The exact DP code is: Exten => 2, 1, Noop(BEFORE CHECK HOUR_SELECTED is ${HOUR_SELECTED})
same => n, ExecIf(${MATH(${HOUR_SELECTED}<12)}?Set(HOUR_SELECTED=${MATH(${HOUR_SELECTED}+12,int)})) same => n, Noop(AFTER CHECK HOUR_SELECTED IS ${HOUR_SELECTED})

And the output of the logs is:
[Feb 13 10:46:18] VERBOSE[1580][C-00007bc6] pbx.c: Executing [2@am_pm_select:1]
NoOp(“SIP/204.145.219.31-000081c6”, “BEFORE CHECK HOUR_SELECTED is 12”) in new stack
[Feb 13 10:46:18] DEBUG[1580][C-00007bc6] pbx_variables.c: Result of
‘HOUR_SELECTED’ is ’12’
[Feb 13 10:46:18] DEBUG[1580][C-00007bc6] pbx_variables.c: Function MATH(12<12) result is 'FALSE' [Feb 13 10:46:18] DEBUG[1580][C-00007bc6] pbx_variables.c: Result of 'HOUR_SELECTED' is '12' [Feb 13 10:46:18] DEBUG[1580][C-00007bc6] pbx_variables.c: Function MATH(12+12,int) result is '24' [Feb 13 10:46:18] DEBUG[1580][C-00007bc6] pbx.c: Launching 'ExecIf' [Feb 13 10:46:18] VERBOSE[1580][C-00007bc6] pbx.c: Executing [2@am_pm_select:2] ExecIf("SIP/204.145.219.31-000081c6", "FALSE?Set(HOUR_SELECTED$)") in new stack [Feb 13 10:46:18] DEBUG[1580][C-00007bc6] pbx_variables.c: Result of 'HOUR_SELECTED' is '24' [Feb 13 10:46:18] DEBUG[1580][C-00007bc6] pbx.c: Launching 'NoOp' [Feb 13 10:46:18] VERBOSE[1580][C-00007bc6] pbx.c: Executing [2@am_pm_select:3] NoOp("SIP/204.145.219.31-000081c6", "AFTER CHECK HOUR_SELECTED IS 24") in new stack TIA. Dovid

9 thoughts on - Help With FUNC_MATH

  • John,

    From looking at the wiki won’t STRFIME just give me what I need based on the unix time that I put in? What I am actually looking to do is convert over from 12 hour format to 24 (unless strftime does just that and I don’t kow what am I am doing?).

  • Is HOUR_SELECTED a floating-point number (e.g. 11.99999999999)? If so, you need to account for that in your comparison.

    –Don

    From: asterisk-users [mailto:asterisk-users-bounces@lists.digium.com] On Behalf Of Dovid Bender Sent: Thursday, February 13, 2020 4:47 AM
    To: Asterisk Users Mailing List – Non-Commercial Discussion Subject: [asterisk-users] Help with FUNC_MATH

    Hi,

    I have some dialplan code that is trying to convert 12 hour time with AM/PM to 24 hour format. The code has something like this:
    Exten => 2,1,ExecIf(${MATH(${HOUR_SELECTED}<12)}?Set(HOUR_SELECTED=${MATH(${HOUR_SELECTED}+12,int)}))

    Earlier on in the dialplan HOUR_SELECTED is set to 12. When they press option 2 they are selecting PM. If the time is from 1PM to 11PM then I want to add 12 to the number (so if it’s 1 make it 13 etc.). When I run the above the logs show the result as false yet if the user sets HOUR_SELECTED to 12 then after this line of dialplan code it gets switched to 24. What am I doing wrong here?

    The exact DP code is:

    Exten => 2, 1, Noop(BEFORE CHECK HOUR_SELECTED is ${HOUR_SELECTED})
    same => n, ExecIf(${MATH(${HOUR_SELECTED}<12)}?Set(HOUR_SELECTED=${MATH(${HOUR_SELECTED}+12,int)}))
    same => n, Noop(AFTER CHECK HOUR_SELECTED IS ${HOUR_SELECTED})

    And the output of the logs is:

    [Feb 13 10:46:18] VERBOSE[1580][C-00007bc6] pbx.c: Executing [2@am_pm_select:1] NoOp(“SIP/204.145.219.31-000081c6”, “BEFORE CHECK HOUR_SELECTED is 12”) in new stack
    [Feb 13 10:46:18] DEBUG[1580][C-00007bc6] pbx_variables.c: Result of ‘HOUR_SELECTED’ is ’12’
    [Feb 13 10:46:18] DEBUG[1580][C-00007bc6] pbx_variables.c: Function MATH(12<12) result is 'FALSE'
    [Feb 13 10:46:18] DEBUG[1580][C-00007bc6] pbx_variables.c: Result of 'HOUR_SELECTED' is '12'
    [Feb 13 10:46:18] DEBUG[1580][C-00007bc6] pbx_variables.c: Function MATH(12+12,int) result is '24'
    [Feb 13 10:46:18] DEBUG[1580][C-00007bc6] pbx.c: Launching 'ExecIf'
    [Feb 13 10:46:18] VERBOSE[1580][C-00007bc6] pbx.c: Executing [2@am_pm_select:2] ExecIf("SIP/204.145.219.31-000081c6", "FALSE?Set(HOUR_SELECTED=24)") in new stack
    [Feb 13 10:46:18] DEBUG[1580][C-00007bc6] pbx_variables.c: Result of 'HOUR_SELECTED' is '24'
    [Feb 13 10:46:18] DEBUG[1580][C-00007bc6] pbx.c: Launching 'NoOp'
    [Feb 13 10:46:18] VERBOSE[1580][C-00007bc6] pbx.c: Executing [2@am_pm_select:3] NoOp("SIP/204.145.219.31-000081c6", "AFTER CHECK HOUR_SELECTED IS 24") in new stack

    TIA.

    Dovid

  • My Apologies Dovid, I think I misunderstood your request.

    You don’t have the time you need to convert in the format of date string, Instead you have your users entering via DTMF when they want something to happen?

  • Is it stored as a floating-point number or an integer? Floating-point decimal numbers are often not stored precisely. As in my example, below, “12” may be stored as 11.99999999999 (simplified) and, although it will be treated as “12” in most cases, it will appear to be less than 12 in a comparison.

    From: asterisk-users [mailto:asterisk-users-bounces@lists.digium.com] Is HOUR_SELECTED a floating-point number (e.g. 11.99999999999)? If so, you need to account for that in your comparison.

    –Don

    From: asterisk-users [mailto:asterisk-users-bounces@lists.digium.com] On Behalf Of Dovid Bender Sent: Thursday, February 13, 2020 4:47 AM
    To: Asterisk Users Mailing List – Non-Commercial Discussion Subject: [asterisk-users] Help with FUNC_MATH

    Hi,

    I have some dialplan code that is trying to convert 12 hour time with AM/PM to 24 hour format. The code has something like this:
    Exten => 2,1,ExecIf(${MATH(${HOUR_SELECTED}<12)}?Set(HOUR_SELECTED=${MATH(${HOUR_SELECTED}+12,int)}))

    Earlier on in the dialplan HOUR_SELECTED is set to 12. When they press option 2 they are selecting PM. If the time is from 1PM to 11PM then I want to add 12 to the number (so if it’s 1 make it 13 etc.). When I run the above the logs show the result as false yet if the user sets HOUR_SELECTED to 12 then after this line of dialplan code it gets switched to 24. What am I doing wrong here?

    The exact DP code is:

    Exten => 2, 1, Noop(BEFORE CHECK HOUR_SELECTED is ${HOUR_SELECTED})
    same => n, ExecIf(${MATH(${HOUR_SELECTED}<12)}?Set(HOUR_SELECTED=${MATH(${HOUR_SELECTED}+12,int)}))
    same => n, Noop(AFTER CHECK HOUR_SELECTED IS ${HOUR_SELECTED})

    And the output of the logs is:

    [Feb 13 10:46:18] VERBOSE[1580][C-00007bc6] pbx.c: Executing [2@am_pm_select:1] NoOp(“SIP/204.145.219.31-000081c6”, “BEFORE CHECK HOUR_SELECTED is 12”) in new stack
    [Feb 13 10:46:18] DEBUG[1580][C-00007bc6] pbx_variables.c: Result of ‘HOUR_SELECTED’ is ’12’
    [Feb 13 10:46:18] DEBUG[1580][C-00007bc6] pbx_variables.c: Function MATH(12<12) result is 'FALSE'
    [Feb 13 10:46:18] DEBUG[1580][C-00007bc6] pbx_variables.c: Result of 'HOUR_SELECTED' is '12'
    [Feb 13 10:46:18] DEBUG[1580][C-00007bc6] pbx_variables.c: Function MATH(12+12,int) result is '24'
    [Feb 13 10:46:18] DEBUG[1580][C-00007bc6] pbx.c: Launching 'ExecIf'
    [Feb 13 10:46:18] VERBOSE[1580][C-00007bc6] pbx.c: Executing [2@am_pm_select:2] ExecIf("SIP/204.145.219.31-000081c6", "FALSE?Set(HOUR_SELECTED=24)") in new stack
    [Feb 13 10:46:18] DEBUG[1580][C-00007bc6] pbx_variables.c: Result of 'HOUR_SELECTED' is '24'
    [Feb 13 10:46:18] DEBUG[1580][C-00007bc6] pbx.c: Launching 'NoOp'
    [Feb 13 10:46:18] VERBOSE[1580][C-00007bc6] pbx.c: Executing [2@am_pm_select:3] NoOp("SIP/204.145.219.31-000081c6", "AFTER CHECK HOUR_SELECTED IS 24") in new stack

    TIA.

    Dovid

  • John,

    That is correct. I am trying to figure out why Asterisk is executing the set part of the execif, if it’s coming back as false.

  • Do you know that it is coming back as FALSE, or are you assuming that from examining the expression?

    –Don

    From: asterisk-users [mailto:asterisk-users-bounces@lists.digium.com] Hi,

    I have some dialplan code that is trying to convert 12 hour time with AM/PM to 24 hour format. The code has something like this:
    Exten => 2,1,ExecIf(${MATH(${HOUR_SELECTED}<12)}?Set(HOUR_SELECTED=${MATH(${HOUR_SELECTED}+12,int)}))

    Earlier on in the dialplan HOUR_SELECTED is set to 12. When they press option 2 they are selecting PM. If the time is from 1PM to 11PM then I want to add 12 to the number (so if it’s 1 make it 13 etc.). When I run the above the logs show the result as false yet if the user sets HOUR_SELECTED to 12 then after this line of dialplan code it gets switched to 24. What am I doing wrong here?

    The exact DP code is:

    Exten => 2, 1, Noop(BEFORE CHECK HOUR_SELECTED is ${HOUR_SELECTED})
    same => n, ExecIf(${MATH(${HOUR_SELECTED}<12)}?Set(HOUR_SELECTED=${MATH(${HOUR_SELECTED}+12,int)}))
    same => n, Noop(AFTER CHECK HOUR_SELECTED IS ${HOUR_SELECTED})

    And the output of the logs is:

    [Feb 13 10:46:18] VERBOSE[1580][C-00007bc6] pbx.c: Executing [2@am_pm_select:1] NoOp(“SIP/204.145.219.31-000081c6”, “BEFORE CHECK HOUR_SELECTED is 12”) in new stack
    [Feb 13 10:46:18] DEBUG[1580][C-00007bc6] pbx_variables.c: Result of ‘HOUR_SELECTED’ is ’12’
    [Feb 13 10:46:18] DEBUG[1580][C-00007bc6] pbx_variables.c: Function MATH(12<12) result is 'FALSE'
    [Feb 13 10:46:18] DEBUG[1580][C-00007bc6] pbx_variables.c: Result of 'HOUR_SELECTED' is '12'
    [Feb 13 10:46:18] DEBUG[1580][C-00007bc6] pbx_variables.c: Function MATH(12+12,int) result is '24'
    [Feb 13 10:46:18] DEBUG[1580][C-00007bc6] pbx.c: Launching 'ExecIf'
    [Feb 13 10:46:18] VERBOSE[1580][C-00007bc6] pbx.c: Executing [2@am_pm_select:2] ExecIf("SIP/204.145.219.31-000081c6", "FALSE?Set(HOUR_SELECTED=24)") in new stack
    [Feb 13 10:46:18] DEBUG[1580][C-00007bc6] pbx_variables.c: Result of 'HOUR_SELECTED' is '24'
    [Feb 13 10:46:18] DEBUG[1580][C-00007bc6] pbx.c: Launching 'NoOp'
    [Feb 13 10:46:18] VERBOSE[1580][C-00007bc6] pbx.c: Executing [2@am_pm_select:3] NoOp("SIP/204.145.219.31-000081c6", "AFTER CHECK HOUR_SELECTED IS 24") in new stack

    TIA.

    Dovid

  • I found my mistake. I was running execif on the result. I needed to change:
    ExecIf(${MATH(${HOUR_SELECTED}<11)}?Set(HOUR_SELECTED=${MATH(${HOUR_SELECTED}+12,int)}))
    TO:
    ExecIf($["${MATH(${HOUR_SELECTED}<11)}" ="TRUE"]?Set(HOUR_SELECTED=${MATH(${HOUR_SELECTED}+12,int)}))