Community discussions

MikroTik App
 
eldoncito2019
Member
Member
Topic Author
Posts: 332
Joined: Fri Jun 14, 2019 1:07 pm

Using "do and else" in script.

Tue May 24, 2022 9:44 pm

I'm trying to make a script that does the following: before 18:00:00, it tells me "get ready for dinner" between 18:00:00 and 19:00:00, it tells me "it's dinner time" "and that after 19:00:00, it tells me "to rest dinner", is this possible, here is my script:
{
:local time [/system clock get time]
:if ($time <= "18:00:00" || $time => "19:00:00") do={:put "get ready for dinner"} else={:put "it's dinner time"}  XXX={:put "to rest dinner"}
}

EL DONCITO
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11967
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Using "do and else" in script.

Tue May 24, 2022 10:46 pm

read your own phrase and simply convert it on 3 :if

:if before 18:00:00, do= tells me "get ready for dinner"
:if between 18:00:00 and 19:00:00, do= tells me "it's dinner time"
:if after 19:00:00, do= tells me "to rest dinner"
 
pe1chl
Forum Guru
Forum Guru
Posts: 10183
Joined: Mon Jun 08, 2015 12:09 pm

Re: Using "do and else" in script.

Tue May 24, 2022 10:54 pm

It is possible to do it with 2 if-statements, 3 are not required. But it isn't possible with 1 if-statement, that is clear.
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11967
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Using "do and else" in script.

Tue May 24, 2022 10:55 pm

@pe1chl
It is better to learn to walk first, then to ride a bicycle... :roll:
 
eldoncito2019
Member
Member
Topic Author
Posts: 332
Joined: Fri Jun 14, 2019 1:07 pm

Re: Using "do and else" in script.

Tue May 24, 2022 11:58 pm

