Binary sensor by mqtt appears as unknown: solved

Hello, I’m trying to configure an MQTT button panel designed to be able to be placed outside the home and be able to control routines from there.
I am programming it at home, once it works, I will configure it externally.
The idea is that it has 4 buttons (binary sensors) and that each one sends the value in binary to its topic, obviously I am doing something wrong, this is the fragment of my code in HA:

binary_sensor:
  - name: "dev_0000_00"
    unique_id: "dev_0000_00"
    state_topic: "homeassistant/devices/demo/4btns/D0"
  - name: "dev_0000_01"
    unique_id: "dev_0000_01"
    state_topic: "homeassistant/devices/demo/4btns/D1"
  - name: "dev_0000_02"
    unique_id: "dev_0000_02"
    state_topic: "homeassistant/devices/demo/4btns/D2"
  - name: "dev_0000_03"
    unique_id: "dev_0000_03"
    state_topic: "homeassistant/devices/demo/4btns/D3"

This is the code for ESP32-WROOM in ArduinoIDE:

//MQTT_4_BTN_V2.ino

#include "Secrets.h"
#include "config.h"
#include <WiFi.h>
#include <PubSubClient.h>

#define D0_Pin 35
#define D1_Pin 34
#define D2_Pin 39
#define D3_Pin 36

bool    D0_Ste = false;
bool    D1_Ste = false;
bool    D2_Ste = false;
bool    D3_Ste = false;

WiFiClient espClient;
PubSubClient client(espClient);

void setup() {
  pinMode(D0_Pin, INPUT);
  pinMode(D1_Pin, INPUT);
  pinMode(D2_Pin, INPUT);
  pinMode(D3_Pin, INPUT);
  
  delay(5000);
  Serial.begin(115200);

  setup_wifi();
  client.setServer(CONFIG_MQTT_HOST, CONFIG_MQTT_PORT);
}

void setup_wifi() {
  delay(10);
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(CONFIG_WIFI_SSID);

  WiFi.mode(WIFI_STA);
  WiFi.begin(CONFIG_WIFI_SSID, CONFIG_WIFI_PASS);
  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() {
  while (!client.connected()) {
    Serial.print("Attempting MQTT connection...");
    if (client.connect(CONFIG_MQTT_CLIENT_ID, CONFIG_MQTT_USER, CONFIG_MQTT_PASS)) {
      Serial.println("connected");
    } else {
      Serial.print("failed, rc=");
      Serial.print(client.state());
      Serial.println(" try again in 5 seconds");
      delay(5000);
    }
  }
}

#define PrescalerK 25
int k=PrescalerK;
void loop() {  
  if (!client.connected()){reconnect();}

  while(!k--){
    if(digitalRead(D0_Pin) && D0_Ste == 0){
      D0_Ste = 1;
      client.publish(CONFIG_MQTT_TOPIC_BTN_000, "1", true);
    }
    if(!digitalRead(D0_Pin) && D0_Ste == 1){
      D0_Ste = 0;
      client.publish(CONFIG_MQTT_TOPIC_BTN_000, "0", true);
    }
    
    if(digitalRead(D1_Pin) && D1_Ste == 0){
      D1_Ste = 1;
      client.publish(CONFIG_MQTT_TOPIC_BTN_001, "1", true);
    }
    if(!digitalRead(D1_Pin) && D1_Ste == 1){
      D1_Ste = 0;
      client.publish(CONFIG_MQTT_TOPIC_BTN_001, "0", true);
    }
    
    if(digitalRead(D2_Pin) && D2_Ste == 0){
      D2_Ste = 1;
      client.publish(CONFIG_MQTT_TOPIC_BTN_002, "1", true);
    }
    if(!digitalRead(D2_Pin) && D2_Ste == 1){
      D2_Ste = 0;
      client.publish(CONFIG_MQTT_TOPIC_BTN_002, "0", true);
    }
    
    if(digitalRead(D3_Pin) && D3_Ste == 0){
      D3_Ste = 1;
      client.publish(CONFIG_MQTT_TOPIC_BTN_003, "1", true);
    }
    if(!digitalRead(D3_Pin) && D3_Ste == 1){
      D3_Ste = 0;
      client.publish(CONFIG_MQTT_TOPIC_BTN_003, "0", true);
    }
    client.loop();
    k=PrescalerK;
  }
  delay(10);
}
//config.h

#define CONFIG_MQTT_CLIENT_ID "dev_0000"

#define CONFIG_MQTT_TOPIC_BTN_000 "homeassistant/devices/demo/4btns/D0"
#define CONFIG_MQTT_TOPIC_BTN_001 "homeassistant/devices/demo/4btns/D1"
#define CONFIG_MQTT_TOPIC_BTN_002 "homeassistant/devices/demo/4btns/D2"
#define CONFIG_MQTT_TOPIC_BTN_003 "homeassistant/devices/demo/4btns/D3"

In a side menu panel, I have put the 4 entities, all of them appear as unknown:
image

The default payloads for the mqtt binary sensor are “OFF” and “ON”.

https://www.home-assistant.io/integrations/binary_sensor.mqtt/#payload_off

You are sending 1 and 0, so you need to define these payloads.

binary_sensor:
  - name: "dev_0000_00"
    unique_id: "dev_0000_00"
    state_topic: "homeassistant/devices/demo/4btns/D0"
    payload_on: "1"
    payload_off: "0"
  - name: "dev_0000_01"
    unique_id: "dev_0000_01"
    state_topic: "homeassistant/devices/demo/4btns/D1"
    payload_on: "1"
    payload_off: "0"
  - name: "dev_0000_02"
    unique_id: "dev_0000_02"
    state_topic: "homeassistant/devices/demo/4btns/D2"
    payload_on: "1"
    payload_off: "0"
  - name: "dev_0000_03"
    unique_id: "dev_0000_03"
    state_topic: "homeassistant/devices/demo/4btns/D3"
    payload_on: "1"
    payload_off: "0"

Thank you, I was missing that information, it is already solved

1 Like