Does "If MQTT#Connected" exist ?

Moderators: grovkillen, Stuntteam, TD-er

Post Reply
Message
Author
johndoe39274
Normal user
Posts: 14
Joined: 28 Nov 2021, 21:05
Location: Belgium

Does "If MQTT#Connected" exist ?

#1 Post by johndoe39274 » 09 Dec 2021, 15:19

Hey,

I'd like to perform an action on the condition that ESPeasy is connected to my MQTT broker. In the examples there's an on..do statement for this, but is there a way to use this in an If statement?

So instead of:

Code: Select all

on MQTT#Connected do
 Publish,%sysname%/status,First message!
endon
Do this:

Code: Select all

on mcp#17 do        // PA0 pin change 
 if MQTT#Connected
  Publish,%sysname%/status,First message!
 endif
endon
Thanks in advance!

User avatar
Ath
Normal user
Posts: 3507
Joined: 10 Jun 2018, 12:06
Location: NL

Re: Does "If MQTT#Connected" exist ?

#2 Post by Ath » 09 Dec 2021, 15:30

There is a system variable that returns the state of the MQTT connection.
This should work:

Code: Select all

on mcp#17 do        // PA0 pin change 
 if %ismqtt%=1
  Publish,"%sysname%/status,PA0 pin message!"
 endif
endon
/Ton (PayPal.me)

johndoe39274
Normal user
Posts: 14
Joined: 28 Nov 2021, 21:05
Location: Belgium

Re: Does "If MQTT#Connected" exist ?

#3 Post by johndoe39274 » 10 Dec 2021, 14:53

That sure works, thanks!

My working code is:

Code: Select all

on mcp#17 do        // PA0 pin change 
 if [plugin#mcp#pinstate#17]=0 and %ismqtt%=0       //button pressed and MQTT not connected
  MCPLongPulse_mS,1,1,20       // Pulse LED for 20 milliseconds (LongPulse_mS,<GPIO>,<state>,<duration>)
 endif
endon
In the meantime I found a nice overview of system variables in ESPeasy. Log onto the web interface of your ESPeasy node and go to the Tools menu. There's a button System Variables that conveniently shows all system variables, constants, special characters and standard conversions that you can use in your Rules code.

Post Reply

Who is online

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