Switch MQTT controlers??

Moderators: grovkillen, Stuntteam, TD-er

Post Reply
Message
Author
dampa
Normal user
Posts: 56
Joined: 19 Jul 2018, 01:48

Switch MQTT controlers??

#1 Post by dampa » 17 Mar 2020, 18:11

Is there any way in rules to deactivate/activate MQTT controllers?

Here is what I want to do. Have a device setup with two SSID's to connect to. Depending on the SSID, I want to activate the MQTT-1 controller or the MQTT-2 controller. The MQTT-1 controller would be in my local network, but MQTT-2 would be BeeBotte for when the device was at a second location where it would connect to SSID2.

While I know I could use BeeBotte (remote MQTT) all the time, I want to setup a fallback like you can do with the SSID's.

Thanks in advance for any guidence (even it is 'nope')

User avatar
ThomasB
Normal user
Posts: 614
Joined: 17 Jun 2018, 20:41
Location: USA

Re: Switch MQTT controlers??

#2 Post by ThomasB » 17 Mar 2020, 18:52

I noticed that @TD-er recently added two new commands for the controllers. See ControllerDisable and ControllerEnable commands:
https://espeasy.readthedocs.io/en/lates ... mmand.html

For the record I have no experience with the new commands. Just passing on the info in case it helps your application.

- Thomas

dampa
Normal user
Posts: 56
Joined: 19 Jul 2018, 01:48

Re: Switch MQTT controlers??

#3 Post by dampa » 17 Mar 2020, 19:53

SWEET!!! that works! now to see if I can check the SSID and set it.

dampa
Normal user
Posts: 56
Joined: 19 Jul 2018, 01:48

Re: Switch MQTT controlers??

#4 Post by dampa » 17 Mar 2020, 20:14

well I can get it to work using %ip4% since that is a value

Code: Select all

on System#Boot do
  ControllerDisable,1
  ControllerDisable,2
endon
on WiFi#Connected do
   if %ip4% = 28 do
      ControllerEnable,1
   else  
      ControllerEnable,2
   endif
endon
so If I set my home network to assign the IP based on the device's MAC address the odds of getting that same octet on another network are about 1 in 254 which are pretty good odds it won't get the same octet!

Too bad there isn't a system variable to let you know if you are on the primary or fallback SSID (like 1 or 2)

UPDATE: Yup it works. If I leave the IP settings blank and have my router assign the IP address, with the rules above, I can turn of the controller I want!
Last edited by dampa on 17 Mar 2020, 21:00, edited 2 times in total.

User avatar
ThomasB
Normal user
Posts: 614
Joined: 17 Jun 2018, 20:41
Location: USA

Re: Switch MQTT controlers??

#5 Post by ThomasB » 17 Mar 2020, 20:27

Perhaps %bssid% (MAC) will allow you to detect which network is connected.

- Thomas

TD-er
Core team member
Posts: 2320
Joined: 01 Sep 2017, 22:13
Location: the Netherlands
Contact:

Re: Switch MQTT controlers??

#6 Post by TD-er » 17 Mar 2020, 20:35

If you have primary and secondary set, you also have different SSID's, and there is the %ssid% variable.
Also there is an event when you are connected to WiFi, so in that block you can compare the %ssid% value.
Only thing is that currently the string matching is a bit limited, but another recent addition is the substring stuff as described here: https://espeasy.readthedocs.io/en/lates ... terpreting

dampa
Normal user
Posts: 56
Joined: 19 Jul 2018, 01:48

Re: Switch MQTT controlers??

#7 Post by dampa » 17 Mar 2020, 20:59

Thomas, I don't think that would work since (if I understand it) in rules you can not do a compare to a string, and %bssid% is 50:C7:BF:78:4F:7B.

If you could use a string in a rule I could just use

Code: Select all

if %ssid% = "My hidden SSID" do  
but that doesn't work (I tried it)

But my idea in my previous post does work and will only fail if the other system assigns the same 4th octet in the IP address

User avatar
ThomasB
Normal user
Posts: 614
Joined: 17 Jun 2018, 20:41
Location: USA

Re: Switch MQTT controlers??

#8 Post by ThomasB » 17 Mar 2020, 21:42

But my idea in my previous post does work and will only fail if the other system assigns the same 4th octet in the IP address
Understood. My idea was provided because it sounded like you wanted to harden your rules.
I don't think that would work since (if I understand it) in rules you can not do a compare to a string, and %bssid% is 50:C7:BF:78:4F:7B.
String variables are new, so if this idea does not work then (as mentioned by TD-er) there is also a new substring parser. Theoretically it could be used to sample some digits in the %bssid% for use in your rules (that is to say, skip the colons). https://espeasy.readthedocs.io/en/lates ... #substring

But unfortunately there seems to be a bug in substring. I just did a quick test and found that all digits after the first colon are ignored by the substring function. So this solution won't work at the moment.

But there is also a %mac_int% system var. This value is the last 24 bits of MAC address (as an integer) for use in your rules. I think this is a possible candidate for determining the online network.

BTW, the "do" keyword at the end of your "if" statement is not needed. The "do" is reserved for "on" rules.

- Thomas

dampa
Normal user
Posts: 56
Joined: 19 Jul 2018, 01:48

Re: Switch MQTT controlers??

#9 Post by dampa » 17 Mar 2020, 22:23

TD-er wrote:
17 Mar 2020, 20:35
If you have primary and secondary set, you also have different SSID's, and there is the %ssid% variable.
Also there is an event when you are connected to WiFi, so in that block you can compare the %ssid% value.
Only thing is that currently the string matching is a bit limited, but another recent addition is the substring stuff as described here: https://espeasy.readthedocs.io/en/lates ... terpreting
So that would work if the SSID had a number in it. Am I right that the rules will only work on string that are in fact numbers?
Since my SSID is "Mines of Moria" it won't work, but if my SSID was "Mines of Moria 99" I could extract the 99 out with substring and then use that value.

the down side of doing that is I would have to change every device (and my wife's devices and my kids when they came to visit) to use the new SSID

For now I'll stick to using %ip4%, but it would be nice if there was a value (number) showing if I was using the primary or secondary SSID

TD-er
Core team member
Posts: 2320
Joined: 01 Sep 2017, 22:13
Location: the Netherlands
Contact:

Re: Switch MQTT controlers??

#10 Post by TD-er » 17 Mar 2020, 23:17

Yep in rules we now must turn the values to compare into numerical values.
There is a hex converter command, so BSSID can be turned into an integer value. But it does need some level of masochism to really like doing it like that.
If %ip4% (or another part of your subnet) does also work, please do use it.

Also the enable/disable task and controller commands are really new and I even fixed a bug in the -task variant of these commands.
So please let me know if you found something odd in using them.

Post Reply

Who is online

Users browsing this forum: Google [Bot] and 4 guests