I usually control the lights in my home using zigbee2mqtt and Node-RED, and they work fine. Now I want to display MQTT devices in Home Assistant. If I set it as a “sensor,” it works normally, but I want to set it as a “binary sensor,” and it shows an error. I have carefully read the instructions, but I still don’t know which parameter is set incorrectly. I have already downloaded the diagnostics, and I found that the payload can display normally: contact: true. It seems that MQTT can recognize this sensor normally, but I don’t know why it shows as unavailable.
I’m a beginner, please help me take a look.Incorrect binary sensor configuration below
mqtt: - binary_sensor: name: "Binary_Sensor3" state_topic: "zigbee2mqtt/second_door" unique_id: "second_door_binary_sensor" payload_on: "true" payload_off: "false" value_template: "{{ value_json.contact }}"
If I set it as a binary sensor, the log can also normally recognize the device’s contact state as true, but it will prompt the entity as unavailable.
I know nothing about MQTT, but HA binary sensors are normally “on” or “off”. Have a look at one in the states page of Developer Tools.
try with
mqtt:
- binary_sensor:
name: "Binary_Sensor3"
state_topic: "zigbee2mqtt/second_door"
unique_id: "second_door_binary_sensor"
value_template: "{{ value_json.contact == true }}"
Sorry, I screenshotted the wrong entity earlier. It should be this one, and the status should be Unknown, not Unavailable.
I changed your code, but it’s still showing as Unknown.
Maybe post this image as text
Your original code looks good to me: payload_on
says you’re looking for the output to be true
; and the value_template
should be returning that true
value for comparison:
Are there any errors in the logs? Is this the only mqtt:
header in your configuration.yaml
? Check your indentation: the section under binary_sensor:
is only indented by one space. That shouldn’t cause a problem if it’s consistent but it’s not the convention.
Here is the partial content of the diagnostics below, please take a look.
},
"devices": [],
"mqtt_debug_info": {
"entities": [
{
"entity_id": "sensor.contact",
"subscriptions": [
{
"topic": "zigbee2mqtt/second_door",
"messages": [
{
"payload": "{\"battery\":100,\"battery_low\":false,\"contact\":true,\"linkquality\":84,\"tamper\":false,\"voltage\":3000}",
"qos": 0,
"retain": 1,
"time": "2024-05-14T12:11:46.832415+00:00",
"topic": "zigbee2mqtt/second_door"
},
{
"payload": "{\"battery\":100,\"battery_low\":false,\"contact\":true,\"linkquality\":69,\"tamper\":false,\"voltage\":3000}",
"qos": 0,
"retain": 0,
"time": "2024-05-14T12:14:28.696763+00:00",
"topic": "zigbee2mqtt/second_door"
},
{
"payload": "{\"battery\":100,\"battery_low\":false,\"contact\":true,\"linkquality\":63,\"tamper\":false,\"voltage\":3000}",
"qos": 0,
"retain": 0,
"time": "2024-05-14T12:14:28.701667+00:00",
"topic": "zigbee2mqtt/second_door"
}
]
}
],
"discovery_data": {
Could you answer my two questions? System logs are here:
Hello my friend, during the reboot process, HA did not report any errors. I checked the log records and there were only some other error prompts. There was only one reduction, and I merged several screenshots together, as shown in the picture.
As a newbie on the forum, I’m only able to reply with one screenshot at a time, I apologize for that. The log content does not contain anything related to MQTT, and there were no errors reported during the reboot.
That’s one of the reasons it should be pasted as correctly-formatted text .
Couldn’t make sense of your screenshot. Can you post configuration.yaml
as code here please, obviously redacting any passwords etc.
I have changed the username and password to XX, while keeping everything else exactly as it was.
# Configure a default setup of Home Assistant (frontend, api, etc)
default_config:
# Text to speech
tts:
- platform: google_translate
group: !include groups.yaml
automation: !include automations.yaml
script: !include scripts.yaml
scene: !include scenes.yaml
sonoff:
username: XX
password: XX
reload: always
default_class: switch
device_tracker:
- platform: luci
host: 192.168.31.199
ssl: false
verify_ssl: false
username: XX
password: XX
interval_seconds: 5
consider_home: 10
new_device_defaults:
track_new_devices: true
frontend:
themes: !include_dir_merge_named themes
mqtt:
- binary_sensor:
name: "Binary_Sensor3"
state_topic: "zigbee2mqtt/second_door"
unique_id: "second_door_binary_sensor"
value_template: "{{ value_json.contact }}"
media_player:
- platform: emby
host: 192.168.31.33
api_key: XX
switch:
- platform: konke
name: konke_pro_1
model: minik
host: 192.168.31.67
- platform: konke
name: konke_pro_2
model: minik
host: 192.168.31.18
The following code contains all the content of the mqtt diagnostics
{
"home_assistant": {
"installation_type": "Home Assistant Container",
"version": "2024.4.3",
"dev": false,
"hassio": false,
"virtualenv": false,
"python_version": "3.12.2",
"docker": true,
"arch": "x86_64",
"timezone": "Asia/Shanghai",
"os_name": "Linux",
"os_version": "4.4.302+",
"run_as_root": true
},
"custom_components": {
"hacs": {
"version": "1.34.0",
"requirements": [
"aiogithubapi>=22.10.1"
]
},
"sonoff": {
"version": "3.6.0",
"requirements": [
"pycryptodome>=3.6.6"
]
},
"konke": {
"version": "1.0",
"requirements": [
"pykonkeio==2.1.7"
]
},
"nodered": {
"version": "3.1.5",
"requirements": []
},
"hon": {
"version": "0.7.0",
"requirements": []
}
},
"integration_manifest": {
"domain": "mqtt",
"name": "MQTT",
"codeowners": [
"@emontnemery",
"@jbouwh"
],
"config_flow": true,
"dependencies": [
"file_upload",
"http"
],
"documentation": "https://www.home-assistant.io/integrations/mqtt",
"iot_class": "local_push",
"quality_scale": "gold",
"requirements": [
"paho-mqtt==1.6.1"
],
"is_built_in": true
},
"data": {
"connected": true,
"mqtt_config": {
"broker": "192.168.31.33",
"port": 1883,
"username": "**REDACTED**",
"password": "**REDACTED**",
"discovery": true,
"discovery_prefix": "homeassistant",
"birth_message": {
"topic": "homeassistant/status",
"payload": "online",
"qos": 0,
"retain": false
},
"will_message": {
"topic": "homeassistant/status",
"payload": "offline",
"qos": 0,
"retain": false
}
},
"devices": [],
"mqtt_debug_info": {
"entities": [
{
"entity_id": "binary_sensor.binary_sensor3",
"subscriptions": [
{
"topic": "zigbee2mqtt/second_door",
"messages": [
{
"payload": "{\"battery\":100,\"battery_low\":false,\"contact\":true,\"linkquality\":78,\"tamper\":false,\"voltage\":3000}",
"qos": 0,
"retain": 1,
"time": "2024-05-15T14:06:42.172794+00:00",
"topic": "zigbee2mqtt/second_door"
}
]
}
],
"discovery_data": {
"topic": "",
"payload": ""
},
"transmitted": []
}
],
"triggers": []
}
}
}
No obvious issues there. I think you should re-instate your payload config. Could you try the following code?
mqtt:
- binary_sensor:
name: "Binary_Sensor3"
state_topic: "zigbee2mqtt/second_door"
payload_on: "true"
payload_off: "false"
value_template: "{{ value_json.get('contact', 'true') }}"
json_attributes_topic: "zigbee2mqtt/second_door"
I just modified your code into the configuration.yaml file and then restarted HA, only to find that the sensor was not present in the Settings-devices&services-MQTT-entities
Subsequently, I just added a line :unique_id: “second_door_binary_sensor_4”, and upon restarting, I noticed that the entity had appeared, but its status remained unknown. This issue is quite peculiar, but it doesn’t significantly impact my life. The sensor has already automated the lighting through Node-RED, so displaying it in the HA UI is more of a ‘nice to have’ feature. If it can’t be achieved, that’s fine too.
Thank you very much!
Which MQTT broker are you using?
I am using a Synology DS920+ and have installed four containers using Docker: Home Assistant (HA), zigbee2mqtt, Node-RED, and eclipse-mosquitto. Additionally, I have set up MQTT (Integrations) within Home Assistant.
My MQTT broker should be the Docker version of Eclipse Mosquitto.