Simple front end button to publish a MQTT message via a script

I am trying to get a simple momentary button on my Home Assistant front page that will publish a MQTT message, but I can seem to do it. From what I have read, creating a script would be a good way to do it. But so far, every script that I have tried come back with invalid config.

I have a ESP8266 that subscribes to MQTT and I want to send it common messages through the HA front end and then automate it obviously.

script:
  Mqtt_Button:
    sequence:
      service: mqtt.publish
      data:
        topic: "/mqtt/Door/topic/"
        payload: "ON"

Any ideas on how to do this and what I am doing wrong?

Captial letters should be avoided in HA configuration, they create all kinds of mysterious errors.

But I can’t see anything else wrong. Here is an example of something working, although I use one script to call another as I have a number doing the same thing

  send_trv_command:
    sequence:
      - service: mqtt.publish
        data_template:
          topic: '/energenie/eTRV/Command/{{command}}/{{id}}'
          payload: 'ON'

  send_report_voltage_office:
    sequence:
      - service: script.send_trv_command
        data:
          id: '296'
          command: 'Voltage'

1 Like

That did it. :slight_smile: I changed all upper case letters to lower case letters and now it work as I had expected it to. That is rather annoying, but now that I know about it, I will be sure to make every thing lower case.

Thanks for your help.

Hi,
Sorry for my noob questrion again, not relevant to this topic:
I configured sensor on tasmota and having log as folllowing:

05:57:30.087 MQT: tele/tasmota_ir/SENSOR = {"Time":"2024-07-31T05:57:30","LD2410":{"Distance":[523.0,534.0,500.0],"Energy":[20,100]}}

How can I create a sensor based on this information?
I tried this but failed, Home Assisstant does not create the sensors:

sensor:
  - platform: mqtt
    name: "LD2410 Distance"
    state_topic: "tele/tasmota_ir/SENSOR"
    value_template: "{{ value_json.LD2410.Distance[0] }}"
    unit_of_measurement: "cm"

  - platform: mqtt
    name: "LD2410 Energy"
    state_topic: "tele/tasmota_ir/SENSOR"
    value_template: "{{ value_json.LD2410.Energy[0] }}"
    unit_of_measurement: "unit"

Thank you

Thats the old mqtt format.

its now something like

mqtt:
    sensor:
      - name: "LD2410 Distance"
.....

See here for more info:

1 Like

:grinning: :grinning: :grinning:
Thank you so much,
I got it work already, however, big delay with the sensors