MQTT messages from Home Assistant to ESP Easy Mega ESP8266

Hello Home Assistant community.

Starting with Home Assistant I have some trouble to sent MQTT messages from
Home Assistant (running on a raspberry) to an ESP8266 Wemos D1 (configured with ESPEasy Mega).

I just want to swith off the built-in LED on the ESP8266.
Therefore I want to publish a MQTT message from HA and receive this message on the ESP8266.
A rule is running to check the incoming message for the correct payload and turn off the LED.

I publish the MQTT message from the Mosquitto MQTT Broker from HA.
With the MQTT Explorer I can see the published messages, but the ESP8266 do not response.

I think I make mistakes to send the MQTT messages correctly…

Could someone help me and provide some information to solve this, please?

Via HTTP I can switch off/on the LED:
Off:
http://192.168.1.37/control?cmd=GPIO,2,1
On:
http://192.168.1.37/control?cmd=GPIO,2,0

In addition the other way round (from ESP8266 to HA) is working well.
A DHT22 sensor attached to the ESP8266 is sending via MQTT the values for temperature and humidity.

Below are pictures of the configuration and what I tried so far.

Cheers and many thanks in advance!

EDIT1:
Obviously I cannot attach all pictures I made from my configuration.
So, here are written details about the configuration I used for ESP Easy Mega.

EDIT 2:
I attached an “all in one picture” and hope this will allow you to understand the details

Unit Name:
ESP_Sensor_GZ (_1 attached automatically by ESP Easy)

MQTT Controler Client ID:
ESP_Sensor_GZ_MQTT

Controller Subscribe:
%sysname%/#

Controller Publish:
%sysname%/%tskname%/%valname%

Generic - MQTT
Name: ESP_Sensor_GZ_MQTT_Import

MQTT Topic:
ESP_IN

Value:
Incoming_val

ESPEasy Rules I tried:

// Working
On MQTTimport#Connected do
Publish,%sysname%/status,MQTT_Import_is_now_operational
Endon

// Not Working
On ESP_Sensor_GZ_MQTT_Import#Incoming_val=1 do
GPIO,2,1
Endon

// Not Working
On ESP_IN#Incoming_val=1 do
GPIO,2,1
Endon

On HA side I tried via Mosquitto to publish MQTT messages with several different topic without success.

Topic:
/ESP_Sensor_GZ_MQTT/Incoming_val
Paylod: 1

/ESP_Sensor_GZ_MQTT/ESP_IN
Paylod: 1

/ESP_Sensor_GZ_MQTT_Import/ESP_IN
Paylod: 1

…and different variants of it. Including with and without leading “/”

# mqtt switch:

-   name: Washok LED groen
    unique_id: '020002'
    icon: mdi:lightbulb-outline
    state_topic: "Sonoff_was/groen/LED"
    command_topic: "Sonoff_was/gpio/13"
    payload_on: "0"
    payload_off: "1"
    optimistic: false
    qos: 0
    retain: true

Above an example on howto switch a led on and off, you need to adapt this to your situation.
I am not fully clear on what is being send is that a button or a sensor?
If needed I can send you a basic example of a button or sensor too, if needed.
Switching you can do in rules on your ESP Easy device or you can make an automation which I prefer unless you like to keep the stand alone functionality when HA could be down.

What is the node name “ESP_Sensor_GZ_MQTT” ???
What is the sensor name?
What is the led name?
these you gave them in ESP mega, I did not see them in your pictures.

Hope it helps you on solving this. I used ESP easy nodes for a long time, although slowly phasing them out for ESPHOME, still so many in my walls :slight_smile:


# mqtt switch:

-   name: ESP_Sensor_GZ_MQTT
    unique_id: '100000'
    icon: mdi:lightbulb-outline
    state_topic: "ESP_Sensor_GZ_MQTT/groen/LED"
    command_topic: "ESP_Sensor_GZ_MQTT/gpio/2"
    payload_on: "0"
    payload_off: "1"
    optimistic: false
    qos: 0
    retain: true

Thank you for your response and helping me!

Unfortunately, the code didn’t work.

To anwer your questions:
The “Unit Name”: ESP_Sensor_GZ
The Sensor Name: Sensor_DHT22
The LED has not name

“Sensor Name” and “LED Name” is not needed, I think.
I try to switch off/on the LED undependetly of an attached sensor to the ESP8266.

Your solution with the switch is very welcome, because this would be my next step!

Now, I want to use the Mosquitto MQTT of HA to “publish a packet” MQTT message to the ESP8266
to swith the LED off/on (just by hand to check the connection).
Obviously my connection in not linked, I assume I did not correctlyy address the packet.
Or is it not possible to do like this: sending a package via Mosquitto to the ESP8266 to switch off/on the LED?

Unfortunately I am not able to attach pictures, so here some information in written form:

The MQTT Controller ID: ESP_Sensor_GZ_MQTT
Controller Subscribe: %sysname%/#
Controller Publish: %sysname%/%tskname%/%valname%

Generic MQTT Import: ESP_Sensor_GZ_MQTT_Import
MQTT Topic: ESP_IN
Values: Incoming_val

For Rules I tried this:

// Working
On System#Boot Do
  GPIO,2,0
Endon

// Working
On MQTTimport#Connected do
 Publish,%sysname%/status,MQTT_Import_is_now_operational
Endon

// Not Working
On ESP_Sensor_GZ_MQTT_Import#Incoming_val=1 do
  GPIO,2,1
Endon

// Not Working
On ESP_IN#Incoming_val=1 do
  GPIO,2,1
Endon

Here is my configuration.yaml
(to give it a try, I removed 2 lines of your code)

# Loads default set of integrations. Do not remove.
default_config:

# Load frontend themes from the themes folder
frontend:
  themes: !include_dir_merge_named themes

