Rule advice: Trigger GPIO based on MQTT value

Moderators: grovkillen, Stuntteam, TD-er

Post Reply
Message
Author
philipp
New user
Posts: 6
Joined: 24 Jun 2022, 13:41

Rule advice: Trigger GPIO based on MQTT value

#1 Post by philipp » 24 Jun 2022, 13:49

Hi,

I have created a MQTT Import that looks like this:
Image
And I also have a Switch that is configured like this:
Image

I know want to set the state of GPIO 17 to HIGH if the value of my MQTT topic is 1. For that I created the following rule:

Code: Select all

On MQTT_sprinkler_1#valve01=1 do
   GPIO,17,1
endon
When I change the value on my MQTT broker using a MQTT client, the change is reflected under "MQTT import" in the "Devices" tab on my espeasy device. So I think the MQTT end of my setup is working without any problems. However, the state of GPIO17 doesn't change.
Do you see any mistakes in my rule? What could be a reason for it not working as expected?

Thanks!
philipp

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

Re: Rule advice: Trigger GPIO based on MQTT value

#2 Post by Ath » 24 Jun 2022, 13:55

Your attachments didn't make it into the message...
/Ton (PayPal.me)

philipp
New user
Posts: 6
Joined: 24 Jun 2022, 13:41

Re: Rule advice: Trigger GPIO based on MQTT value

#3 Post by philipp » 24 Jun 2022, 13:59

Are you sure? They load fine from different devices here. Anyway, here's the direct link to the screenshots: https://imgur.com/a/9G689IJ

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

Re: Rule advice: Trigger GPIO based on MQTT value

#4 Post by TD-er » 24 Jun 2022, 14:21

If you only want to control the GPIO via rules, you don't need to add the switch plugin.
I think it may even be causing issues if you do.

Next, please try to debug the rules using the logentry command.

Code: Select all

On MQTT_sprinkler_1#valve01 do
  logentry,"MQTT import valve01: %eventvalue1%"
  if %eventvalue1%=1
    GPIO,17,1
  endif
endon

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

Re: Rule advice: Trigger GPIO based on MQTT value

#5 Post by Ath » 24 Jun 2022, 14:37

philipp wrote: 24 Jun 2022, 13:59 Are you sure? They load fine from different devices here. Anyway, here's the direct link to the screenshots: https://imgur.com/a/9G689IJ
Because of the network I'm on, that link is not allowed to open :?

This is what I'm seeing:
Screenshot - 24-06-2022 , 14_34_50.png
Screenshot - 24-06-2022 , 14_34_50.png (8.41 KiB) Viewed 3439 times
Probably it's the same cause ;) very strict network management, don't worry, I'll see 'm later.
/Ton (PayPal.me)

philipp
New user
Posts: 6
Joined: 24 Jun 2022, 13:41

Re: Rule advice: Trigger GPIO based on MQTT value

#6 Post by philipp » 24 Jun 2022, 15:48

TD-er wrote: 24 Jun 2022, 14:21 If you only want to control the GPIO via rules, you don't need to add the switch plugin.
I think it may even be causing issues if you do.

Next, please try to debug the rules using the logentry command.

Code: Select all

On MQTT_sprinkler_1#valve01 do
  logentry,"MQTT import valve01: %eventvalue1%"
  if %eventvalue1%=1
    GPIO,17,1
  endif
endon
Tried that. Here's what I see in the log:

Code: Select all

6416943: IMPT : [MQTT_sprinkler_1#valve01] : 0.00
6424173: IMPT : [MQTT_sprinkler_1#valve01] : 1.00
6426821: WD : Uptime 107 ConnectFailures 0 FreeMem 220796 WiFiStatus WL_CONNECTED 3 ESPeasy internal wifi status: Conn. IP Init
6438941: IMPT : [MQTT_sprinkler_1#valve01] : 0.00
6444444: IMPT : [MQTT_sprinkler_1#valve01] : 1.00
However, as you can probably see from the log, the GPIO's state doesn't change.

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

Re: Rule advice: Trigger GPIO based on MQTT value

#7 Post by TD-er » 25 Jun 2022, 10:51

Well it should not change, as it is only setting it to '1'
Thats what you initially did, but if you want to set it via MQTT, then you can simply change the rules to this:

Code: Select all

On MQTT_sprinkler_1#valve01 do
  logentry,"MQTT import valve01: %eventvalue1%"
  GPIO,17,%eventvalue1%
endon

philipp
New user
Posts: 6
Joined: 24 Jun 2022, 13:41

Re: Rule advice: Trigger GPIO based on MQTT value

#8 Post by philipp » 25 Jun 2022, 11:01

TD-er wrote: 25 Jun 2022, 10:51 Well it should not change, as it is only setting it to '1'
Thats what you initially did, but if you want to set it via MQTT, then you can simply change the rules to this:

Code: Select all

On MQTT_sprinkler_1#valve01 do
  logentry,"MQTT import valve01: %eventvalue1%"
  GPIO,17,%eventvalue1%
endon
Tried this rule as well, but it doesn't change the GPIO's state either. Here's what the log says:

Code: Select all

173093: IMPT : [MQTT_sprinkler_1#valve01] : 0.00
175339: IMPT : [MQTT_sprinkler_1#valve01] : 1.00
179839: IMPT : [MQTT_sprinkler_1#valve01] : 0.00
181840: IMPT : [MQTT_sprinkler_1#valve01] : 1.00

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

Re: Rule advice: Trigger GPIO based on MQTT value

#9 Post by TD-er » 25 Jun 2022, 11:26

Can you try another GPIO pin?
On some boards GPIO-17 may be assigned other functions.

philipp
New user
Posts: 6
Joined: 24 Jun 2022, 13:41

Re: Rule advice: Trigger GPIO based on MQTT value

#10 Post by philipp » 25 Jun 2022, 11:37

I just tried GPIO-21 and GPIO-26, same problem.
Could it be something in the GPIO's device settings? Any box I need to tick? (c.f. screenshot from my first post)

philipp
New user
Posts: 6
Joined: 24 Jun 2022, 13:41

Re: Rule advice: Trigger GPIO based on MQTT value

#11 Post by philipp » 25 Jun 2022, 11:40

Aaaaah, I figured it out. :-)

I hadn't ticked the Generate events for accepted topics box. With that setting turned on, everything is working as expected. Thank you so much for your help and the awesome project!

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

Re: Rule advice: Trigger GPIO based on MQTT value

#12 Post by TD-er » 25 Jun 2022, 11:42

Just try first in the command box on the tools page.
Try to set some GPIO pin (e.g. GPIO-2 which is used on most boards to trigger a LED, often "inverted")

For example the output of "gpio,2,0" on the ESP8266 board on my desk:

Code: Select all

{
"log": "port#2: set to 0",
"plugin": 1,
"pin": 2,
"mode": "output",
"state": 0
}
What do you get there and do you also see something appear in the logs?

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

Re: Rule advice: Trigger GPIO based on MQTT value

#13 Post by TD-er » 25 Jun 2022, 11:43

Ah, good that you figured it out.

Post Reply

Who is online

Users browsing this forum: No registered users and 26 guests