home-assistant.io/source/_components/binary_sensor.mqtt.markdown
Dan Nixon 5d321e66f7 Update MQTT platforms that now support availablity (#4293)
To accompany changes from:
https://github.com/home-assistant/home-assistant/pull/11336

Updated platforms:
- Switch (previously supported)
- Binary sensor (previously supported)
- Cover (previously supported)
- Light
- JSON light
- Template light
- Sensor
- Alarm control panel
- Lock
- HVAC
- Fan
- Vacuum
2018-01-01 18:33:19 -08:00

104 lines
4.2 KiB
Markdown

---
layout: page
title: "MQTT Binary Sensor"
description: "Instructions how to integrate MQTT binary sensors within Home Assistant."
date: 2015-05-30 23:21
sidebar: true
comments: false
sharing: true
footer: true
logo: mqtt.png
ha_category: Binary Sensor
ha_release: 0.9
ha_iot_class: "depends"
---
The `mqtt` binary sensor platform uses an MQTT message payload to set the binary sensor to one of two states: `on` or `off`.
The binary sensor state will be updated only after a new message is published on `state_topic` matching `payload_on` or `payload_off`. If these messages are published with the `retain` flag set, the binary sensor will receive an instant state update after subscription and Home Assistant will display the correct state on startup. Otherwise, the initial state displayed in Home Assistant will be `unknown`.
The `mqtt` binary sensor platform optionally supports an `availability_topic` to receive online and offline messages (birth and LWT messages) from the MQTT device. During normal operation, if the MQTT cover device goes offline (i.e. publishes `payload_not_available` to `availability_topic`), Home Assistant will display the binary sensor as `unavailable`. If these messages are published with the `retain` flag set, the binary sensor will receive an instant update after subscription and Home Assistant will display the correct availability state of the binary sensor when Home Assistant starts up. If the `retain` flag is not set, Home Assistant will display the binary sensor as `unavailable` when Home Assistant starts up. If no `availability_topic` is defined, Home Assistant will consider the MQTT device to be available.
To use an MQTT binary sensor in your installation, add the following to your `configuration.yaml` file:
```yaml
# Example configuration.yaml entry
binary_sensor:
- platform: mqtt
state_topic: "home-assistant/window/contact"
```
{% configuration %}
name:
description: The name of the binary sensor.
required: false
type: string
default: MQTT Binary Sensor
state_topic:
description: The MQTT topic subscribed to receive sensor values.
required: true
type: string
payload_on:
description: The payload that represents the on state.
required: false
type: string
default: ON
payload_off:
description: The payload that represents the off state.
required: false
type: string
default: OFF
availability_topic:
description: "The MQTT topic subscribed to receive birth and LWT messages from the MQTT device. If `availability_topic` is not defined, the binary sensor availability state will always be `available`. If `availability_topic` is defined, the binary sensor availability state will be `unavailable` by default."
required: false
type: string
payload_available:
description: The payload that represents the online state.
required: false
type: string
default: online
payload_not_available:
description: The payload that represents the offline state.
required: false
type: string
default: offline
qos:
description: The maximum QoS level to be used when receiving messages.
required: false
type: integer
default: 0
device_class:
description: "The [type/class](/components/binary_sensor/) of the sensor to set the icon in the frontend."
required: false
type: string
value_template:
description: "Defines a [template](/docs/configuration/templating/#processing-incoming-data) to extract a value from the payload."
required: false
type: string
{% endconfiguration %}
To test, you can use the command line tool `mosquitto_pub` shipped with `mosquitto` or the `mosquitto-clients` package to send MQTT messages. To set the state of the binary sensor manually:
```bash
$ mosquitto_pub -h 127.0.0.1 -t home-assistant/window/contact -m "OFF"
```
The example below shows a full configuration for a binary sensor:
```yaml
# Example configuration.yaml entry
binary_sensor:
- platform: mqtt
name: "Window Contact Sensor"
state_topic: "home-assistant/window/contact"
payload_on: "ON"
payload_off: "OFF"
availability_topic: "home-assistant/window/availability"
payload_available: "online"
payload_not_available: "offline"
qos: 0
device_class: opening
value_template: '{% raw %}{{ value.x }}{% endraw %}'
```