[Solved] GPIO through MQTT with ESP Easy?

Hi

I’m becoming crazy with that. I have read all subjects I could find here but still no perfect solution for that !! I have some ESPEasy modules all around my flat. I have wired a relay on GPIO 5 of one of them to control my heater, and an other one to control a fan in bathroom !! I can put them on through HA without problems but not off !! When I do Off in web interface of HA, button of HA goes off but relay stays on on ESP !!

Here is code I use: (logic reversed between both due to devices controlled :wink:

  switch:
  - platform: mqtt
    name: "Ventilateur"
    state_topic: "/sdb/relay/state"
    command_topic: "/sdb/gpio/5"
    payload_on: 1
    payload_off: 0
    optimistic: false
    retain: true
  - platform: mqtt
    name: "Chauffage"
    state_topic: "/cuisine/relay/state"
    command_topic: "/cuisine/gpio/5"
    payload_on: 0
    payload_off: 1
    optimistic: false
    retain: true

Thanks for help,

Vincèn

I can’t see any obvious problems with your configuration. Do the modules turn off if you send an mqtt message from somewhere else?

maybe it is ESP program problem not HA?

Well I tried to trigger on and off relays using MQTT Snooper service from my cellphone but not much success :frowning: Not sure if it’s because it would have a problem on ESP or if I’m doing it in wrong way to send the MQTT message…

So for now is I’m not interested to get “feedback” from relay, I succeeded to get it working this way:

platform: mqtt
name: "Ventilateur"
#state_topic: "/sdb/gpio/5/state"
command_topic: "/sdb/gpio/5"
payload_on: "1.0"
payload_off: "0.0"
optimistic: false
retain: true
value_template: '{{ value.x }}'

It depends on how you have the device setup in ESP Easy. Looks like you don’t have the state topic correct to me. it should be “/espname/devicename/value name 1”. For example, the state topic for the below pic is “/ESP02/Relay_01/RL_01”

You really want to get your state_topic correct as per my other post. Also payload for ESP Easy is 1 or 0. You can test GPIO relays using below format

http://192.168.100.127/control?cmd=GPIO,1,1

Thanks @Paulio for explanations to setup feedback but I don’t get it working (no big deal here !!). I have created the relay device in ESP (till now I had setup nothing as I thought GPIO for relays were working without additional setup:


I have setup HA like that:
state_topic: "/sdb/gpio5/gpio5"
but feedback still not working correctly :confused:

Ok that looks correct now. Have you enabled MQTT Retain Msg: in the advanced setting, If this is not enabled and you restart home assistant while relay in ON state things will be out of sync and you need to manually send http://sdbipaddress/control?cmd=GPIO,5,0 to get in sync.

Also noticed you are using ESP Easy Mega. I have not tried that. I am using ESPEasy_R147_RC8 and everything working perfect for me so not sure if something different with Mega setup.

@Paulio Yep MQTT Retain Msg is activated on all my ESP :wink:

I always used ESP Easy Mega in version 2 :wink: Unless it’s still beta it works really well and supports a lot more devices than version 1 !

Thanks again for help and solution :wink:

Ok cool, I been meaning to try Mega but other version does everything I need so haven’t looked yet. V1 supports 12 devices which is more then I can use with a wemos d1 mini so all good ;).

Cool did that solve your issue or you happy with your workaround?

Yep problem solved :wink: Just need to figure out an other issue (but due to my still poor knowledge on all programming stufffs of ha :smiley: Float conversion?

Could you share your latest configuration.yaml text for this setup? i’m having te same setup but cant get it up and running…

@Markgo1988 I would prefer to avoid for now as it’s not working well :frowning: I have an issue of on/off sequence when putting back online the ESP module with relay or rebooting the Home assistant !
Right now I’m cleaning my system by reinstalling completely HA and doing a clean setup (tweaked and played too much with existing system that became too much a mess :smiley:

Thanks a lot for sharing and helping, got mine running finally.

Configuration NodeMCU with ESP Easy Mega:

  • Name of your device is essential for the MQTT configuration (“config” - “unit name”)

  • Add “OpenHAB MQTT” under “controllers”

  • Add “Switch” under “devices”

  • “Device name” (Relay02) and “Value name” (RL02) are also essential for your MQTT configuration

  • Change “1st GPIO: GPIO-2 (D4)” to your configuration, i used jumper “D4”

After configuration of your ESP device, use this configuration in Home Assistant - for me under “switches”

#
- platform: mqtt
  name: "<your-ESP-unit-name ("config"-"unit name")>"
  state_topic: "/<your-ESP-unit-name>/Relay02/RL02"
  command_topic: "/<your-ESP-unit-name>/gpio/2"
  payload_on: 0
  payload_off: 1
  optimistic: false
  retain: true
#

I had to use “payload_on: 0”, to switch my relay properly…might be “1” for you, give it a try.

Edit: I made a sketch with “Fritzing”, feel free to use/adapt it, if interested.

2 Likes

Has anyone had the issue with the relays attached to the nodemcu cycling on and then off whenever the nodemcu is booted? Oddly in only occurs with the relay configured as a switch in HASS.io. If it is not configured, the port does not cycle on a reboot. Doesn’t work well for a garage door opener relay. Any thoughts or experience is appreciated. Thanks.

Thanks for this. There is so much out there where the writer assumes you magically know the relationship between the topics specified in configuration.yaml and what is configured in ESPEasy!

The last part of the jigsaw for me was that ESPEasy defaulted the topic names to have no preceeding “/” but all the examples I see have that in the yaml file. Spotted that, removed it and it works!

It’s worth noting that, under Developer, you will have an MQTT heading. If you select that and listen on topic #, you will see all messages being published. That helped me a lot!

1 Like