Create a button to publish to mqtt

Hi,
I am new to HA and I am trying to create a button for the UI that when pressed, will publish to a mqtt topic.
I would like a push button on the UI to do nothing other than publish a value to an mqtt topic.
I have an instance of node red on another machine that will subscribe to the topic.
I have tried to create this through the button card config but I get an error:
UI editor is not supported for this config.
I tried it like this:

type: button
tap_action:
  action: call-service
  service: mqtt.publish  
  topic: south_garage_door
  payload: "0"
show_icon: false
name: South Garage Door

Thanks for any help. I am sorry but I am very new to HA,YAML

1 Like

You probably will have to create a script or shell command and call it from the button.

Like here:
https://community.home-assistant.io/t/mqtt-script-payload/211454/2?u=mcarty

1 Like

Thank you so much for your reply! I see the example you sent. Can the script or shell command be input through the Button Card Configuration? or do I need to do that somewhere else?

After creating the script it will show up in the lovelace button card dropdown selection.

I see it now. Thank you so much! I am glad there are people like you willing to help newbies like myself! You got me on the right track now. I appreciate that you took the time to help me!

I know the topic is quite old, but for reference, if you use the button-card from hacs you can do the following to make it without creating a script:

type: custom:button-card
tap_action:
  action: call-service
  service: mqtt.publish
  service_data:
    topic: <TOPIC GOES HERE>
    payload: <PAYLOAD GOES HERE>

Maybe it would work with the built-in button card.

3 Likes

Works with non-hacs button card with MQTT integration:

type: button
tap_action:
  action: call-service
  service: mqtt.publish
  service_data:
    topic: backyard/lights/8
    payload: 'on'
4 Likes