MQTT button
The MQTT button integrationIntegrations connect and integrate Home Assistant with your devices, services, and more. [Learn more] lets you send an MQTT message when the button is pressed in the frontend or the button press action is called. This can be used to expose some service of a remote device, for example reboot.
Configuration
An MQTT button can be set up in the following ways:
- By adding an MQTT device as a subentry in the UI.
- Through device discovery, as a component with
"platform": "button"(p) in the discovery payload of the device. - Through single component discovery, on the discovery topic
<discovery_prefix>/button/[<node_id>/]<object_id>/config. -
Manually in YAML, by adding the configuration to your
configuration.yamlThe configuration.yaml file is the main configuration file for Home Assistant. It lists the integrations to be loaded and their specific configurations. In some cases, the configuration needs to be edited manually directly in the configuration.yaml file. Most integrations can be configured in the UI. [Learn more] file, as shown in the example below.
The configuration variables below apply to discovery and YAML. How the device information is set depends on the configuration method. See MQTT device configuration.
After changing the configuration.yamlThe configuration.yaml file is the main configuration file for Home Assistant. It lists the integrations to be loaded and their specific configurations. In some cases, the configuration needs to be edited manually directly in the configuration.yaml file. Most integrations can be configured in the UI. [Learn more] file, restart Home Assistant to apply the changes.
# Example configuration.yaml entry
mqtt:
- button:
command_topic: "home/bedroom/switch1/reboot"
Configuration Variables
A list of MQTT topics subscribed to receive availability (online/offline) updates. Must not be used together with availability_topic.
The payload that represents the available state.
The payload that represents the unavailable state.
When availability is configured, this controls the conditions needed to set the entity to available. Valid entries are all, any, and latest. If set to all, payload_available must be received on all configured availability topics before the entity is marked as online. If set to any, payload_available must be received on at least one configured availability topic before the entity is marked as online. If set to latest, the last payload_available or payload_not_available received on any configured availability topic controls the availability.
Defines a template to extract device’s availability from the availability_topic. To determine the devices’s availability result of this template will be compared to payload_available and payload_not_available.
The MQTT topic subscribed to receive availability (online/offline) updates. Must not be used together with availability.
Defines a template to generate the payload to send to command_topic.
The MQTT topic that Home Assistant publishes to when you press the button in Home Assistant, including when you call the button.press action.
Use default_entity_id instead of name for automatic generation of the entity ID. For example, button.foobar. When used without a unique_id, the entity ID will update during restart or reload if the entity ID is available. If the entity ID already exists, the entity ID will be created with a number at the end. When used with a unique_id, the default_entity_id is only used when the entity is added for the first time. When set, this overrides a user-customized entity ID if the entity was deleted and added again.
Information about the device this entity is a part of, to tie it into the device registry. Only works when unique_id is set. See MQTT device configuration for the device options and how to set them for each configuration method.
The type/class of the button to set the icon in the frontend. The device_class can be null.
Controls whether this entity is enabled by default. When set to true, the entity is enabled and usable immediately. When set to false, Home Assistant adds the entity as disabled. A disabled entity has no state, and you can’t use it in automations or dashboards until you enable it.
The category of the entity.
Defines a template to extract the JSON dictionary from messages received on the json_attributes_topic. Usage example can be found in MQTT sensor documentation.
The MQTT topic subscribed to receive a JSON dictionary payload and then set as sensor attributes. Usage example can be found in MQTT sensor documentation.
Controls how long queued or retained messages sent from Home Assistant persist at the broker for offline subscribers. This option prevents the broker from retaining stale messages. The expected value for this option is a JSON mapping, for example, {"days": 1, "hours": 2, "minutes": 20, "seconds": 30} or {"seconds": 3600}.
Number of days published messages are queued or retained for offline subscribers.
Number of hours published messages are queued or retained for offline subscribers.
Number of minutes published messages are queued or retained for offline subscribers.
The name to use when displaying this button. Can be set to null if only the device name is relevant.
The payload that represents the available state.
The payload that represents the unavailable state.
Must be button. Only allowed and required in MQTT auto discovery device messages.
The maximum QoS level to be used when receiving and publishing messages.
If the published message should have the retain flag on or not.
An ID that uniquely identifies this button entity. If two buttons have the same unique ID, Home Assistant will raise an exception. Required when used with device-based discovery.
Make sure that your topic matches exactly. some-topic/ and some-topic are different topics.
Examples
In this section, you will find some real-life examples of how to use this feature.
Full configuration
The example below shows a full configuration for a button.
# Example configuration.yaml entry
mqtt:
- button:
unique_id: bedroom_switch_reboot_btn
name: "Restart Bedroom Switch"
command_topic: "home/bedroom/switch1/commands"
payload_press: "restart"
availability:
- topic: "home/bedroom/switch1/available"
qos: 0
retain: false
entity_category: "config"
device_class: "restart"