ESP8266 MQTT message not received by home assistant?

I’m trying to automate my door bell in the same way some people did in this topic: MQTT Doorbell

I’m using a ESP8266 and this arduino sketch to send the message:

#include <ESP8266WiFi.h>
#include <PubSubClient.h>

#define wifi_ssid "lofoten"
#define wifi_password "mypass"

#define mqtt_server "192.168.2.8"
#define mqtt_user "homeassistant"
#define mqtt_password "mymqttpass"

#define doorbell_topic "hal/doorbell"

int doorbellState = 0;
const int doorbellPin = 2;

WiFiClient espClient;
PubSubClient client(espClient);

void setup() {
  Serial.begin(115200);

  setup_wifi();
  client.setServer(mqtt_server, 1883);
  pinMode(doorbellPin, INPUT_PULLUP);

  WiFi.mode(WIFI_STA); //don't be a AP
 }

void setup_wifi() {
  delay(10);
  // We start by connecting to a WiFi network
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(wifi_ssid);

  WiFi.mode(WIFI_STA);
  WiFi.begin(wifi_ssid, wifi_password);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
}

void reconnect() {
  // Loop until we're reconnected
  while (!client.connected()) {
    Serial.print("Attempting MQTT connection...");
    // Attempt to connect
    // If you do not want to use a username and password, change next line to
    // if (client.connect("ESP8266Client")) {
    if (client.connect("ESP8266Client", mqtt_user, mqtt_password)) {
      Serial.println("connected");
    } else {
      Serial.print("failed, rc=");
      Serial.print(client.state());
      Serial.println(" try again in 5 seconds");
      // Wait 5 seconds before retrying
      delay(5000);
    }
  }
}

void loop() {
  if (!client.connected()) {
    reconnect();
  }
  client.loop();
 
doorbellState = digitalRead(doorbellPin);

 if ( doorbellState == LOW ) {
    // Put your code here.  e.g. connect, send, disconnect.
    client.publish(doorbell_topic, "on" , true);
    Serial.println("Doorbell is pressed!");
    
    
    delay( 5000 );
  }
}

I see the right debug information in my arduino serial monitor:

Connecting to lofoten
...
WiFi connected
IP address: 
192.168.2.28
Attempting MQTT connection...connected
Doorbell is pressed!

In my automations I have this configured:

- alias: Doorbell ringing
  trigger:
    platform: mqtt
    topic: hal/doorbell
    payload: 'on'
  action:
    service: notify.pushbullet
    data:
      message: "Deurbel!"

and in my configuration I have:

mqtt:
  username: homeassistant
  password: mymqttpass

But when I trigger the doorbell, I do see the right output in the arduino serial monitor, yet there doesn’t seem to arrive any message in home assistant.

Any idea what could be wrong?

The sketch is publishing 1 when you press the door bell and your automation is looking for the payload on. Both values should be the same.

sorry, that was just something I changed to see if anything would happen if I would do that, but originally I had “on” in there, I now changed my post.

In the mean while I did some more digging around. I noticed that when I added this to my configuration.yaml:

sensor:
      - platform: mqtt
        name: "mqtt doorbell"
        state_topic: "hal/doorbell"
        qos: 0

I get a extra sensor, that does actually change to “on” when I sens the mqtt message.

I then changed my arduino code to:

 if ( doorbellState == LOW ) {
    // Put your code here.  e.g. connect, send, disconnect.
    client.publish(doorbell_topic, "on" , true);
    Serial.println("Doorbell is pressed!");
    delay( 5000 );
    client.publish(doorbell_topic, "off" , true);
  }

and I now see the sensor change to “on” when the doorbell is pressed, and switch back to off after 5 seconds :smiley:

I figured that would be enough to get my automation going, so I changed it to:

  trigger:
    platform: state
    entity_id: sensor.mqtt_doorbell
    to: 'on'
  action:
    service: notify.pushbullet
    data:
      message: "Deurbel!"

But unfortunately, it doesn’t get triggered :expressionless:
I’m also not sure why just a direct MQTT trigger doesn’t work, according to this documentation, it should be possible: https://home-assistant.io/docs/automation/trigger/ (And I think that some time in the past it did work for me :no_mouth: )

If you search in Dev Tools states for doorbell under entity, what do you get?

This is what I see there, goes to ‘on’ when I press the button, and ‘off’ 5 seconds later

In the mean while, I did go back to basics, and created this automation:

- alias: Doorbell ringing2
  trigger:
    platform: mqtt
    topic: hal/doorbell
  action:
    service: notify.pushbullet
    data:
      message: "Deurbel! via mqtt"

so I left out the “payload” this time, and to my surprise… it works! It does trigger (twice actually, because it will now trigger for the on and the off message). So it seems the payload causes the problem.

You could try like this to prevent firing twice:

  trigger:
    platform: state
    entity_id: sensor.mqtt_doorbell
    from: 'off'
    to: 'on'

Try using this:

- alias: Doorbell ringing2
  trigger:
    platform: state
    entity_id: sensor.mqtt_doorbell
    from: 'off'
    to: 'on'
  action:
    service: notify.pushbullet
    data:
      message: "Deurbel! via mqtt"

You might have forgotten your broker address?

mqtt:
  broker: IP OR SOMETHING
  username: homeassistant
  password: mymqttpass