# Text to speech
tts:
  - platform: google_translate

automation: !include automations.yaml
script: !include scripts.yaml
scene: !include scenes.yaml

#--- MQTT ---
mqtt:
  sensor:
    - name: "DHT22_temp"
      device_class: temperature
      unit_of_measurement: "°C"
      state_topic: "ESP_Sensor_GZ_1/Sensor_DHT22/Temperature"
    - name: "DHT_hum"
      device_class: humidity
      unit_of_measurement: "%"
      state_topic: "ESP_Sensor_GZ_1/Sensor_DHT22/Humidity"

  switch:
    name: ESP_Sensor_GZ_MQTT
    unique_id: '100000'
    #icon: mdi:lightbulb-outline
    #state_topic: "ESP_Sensor_GZ_MQTT/groen/LED"
    command_topic: "ESP_Sensor_GZ_MQTT/gpio/2"
    payload_on: "0"
    payload_off: "1"
    optimistic: false
    qos: 0
    retain: true

Hi
I fear it does not really help you when using ESP Easy, but it seems to be much simpler when using ESPHome:

switch:
  - platform: gpio
    pin: D4
    name: "BME6802 LED"
    inverted: true
    id: builtin

and in case it is switched on on boot ( my Wemos D1 Wifi is doing that)

esphome:
  name: something
  platform: ESP8266
  board: d1_mini
  on_boot:
    priority: -10
    # ...
    then:
      - switch.turn_off: builtin

becomes a switch in HA and you can toogle from Lovelace or in automations, for example if you want to toggle the LED on high temperatures or humidity.

Armin

Many thanks Armin!

Unfortunately you are right, I am not using ESPHome (at least not at this moment…)

Do you know how do I can sent a Mosquitto MQTT message from HA to ESP Easy?

I mean, I just want to use Mosquitto MQTT to publish a packet to the ESPEasy.
I think I am not entering the correct “topic” that the packet will be received by ESP Easy.

Her is a picture of HA Mosquitto.
What do I need to enter to at the line “topic” to sent a playload of “0” or “1” to ESP Easy?
Or is this not possible to send via Mosquitto MQTT packets in this way?

Details of the ESP8266 configuration:
The “Unit Name”: ESP_Sensor_GZ
The MQTT Controller ID: ESP_Sensor_GZ_MQTT
Controller Subscribe: %sysname%/#
Controller Publish: %sysname%/%tskname%/%valname%
Generic MQTT Import: ESP_Sensor_GZ_MQTT_Import
MQTT Topic: ESP_IN
Values: Incoming_val

Do not really have experience with ESP Easy… using ESPHome for some devices, one device has manually created code using platformIO

based on my experience with MQTT and your screenshots your subscription is for %sysname%/#
%sysname% is a variable for the systemname and it subscribes to all topics in %sysname%
not sure what it refers to in ESPEasy, might be ESP_SENSOR_GZ
taking a wild guess the topic thus might be
ESP_Sensor_GZ/ESP_IN
or
/ESP_Sensor_GZ/ESP_IN

the payload might require JSON-Formatting, at least you have a setting like “parse JSON” in one of the screenshots, something like

{
“Incoming_val”: 1
}

But it also might accept raw values…
again, at lot of guessing involved…
Armin

# mqtt_sensors:
- name: "t_wemos1"
  unique_id: '8469874900'
  state_topic: Wemos1/DHT22/temperature
  device_class: temperature  
  unit_of_measurement: °C
  value_template: "{{value_json}}"
  json_attributes_topic: "home/Wemos1/attributes"

# mqtt_sensors:
- name: "h_wemos1"
  unique_id: '7289874900'
  state_topic: Wemos1/DHT22/humidity
  unit_of_measurement: "%H"
  device_class: humidity
  value_template: "{{value_json}}"

Hi I only gave some snippets of code, I work with together with esp easy, you have to adapt them, sorry.
You could ask for the json output of the ESP Easy node and get the proper names, and MQTT messages.

One little detail, I did notice today in your configuration yaml is that your MQTT is directly within configuration.yaml. A reason for the code snippets i did send you is that i did decentralise my MQTT into a folder with all sensors switches lights and whatever in diffferrent folders here is what I did, this way it is easier to see what is MQTT and if you need to change one or add one you only change one little file, less risk of bricking.

in my configuration.yaml I have:

mqtt:
  binary_sensor: !include_dir_merge_list mqtt/binary_sensor/
  sensor: !include_dir_merge_list mqtt/sensor/
  button: !include_dir_merge_list mqtt/button/
  cover: !include_dir_merge_list mqtt/cover/
  fan: !include_dir_merge_list mqtt/fan/
  light: !include_dir_merge_list mqtt/light/
  lock: !include_dir_merge_list mqtt/lock/
  switch: !include_dir_merge_list mqtt/switch/

in my config folder, I made a folder with name mqtt and within that folder I made subfolders named the above entities, and in there I place the snippets you see, that way they run. Hope this helps
Succes

Thank you!

It works for entering

/ESP_Sensor_GZ/ESP_IN

in the “Topic” field with a payload of 1.

The topic which the ESPEasy is listening for is as well

/ESP_Sensor_GZ/ESP_IN

This rule here:

On ESP_Sensor_GZ_MQTT_Import#Incoming_val=1 do
  GPIO,2,1
Endon

takes the payload and switched the LED.

Cheers!

What is working in my configuration yaml is following:

  switch:
    - name: "TEST_LED"
      state_topic: "ESP_Sensor_GZ_1/gpio/2"
      command_topic: "ESP_Sensor_GZ_1/gpio/2"
      payload_on: "0"
      payload_off: "1"

Cheers!

Hello Martin,

It seems thats your ESP is working, maybe you can support me bit.

RG
Chris