Button to send MQTT message

Hi!

I want to add a button to the Lovelace GUI. When clicking on this button I would like to send a MQTT message. So I add the entity button via gui and switch to the editor. NextI tried to make a button like this:

type: entity-button
tap_action:
  action: call-service
  service: mqtt.publish
  service_data:
    retain: 0
    payload: "<?xml version=\"1.0\" encoding=\"UTF-8\"?><msg><sender>A0A5A58F-B787-4AB8-A4D8-F4FCFF7D114E</sender><target></target><command>RFID_OPENDOOR</command><params>ReaderId=4</params></msg>"
    topic: topic/subtopic
    qos: 0
hold_action:
  action: none
show_icon: true
show_name: true
entity: climate.soba_3
name: Vhod
icon: 'mdi:door'

But when I click save, the code is gone! Am I doing something wrong or is some kind of bug in the editor? Is the entity button the right choice since the action is not related to the entity?

1 Like

My UI is stored in a file (ui-lovelace.yaml) and I use VS Code to edit it (in other words, I’m not using the default UI editor). I tried your example, with only minor changes (the topic and the entity name), saved it (successfully), a large button named Vhod appeared, clicked it, and it published the MQTT payload. So, it works for me.

If you are using the default UI editor then you may be correct that it has a bug. As an experiment, change the payload so that it uses single-quotes to delimit the string:

    payload: '<?xml version=\"1.0\" encoding=\"UTF-8\"?><msg><sender>A0A5A58F-B787-4AB8-A4D8-F4FCFF7D114E</sender><target></target><command>RFID_OPENDOOR</command><params>ReaderId=4</params></msg>'

Other than that, I’m not sure why the editor would fail to save the code.

Hi!

Thank you for yor help. I tried your suggestion, but the problem with the editor persists. I will report a bug.

Tried also to edit directly the file /config/.storage/lovelace

                    "cards": [
                        {
                            "entity": "climate.soba_3",
                            "hold_action": {
                                "action": "none"
                            },
                            "icon": "mdi:door",
                            "name": "Vhod",
                            "show_icon": true,
                            "show_name": true,
                            "tap_action": {
                                "action": "call-service",
                                "service": "mqtt.publish",
                                "service_data": {
                                    "payload": "<?xml version=1.0 encoding=UTF-8?><msg><sender>hass-argo</sender><target></target><command>RFID_OPENDOOR</command><params>ReaderId=4</params></msg>",
                                    "qos": 0,
                                    "retain": 0,
                                    "topic": "topic/subtopic"
                                }
                            },
                            "type": "entity-button"
                        }
                    ],
                    "path": "test",
                    "title": "Vrata"

Also tried to remove double quotes from the payload (just for testing puroposes) and still not working. The button definition is now looking OK, but clicking on the button does nothing. Any suggestions?