[Solved] Need help for global variable

Hello,
I tried to delay one event but sth. is wrong with code:
So the idea ist to do sth. after an event occurs twice so there is some kind of verification here is my rule

var delaycounter

rule "switch automated off"
    when
        Item switch_mobile_presence changed from ON to OFF
    then
        if (delaycounter == 2)
        {
            delaycounter = 0
            WallPlug13.sendCommand(OFF)
            logInfo("switch automated off", "triggered")
        }
        else
        {
            logInfo("switch automated off delay", delaycounter.toString())
            delaycounter++
        }
end

error is

tostring is not a member of 'null';

Maybe this can work for your case…

rule "switch automated off"
    when
        Item switch_mobile_presence changed from ON to OFF
    then
        createTimer(now.plusSeconds(2)) [|
            WallPlug13.sendCommand(OFF)
            logInfo("switch automated off", "triggered")
        ]
end

If you need just to wait some second before send command will works great

OR if you need to trigger twice the sensor this gonna work:

var Number delaycounter = 0

rule "switch automated off"
    when
        Item switch_mobile_presence changed from ON to OFF
    then
 
        if (delaycounter == 2)
        {
            delaycounter = 0
            WallPlug13.sendCommand(OFF)
            logInfo("switch automated off", "triggered")
        }
        else
        {
            logInfo("switch automated off delay", delaycounter.toString())
            delaycounter = delaycounter + 1
        }
end

just initialize to 0