Page 1 of 1

Rule advice: Trigger GPIO based on MQTT value

Posted: 24 Jun 2022, 13:49
by philipp
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

Re: Rule advice: Trigger GPIO based on MQTT value

Posted: 24 Jun 2022, 13:55
by Ath
Your attachments didn't make it into the message...

Re: Rule advice: Trigger GPIO based on MQTT value

Posted: 24 Jun 2022, 13:59
by philipp
Are you sure? They load fine from different devices here. Anyway, here's the direct link to the screenshots: https://imgur.com/a/9G689IJ

Re: Rule advice: Trigger GPIO based on MQTT value

Posted: 24 Jun 2022, 14:21
by TD-er
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

Re: Rule advice: Trigger GPIO based on MQTT value

Posted: 24 Jun 2022, 14:37
by Ath
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 3488 times
Probably it's the same cause ;) very strict network management, don't worry, I'll see 'm later.

Re: Rule advice: Trigger GPIO based on MQTT value

Posted: 24 Jun 2022, 15:48
by philipp
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.

Re: Rule advice: Trigger GPIO based on MQTT value

Posted: 25 Jun 2022, 10:51
by TD-er
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

Re: Rule advice: Trigger GPIO based on MQTT value

Posted: 25 Jun 2022, 11:01
by philipp
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

Re: Rule advice: Trigger GPIO based on MQTT value

Posted: 25 Jun 2022, 11:26
by TD-er
Can you try another GPIO pin?
On some boards GPIO-17 may be assigned other functions.

Re: Rule advice: Trigger GPIO based on MQTT value

Posted: 25 Jun 2022, 11:37
by philipp
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)

Re: Rule advice: Trigger GPIO based on MQTT value

Posted: 25 Jun 2022, 11:40
by philipp
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!

Re: Rule advice: Trigger GPIO based on MQTT value

Posted: 25 Jun 2022, 11:42
by TD-er
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?

Re: Rule advice: Trigger GPIO based on MQTT value

Posted: 25 Jun 2022, 11:43
by TD-er
Ah, good that you figured it out.