ive been testing a MQTT Sub which run a on-message script, this seems to work fine for a few moments and then stops responding to the changes on the MQTT server.my setup is a RB4011 using an external MQTT server on the same network
/iot/mqtt/subscriptions/print
0 broker=broker topic="mkt/rb4011/vpn/status" qos=2
on-message=":log info "Got data {$msgData} from topic {$msgTopic}"\\n\\n:if ($msgData = "on") do={\\n
/ip/firewall/mangle/enable numbers=31\\n :log info "VPN Status ON - Enabled mangle rule
31"\\n}\\n\\n:if ($msgData = "off") do={\\n /ip/firewall/mangle/disable numbers=31\\n :log info
"VPN Status OFF - Disabled mangle rule 31"\\n}"
You should not refer to the firewall rules by number, as the numbers are dynamic and intrinsically tied to the print command. This means that a firewall rule has nu number before you run the print command, and the number changes depending on what you do with the print command.
Instead, either refer to the firewall rule by the .id which is permanent, or use thefind command.
In my example below, I have a firewall with a comment saying Wireguard, but you can find it by any parameter or a combination of parameters.
Thanks I really appreciate your comment and will look to implement it this way, the problem I have forgetting the firewall logic, it is even when I just subscribe to the topic and print the data to the MikroTik log this stops working after a few mins, it’s like it has just lost the connection to there broker, I’ve found if I manually publish a message from the router to the broker it’s kicks back in to life.