It will be like this with 2 if:
:local time [/system clock get time]
:if ($time <= "18:00:00" || $time = "19:00:00") do={:put "get ready for dinner"} else={:put "it's dinner time"}
:if ($time  > "19:00:00") do={:put ("to rest dinner"}

EL DONCITO
 
User avatar
nichky
Forum Guru
Forum Guru
Posts: 1275
Joined: Tue Jun 23, 2015 2:35 pm

Re: Using "do and else" in script.

Wed May 25, 2022 12:13 am

i'm really want to know, where you going to use this script?
 
eldoncito2019
Member
Member
Topic Author
Posts: 332
Joined: Fri Jun 14, 2019 1:07 pm

Re: Using "do and else" in script.

Wed May 25, 2022 12:20 am

I'm slowly learning to use the functions, just that, but the script doesn't work if I run it every second.
Can you help me, here is the script:
:global stopRouterRun false

:do {
:local time [/system clock get time]
:if ($time < "17:14:00" || $time = "17:14:10") do={:put "get ready for dinner"} else={:put "it's dinner time"}
:if ($time  > "17:14:10") do={:put "to rest dinner"}

   :delay 1s
} while=(!$stopRouterRun)

EL DONCITO.
 
pe1chl
Forum Guru
Forum Guru
Posts: 10183
Joined: Mon Jun 08, 2015 12:09 pm

Re: Using "do and else" in script.

Wed May 25, 2022 1:07 am

It will be like this with 2 if:
:local time [/system clock get time]
:if ($time <= "18:00:00" || $time = "19:00:00") do={:put "get ready for dinner"} else={:put "it's dinner time"}
:if ($time  > "19:00:00") do={:put ("to rest dinner"}
That is not correct, and also too complicated. In each if statement you need to make only 1 time compare, and use the do and else branch.
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11967
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Using "do and else" in script.

Wed May 25, 2022 2:36 am

@eldoncito2019, ignore @pe1chl (I'm not saying he doesn't say the right things, but it's early for you)
I know you're a beginner (no offense, of course) and until you learn the simple things first, forget about the slightly more complex ones ...

Ignore "else" and follow my already provided hint...
read your own phrase and simply convert it on 3 :if

:if before 18:00:00, do= tells me "get ready for dinner"
:if between 18:00:00 and 19:00:00, do= tells me "it's dinner time"
:if after 19:00:00, do= tells me "to rest dinner"
But haven't you noticed that your calculations are absurd?
You split day time on 2 parts: from 18:00 to 19:00 for dinner, and from 19:01 to 17:59 for NOT dinner.
02:00 is both after 18:00 and before 19:00
You mean midnight for split the time on 3 parts?
At that point you have 3 time intervals: 00:00 to 17:59 for "get ready for dinner", 18:00 to 19:00 for "dinner time", and from 19:01 to 23:59 for "rest dinner".
But if you want go over midnigth for "rest dinner" you put on play more complex conditions...

Ignoring the logic, but for just make one example, with only 2 "if", without any "else":
{
:local time [/system clock get time]
:local message "after dinner"
:if ($time >= 18:00:00 and $time <= 19:00:00) do={:set message "dinner time"}
:if ($time > 19:00:00) do={:set message "before dinner"}
:put $message
}
 
eldoncito2019
Member
Member
Topic Author
Posts: 332
Joined: Fri Jun 14, 2019 1:07 pm

Re: Using "do and else" in script.  [SOLVED]

Wed May 25, 2022 4:24 am

:global stopRouterRun false
:do {

    :local time [/system clock get time]
    :local message ("$time after dinner")
    :if ($time >= 21:22:00 and $time <= 21:22:10) do={:set message ("$time dinner time")}
    :if ($time > "21:22:10") do={:set message ("$time before dinner")}
    :put $message

    :delay 1s

} while=(!$stopRouterRun)

Thank you REX.



EL DONCITO.
 
eldoncito2019
Member
Member
Topic Author
Posts: 332
Joined: Fri Jun 14, 2019 1:07 pm

Re: Using "do and else" in script.

Wed May 25, 2022 5:24 pm

Very good REX, I'm learning about script, and if I just want the script to run like this:
:global stopRouterRun false

:do {

    :local time [/system clock get time]
    :if ($time >= 12:00:00 and $time <= 12:30:00) do={:set message ("$time dinner time")}
    :put $message

    :delay 1s

} while=(!$stopRouterRun)


EL DONCITO.
 
eldoncito2019
Member
Member
Topic Author
Posts: 332
Joined: Fri Jun 14, 2019 1:07 pm

Re: Using "do and else" in script.

Wed May 25, 2022 5:41 pm

I solved it like this
I hope you are well:
:global stopRouterRun false

:do {

    :local time [/system clock get time]
    :if ($time >= 18:31:00 and $time <= 18:31:10) do={:put ("$time dinner time")}
    
    :delay 1s

} while=(!$stopRouterRun)
But I have another question, how do I do it in the terminal so that the script does not continue running. Thank you.


EL DONCITO
 
msatter
Forum Guru
Forum Guru
Posts: 2897
Joined: Tue Feb 18, 2014 12:56 am
Location: Netherlands / Nīderlande

Re: Using "do and else" in script.

Wed May 25, 2022 6:55 pm

Currently, the mode of transportation is clearly still a push buggy. :lol:
 
eldoncito2019
Member
Member
Topic Author
Posts: 332
Joined: Fri Jun 14, 2019 1:07 pm

Re: Using "do and else" in script.

Wed May 25, 2022 7:54 pm

Currently, the mode of transportation is clearly still a push buggy. :lol:
I do not understand. LOL :?



EL DONCITO.
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11967
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Using "do and else" in script.

Wed May 25, 2022 8:25 pm

Remove
:global stopRouterRun false

:do
{

:local time [/system clock get time]
:if ($time >= 12:00:00 and $time <= 12:30:00) do={:set message ("$time dinner time")}
:put $message

:delay 1s

}
while=(!$stopRouterRun)

Who is online

Users browsing this forum: mbezuidenhout, UkRainUa and 19 guests