1 is not 1 - but why?

Hi, if I write this global variable:
:set currentStateDSL1 [:len [/ip route find gateway=$gatewayDSL1 dst-address=$dstAddressDSL1 routing-table=main active=yes]];

This returns a 1 (or a 0 in case of error).

But if I enter it this way:
:set currentStateDSL1 1;

it does not work:

:if (($currentStateDSL1 = 1) and ($previousStateDSL1 = 0)) do={...

Under "Enviroment" (Scripts) is in both ways a "1" displayed. But the ":set currentStateDSL1 1;" is not working?!

Are they both same number or string type in both cases as 1 != "1" even if they are displayed in the same form on the screen/in terminal?

BartoszP - in accordance with PA syntax both values should be "numbers".

What exactly "is not working"? any errors? Where are you running the script?

Try to add :global currentStateDSL1; at the very beginning.

global currentStateDSL1;

global previousStateDSL1 0 ;

:set currentStateDSL1 [:len [/ip route find gateway=$gatewayDSL1 dst-address=$dstAddressDSL1 routing-table=main active=yes]];

:log info " Return var currentStateDSL1: $currentStateDSL1"

:set currentStateDSL1 1;
:log info " Return var set currentStateDSL1 1; $currentStateDSL1"

:if (($currentStateDSL1 = 1) and ($previousStateDSL1 = 0)) do={:log info " Ok!!!"};

and result:

image

Without global shouldn't it be $ in front of a variable in set to declare it?

:set $currentStateDSL1 1;

PA noted "global", so first of all we declare globals.

and Yes, you are right $current... - if we want declare in :set

One error after another...
Variables must be declared first
in front of global and local must be used ":" (or "/" )
after :set don't put the $ (except arrays)
if run from the command line, the script requires to be enclosed on parentheses { }
useless ; are useless ;

:len return a number, not one string

@Guscht
As usual, without seeing the entire script, we can only speculate. For example, is $previousStateDSL1 present? When is it defined? How? It's also in the conditional; without specifying it, the topic's question doesn't make sense either.

And what does "not working" mean? It doesn't make any sense without explaining what's not working. What's not working? Your TV won't turn on?

No, his TV is working fine. :joy:

That's probably why the AP disappeared...

Untested (for obvious reason) script example: (enclose all on { } for try it on terminal)

:global currStateDSL1 ; :if ([:typeof $currStateDSL1] = "nothing") do={:set currStateDSL1 0}
:global prevStateDSL1 ; :if ([:typeof $prevStateDSL1] = "nothing") do={:set prevStateDSL1 0}
:local  gatewayDSL1   <...>
:local  dstAddrDSL1   <...>

# debug lines, remove after tests...
:set currStateDSL1 1
:log info "DEBUG: currStateDSL1 is $currStateDSL1 of type $[:typeof $currStateDSL1]"
# debug end

:set currStateDSL1 [:len [/ip route find where gateway=$gatewayDSL1 and dst-address=$dstAddrDSL1 and routing-table=main and active=yes]]
:log info "Return Lenght of the Find is $currStateDSL1 of type $[:typeof $currStateDSL1]"

:if (($currStateDSL1 = 1) and ($prevStateDSL1 = 0)) do={
    # the DSL go from 0 to 1
    :log info "currStateDSL1 is 1 and prevStateDSL1 is 0"
} else={
    # the DSL NOT go from 0 to 1, so it covers all other cases:
    # still offline 0 / 0
    # still online  1 / 1
    # go    offline 1 / 0
    :log info "currStateDSL1 is not 1 or prevStateDSL1 is not 0"
}

:set prevStateDSL1 $currStateDSL1