Script fails getting value with scheduler but works in console

I use the following script to check if OSPF is in state “Full” as an indicator whether the ipsec and eoip tunnel to the VPN server is working. If not, the script forces an ipsec reconnect and sends an email. The script runs every minute by scheduler. The VPN server gets routes to VPN client networks via OSPF. At each client there is only one OSPF neighbor (the VPN server).
The script worked until the first time OSPF went to “Down”. After that event, the script cannot get the OSPF state anymore (no value is assigned to $ospfstate). The current OSPF state is “Full”.
I tried the script from the console and it works still fine but not when startet by “Run script” or by the scheduler. The script runs on one Hex v3 and one Hex PoE each with 6.40.5 installed and
one Hex v3 with 6.40.4 installed. The problem only occures (until now) with RouterOS 6.40.5.

Policy for script and scheduler: read,write,policy,test
Owner: admin

:local email "xxx@xxx"
:local emailfrom "xxx@xxx"
:local smtphost 123.123.123.123
:local smtpport 587
:local smtpuser "user"
:local smtppass "pass"

:global siteip 99
:global sitenumber 999
:local sitename ("B" . $sitenumber)

/routing ospf neighbor
:local ospfstate [get value-name=state number=0]
:put "OSPF State = $ospfstate"
:log info "OSPF State = $ospfstate"

:if ($ospfstate != "Full") do={
   :put "CheckOspf: State not Full ... reconnecting"
   :log info "CheckOspf: State not Full ... reconnecting"
   /tool e-mail send server=$smtphost port=$smtpport start-tls=yes user=$smtpuser password=$smtppass from=$emailfrom to=$email subject="$sitename: CheckOspf: failed!"
   /ip ipsec remote-peers remove numbers=0
}

Output in Log when started by scheduler:
994 Nov/13/2017 15:21:14 memory script, info OSPF State =
995 Nov/13/2017 15:21:14 memory script, info CheckOspf: State not Full … reconnecting

Output in Log started via console:
999 Nov/13/2017 15:34:16 memory script, info OSPF State = Full

Actually I found a solution before sending my request by changing the following line:

:local ospfstate [get value-name=state number=0]

to

:local ospfstate [get value-name=state [find instance="default"]]

Since I already prepared the request I thought I might share the experience as well with other maybe helping someone else. However I do not understand why the first solution works in the console but not with the scheduler while the second one does since there is always just one neighbor with the number=0. It seems to be advisable to never use constant number reference but always use find.

This is actually commonly asked question. More info on why it does not work read here:
https://wiki.mikrotik.com/wiki/Manual:Console#Item_Numbers

Thanks a lot for the quick reply.