disable/enable queue tree rule when no one using hotspot

Is it possible to do this?
I am using radius for my hotspot, for starter i’ve tried to put this script on “On Login” scripts user profile

:local hotspotuser +=1;

and “On Logout”

:local hotspotuser -=1;

but when i tried to ‘debug’ it on terimal :

[admin@local] > :put $hotspotuser
syntax error (line 1 column 7)

i am trying to make a global variable to holding the number on logged user and counting it down when user goes off, another one is i want to make a script which run at every 10s interval monitoring/read this $hotspotuser var, when ever its “0”, it will enable/disable rules in queue tree, are these possible?

I don’t have no idea of scripting.. but I think it must be:

:global instead of :local? :confused:


# Before using variable in script, it's name must be introduced. There are several ways to do that: With :global. It introduces name of global variable, which is created if it doesn't exist already.

[admin@MikroTik] ip route> /
[admin@MikroTik] > :global g1
[admin@MikroTik] > :set g1 "this is global variable"
[admin@MikroTik] > :put $g1
this is global variable
[admin@MikroTik] >

Global variables can be accessed by all scripts and console logins on the same router. There is no way currently to remove global variable, except rebooting router. Variables are not kept across reboots.
# With :local. It introduces new local variable, which is not shared with any other script, other instance of the same script, other console logins. It's value is lost when script finishes or when variable name is freed by :unset.

[admin@MikroTik] > :local l1
[admin@MikroTik] > :set l1 "this is local variable"
[admin@MikroTik] > :put $l1
this is local variable
[admin@MikroTik] >

Yes thanks but i have tried this, somehow “on Login” and “On Logout” doesn’t modified the value :frowning:,
On Login :

:global hotspotuser $hotspotuser; 
:set hotspotuser ($hotspotuser+1);

On Logout :

:global hotspotuser $hotspotuser; 
:set hotspotuser ($hotspotuser-1);

But when i tried to retrieved the $hotspotuser by “:put $hotspotuser;” on the terminal,it just show 0 which a value that i assigned before on the terminal.

I repeat I don’t have idea of scripting, but I think:

on startup create and set variable=0
Later on login or logout +1 and -1 only.


Make a script that runs on startup with:

:global hotspotuser;
:set $hotspotuser=0

login

:set hotspotuser ($hotspotuser+1);

logout:

:set hotspotuser ($hotspotuser-1);

It’s unnecessary to keep count. At any point in time you can access the number of logged in users via:

:local numberOfLoggedInUsers [:len [/ip hotspot active]]

@Ibersystems

Ya i was forgot to add user on hotspot, instead i am using user from userman.

Thanks but it’s always shows 0,i tried this on the terminal, “:global numberOfLoggedInUsers [:len [/ip hot
spot active]]” , and " :put $numberOfLoggedInUsers;" always shows 0, i have checked there is user logged in. and how to disabled rules in queue tree? my pseudo code would be like this :

:if ($numberOfLoggedInUsers = 0 and rule a enabled) do={ disable rule "a"}
:if ($numberOfLoggedInUsers >= 0 and rule a disabled) do={ enable rule "a"}

Sorry, that should be

:local numberOfLoggedInUsers [:len [/ip hotspot active find]]

Left out the find command. If that returns 0 when you have logged in users that would be a bug.

You can then conditionally turn on queue trees by finding them via comments.

:if ([:len [/ip hotspot active find]] > 0) do={ / queue tree set enabled [/queue tree find comment="turn this on for more than ten users"] }

Cool :slight_smile:, hmm but it’s error at this “[”

:if ([:len [/ip hotspot active find]] > 0) do={ /queue tree set enabled > [> /queue tree find comment=“turn this on for more than ten users”] }

any ideas?

:if ([:len [/ip hotspot active find]] > 0) do={ /queue tree set [/queue tree find comment=“turn this on for more than ten users”] enabled }



:if ([:len [/ip hotspot active find]] > 0) do={ /queue tree set [/queue tree find comment=“turn this on for more than ten users”] > e> nabled }

now it’s at “e”, sorry to kept asking you :frowning:.

I’m not near a router so this is a little difficult. You need to reproduce the command you’d usually use to enable/disable things.

Maybe

:if ([:len [/ip hotspot active find]] > 0) do={ /queue tree set [/queue tree find comment="turn this on for more than ten users"] disabled=no }

Figure out how you’d disable or enable a queue from the CLI manually, then use that command in the script.

It’s:

:if ([:len [/ip hotspot active find]] > 0) do={ /queue tree enable [/queue tree find comment="turn this on for more than ten users"] }

thanks, hmm it seem there is no comment properties on /queue tree, winbox seem only temporary save the comment if you set one, but will discard when reboot, so i use name instead :

 :if ([:len [/ip hotspot active find]] > 0) do={ /queue tree set [/queue tree find name="hotspot-morning"] disabled=yes }

Many thanks again fewi :open_mouth: :smiley:

:if ([:len [/ip hotspot active find]] > 0) do={ /queue tree set [/queue tree find name="hotspot-morning-down"] disabled=no }
:if ([:len [/ip hotspot active find]] > 0) do={ /queue tree set [/queue tree find name="hotspot-morning-up"] disabled=no }
:if ([:len [/ip hotspot active find]] > 0) do={ /queue tree set [/queue tree find name="hotspot-night-down"] disabled=yes }
:if ([:len [/ip hotspot active find]] > 0) do={ /queue tree set [/queue tree find name="hotspot-night-up"] disabled=yes }

:if ([:len [/ip hotspot active find]] = 0) do={ /queue tree set [/queue tree find name="hotspot-morning-down"] disabled=yes }
:if ([:len [/ip hotspot active find]] = 0) do={ /queue tree set [/queue tree find name="hotspot-morning-up"] disabled=yes }
:if ([:len [/ip hotspot active find]] = 0) do={ /queue tree set [/queue tree find name="hotspot-night-down"] disabled=no }
:if ([:len [/ip hotspot active find]] = 0) do={ /queue tree set [/queue tree find name="hotspot-night-up"] disabled=no }

The code run well but as i run it every 10s,it will rereset all of queue, the connection doesn’t drop but just a little spike, is there any way to check whether a specific id properties is disabled or not and return it as bool?
pseudo code:
if(hotspotuser>0 && hotspot-morning-down.disabled=yes) do {enable hotspot-morning-down), how to do that? i was trying to get disabled properties of id but return as “*100000e” .

fewi ? can you please help me with this last one :slight_smile:, or anyone ? :frowning: