Publish an MQTT message

Use this action to publish a message to an MQTT topic. A common use is to send a command to a device that listens on MQTT, for example switching a light or relay, or to publish a value other systems can subscribe to.

Using this action from the user interface

If you prefer building automations and scripts visually, Home Assistant walks you through this action step by step. You pick what to target, tweak a few options, and save. No YAML knowledge required.

To publish a message from an automation or a script:

  1. Go to Settings > Automations & scenes.
  2. Open an existing automation or script, or select Create automation > Create new automation.
  3. If you’re setting up a new automation, add a trigger in the When section. Scripts don’t need a trigger. They run when something else calls them.
  4. In the Then do section, select Add action.
  5. Search for and select Publish.
  6. Enter the Topic to publish to.
  7. Optionally, enter the Payload to send. Under Publish options, you can also set the quality of service, the retain flag, and other options.
  8. Select Save.

Options in the UI

Topic

The topic to publish the message to.

Payload (Optional)

The message to publish. When you leave this empty, an empty message is published.

Evaluate payload (Optional)

When the payload is a Python bytes literal, evaluate it and publish the raw data. Off by default.

QoS (Optional)

The quality of service to use: 0 (at most once), 1 (at least once), or 2 (exactly once). The default is 0.

Retain (Optional)

When turned on, the broker stores the most recent message on the topic and sends it to new subscribers. Off by default.

Message Expiry Interval (Optional)

How long the broker keeps the message before it expires, in seconds. Only supported with MQTT protocol version 5.0.

Using this action in YAML

If you work directly in YAML, or you want to know exactly what Home Assistant does under the hood, this section has the technical reference. It lists the field names you use in YAML, their types, and which ones are required.

In YAML, refer to this action as mqtt.publish. A basic example looks like this:

ActionActions are used in several places in Home Assistant. As part of a script or automation, actions define what is going to happen once a trigger is activated. In scripts, an action is called *sequence*. [Learn more]
action: mqtt.publish
data:
  topic: "homeassistant/light/1/command"
  payload: "ON"

Options in YAML

topic string Required

The topic to publish the message to.

payload string

The message to publish. When omitted, an empty message is published.

evaluate_payload boolean

When the payload is a Python bytes literal, evaluate it and publish the raw data.

qos integer

The quality of service to use: 0 (at most once), 1 (at least once), or 2 (exactly once).

retain boolean

When set, the broker stores the most recent message on the topic and sends it to new subscribers.

message_expiry_interval integer

How long the broker keeps the message before it expires, in seconds. Only supported with MQTT protocol version 5.0.

Good to know

  • The payload must be a string. The MQTT integration supports templates, so you can build the payload from the state of your entities.

  • To publish JSON, format and escape it as a string. A folded block keeps it readable:

    topic: "homeassistant/light/1/state"
    payload: >-
      {"Status": "off", "Data": "something"}
    
  • When the payload is a Python bytes literal and you want to publish raw data instead of text, turn on Evaluate payload.

  • Publishing an empty message with Retain turned on clears a previously retained message on that topic.

More examples

Real scenarios where this action shows up in automations and scripts. Copy any example and adapt it to your setup.

Tip

You don’t need to edit YAML to use these examples. Copy a YAML snippet from this page, open the automation editor in Home Assistant, and press Ctrl+V (or Cmd+V on Mac). Home Assistant automatically converts the pasted YAML into the visual editor format, whether it’s a full automation, a single trigger, a condition, or an action.

Automation: turn on a device when you get home

Send a command to a device over MQTT as soon as you arrive home.

  • Trigger: You arrive home
  • Action: Publish an MQTT message
Show example YAML
AutomationAutomations in Home Assistant allow you to automatically respond to things that happen in and around your home. [Learn more]
alias: "Turn on the porch light when I get home"
triggers:
  - trigger: state
    entity_id: person.me
    to: "home"
actions:
  - action: mqtt.publish
    data:
      topic: "homeassistant/light/porch/command"
      payload: "ON"
      retain: true

Publish an MQTT discovery configuration

You can use this action to set up a device through MQTT discovery by publishing a configuration message. The example below sets up a temperature sensor.

Show example YAML
ActionActions are used in several places in Home Assistant. As part of a script or automation, actions define what is going to happen once a trigger is activated. In scripts, an action is called *sequence*. [Learn more]
action: mqtt.publish
data:
  topic: "homeassistant/sensor/bathroom_temperature/config"
  payload: >-
    {"device_class": "temperature",
    "unit_of_measurement": "\u00b0C",
    "value_template": "{{ value | float }}",
    "state_topic": "sensors/bathroom/temperature",
    "unique_id": "bathroom_temperature",
    "device": {
    "identifiers": "bathroom_sensor",
    "name": "Bathroom",
    "manufacturer": "rtl_433" }
    }

Still stuck?

The Home Assistant community is quick to help: join Discord for real-time chat, post on the community forum with the action you’re calling and what you expected to happen, or share on our subreddit /r/homeassistant.

Tip

AI assistants like ChatGPT or Claude can also explain actions or suggest the right one when you describe what you want in plain language.

Related actions

These actions work well alongside this one: